mod_python and wsgi

Every time I sit down to do something with mod_python, I have problems. Usually they can be traced to forgetting to RTFM.

It doesn’t help that I tend to put experimental code in my apache www dir on occasion — and forget to back it up — so that after a system reinstall, I then have to dredge the dim, dark recesses of my memory to remember how to do simple stuff.

So for my own future reference…

  1. a WSGI handler (wsgi_handler.py) for mod_python can be found here
  2. the handler needs to be accessible on the python path, so copying it to /usr/lib/python2.4/site-packages means it’s accessible (there’s a better way, but it momentarily escapes me)
  3. the apache config required is:
    <Directory /var/www/test>
    PythonHandler wsgi_handler
    PythonOption WSGI.Application hello::simple_app
    AddHandler python-program .py
    </Directory>
    

    Where:

    • /var/www/test is an absolute path to a directory I’m serving on the web
    • hello is a python script (hello.py — see below)
    • simple_app is a function in the script

    In the case of my kubuntu laptop, I just created a mod_python.conf file containing the above in /etc/apache2/mods-enabled

  4. hello.py, a simple WSGI app, looks like this (snaffled from the python PEP for WSGI):
    def simple_app(environ, start_response):
        status = '200 OK'
        response_headers = [('Content-type','text/plain')]
        start_response(status, response_headers)
        return ['Hello world!\n']
    
  5. restart apache, and hopefully we’re away…

I’ve also added an extension to make wsgi_handler slightly more useful for my purposes; the ability to serve more than one ‘app’ from the same script. The extended version can be found here.

The apache configuration can then be modified to:

<Directory /var/www/test>
PythonHandler wsgi_handler
PythonOption WSGI.Application hello
AddHandler python-program .py
</Directory>

And hello.py now looks like this:

def simple_app(environ, start_response):
    status = '200 OK'
    print str(environ)
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello WSGI world!\n']

def test(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-Type','text/plain')]
    start_response(status, response_headers)
    return ['test test test\n']

Meaning that you can now point your browser to:

http://localhost/test/test.py/simple_app

or:

http://localhost/test/test.py/test

Which is useful for experimentation.

RSS feed | Trackback URI

1 Comment »

Comment by jrbriggs
2006-07-12 22:31:30

Another note to self:

Recalled the ‘better way’ to ensure wsgi_handler is accessible to mod_python — use the Python path directive:

PythonPath "sys.path + [ '/path/to/wsgi_handler' ]“

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Trackback responses to this post