views:

2202

answers:

3

I want to get a simple Python "hello world" web page script to run on Windows Vista/ Apache but hit different walls. I'm using WAMP. I've installed mod_python and the module shows, but I'm not quite sure what I'm supposed to do in e.g. http.conf (things like AddHandler mod_python .py either bring me to a file not found, or a forbidden, or module not found errors when accessing http://localhost/myfolder/index.py). I can get mod_python.publisher to work but do I "want" this/ need this?

Can anyone help?

Thanks!

+1  A: 

You do not NEED mod_python to run Python code on the web, you could use simple CGI programming to run your python code, with the instructions in the following link: http://www.imladris.com/Scripts/PythonForWindows.html

That should give you some of the configuration options you need to enable Python with CGI, and a google search should give you reams of other info on how to program in it and such.

Mod_python is useful if you want a slightly more "friendly" interface, or more control over the request itself. You can use it to create request filters and other things for the Apache server, and with the publisher handler you get a simpler way of handling webpage requests via python.

The publisher handler works by mapping URLs to Python objects/functions. This means you can define a function named 'foo' in your python file, and any request to http://localhost/foo would call that function automatically. More info here: http://www.modpython.org/live/current/doc-html/hand-pub-alg-trav.html

As for the Apache config to make things work, something like this should serve you well

<Directory /var/www/html/python/>
  SetHandler mod_python
  PythonHandler mod_python.publisher
  PythonDebug On
</Directory>

If you have /var/www/html/ set up as your web server's root and have a file called index.py in the python/ directory in there, then any request to http://localhost/python/foo should call the foo() function in index.py, or fail with a 404 if it doesn't exist.

Daniel Bruce
Many, many thanks for your answer!
Philipp Lenssen
A: 

AddHandler mod_python .py

Have you set 'PythonHandler'?

These days, consider using WSGI instead of native mod-python interfaces for more wide-ranging deployment options. Either through mod-python's WSGI support, or, maybe better, mod-wsgi. (CGI via eg. wsgiref will also work fine and is easy to get set up on a development environment where you don't care about its rubbish performance.)

bobince
+8  A: 

Stay away from mod_python. One common misleading idea is that mod_python is like mod_php, but for python. That is not true. Wsgi is the standard to run python web applications, defined by PEP 333. So use mod_wsgi instead.

Or alternatively, use some web framework that has a server. Cherrypy's one is particulary good. You will be able to run your application both standalone and through mod_wsgi.

An example of Hello World application using cherrypy:

import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

application = HelloWorld()
if __name__ == '__main__':
    cherrypy.engine.start()
    cherrypy.engine.block()

Very easy huh? Running this application directly on python will start a webserver. Configuring mod_wsgi to it will make it run inside apache.

nosklo
Thanks for you great answer, too!
Philipp Lenssen
Running mod_wsgi application on Apache is fairly straight forward. I definitely recommend using CherryPy and it works well with Apache and mod_wsgi, even though it has its own light-wieght server.
Mohan Gulati