Skip to content

Web programming

Python

Python can be used embedded in apache like php, but with % tags.

installation

There are two different methods, mod_python.psp (python server pages) that use a php like construct. And using your own handler.

psp

Very terse !!

install
apt-get install apache2-mod-python

Add the top so it will look something like :

/etc/apache2/sites-enabled/default
1
2
3
4
5
6
7
...
ServerAdmin webmaster@localhost
DocumentRoot /path/to/your/sites/files
AddHandler mod_python .py
PythonHandler mod_python.psp
PythonDebug On
...
restart
/etc/init.d/apache restart

testing

Now tryout a file in your document root like :

hello
<%= "Hello world %>

writing the handler

Add a section like this to /etc/apache2/sites-enabled/default. Or another appropriate location.

/etc/apache2/sites-enabled/default
1
2
3
4
5
<Directory /var/www/report>
     AddHandler mod_python .py
     PythonHandler watch
     PythonDebug On
</Directory>

This means for all scripts with the .py extension under /var/www/report (absolute path) use mod_python. And in particular run the watch.py script. : from mod_python import apache def handler(req): req.content_type = "text/plain" req.write("Hello World!") return apache.OK Actually now any url you end in .py will work, http://localhost/report/watch.py but also /blerk.py even if it does not exits.