2008-01-27

mod_python

So, the Twisted engine turned out to be a bit more complicated than necessary for my needs. Maybe later..

A more conservative approach is needed, hence Apache/mod_python. To get it running in Ubuntu (assuming you have Apache already installed), do:

sudo apt-get install libapache2-mod-python

sudo apt-get install libapache2-mod-python-doc

sudo emacs /etc/apache2/sites-enabled/000-default

(Or whatever editor one is using.)

..and inside <Directory /var/www/> insert these lines:

AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

Exit Emacs and restart Apache:

sudo /etc/init.d/apache2 restart

Done.

Ok, not quite. The above enables mod_python Publisher handler which operates at a high level. Instead of, or in addition to above, put something like this into Apache config:

<Directory /var/www/python>
        AddHandler mod_python .py
        PythonHandler python
        PythonDebug On
</Directory>

Now you have more control over the response:

from mod_python import apache

def handler(req):
        import time

        req.content_type = "text/plain"
        req.headers_out['CacheControl'] = "no-cache"
        req.headers_out['Expires'] = "-1"
        req.headers_out['This-is-my-own-wacky-and-useless-header'] = "Pangalactic Gargleblaster"
        req.write(str(time.time()))

        return apache.OK