Wednesday, February 20, 2013

Phyton-CGI with lighttpd

Since static html pages are boring and for little girls only, we now want to create our first website with phyton-cgi calls.

Add/change the following in your lighttpd configuration file (/etc/lighttpd/lighttpd.conf):
$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )
Create a directoy "/var/www/cgi-bin/" for your phyton scripts, and also make sure that phyton is installed and the binary is "/usr/bin/phyton" - otherwise change according to your setup.
Now we change or index.html file and add a simple form to it:
<form method="GET" action="/cgi-bin/test.cgi">
  <input type="submit" value="Start" name="Start"/>
Then we create our phyton file "/var/www/cgi-bin/" with following content:
import cgi
import cgitb 
cgitb.enable()  # for troubleshooting
cgi.test()      # cgi test output
Now we can go to our website and click on the "Start"-Button, which will call our python-cgi script and outputs a page with all kinds of environment variables etc. It worked!

If you have problems try restarting the lighttpd service and check if you have the rights to read/write/execute stuff in your /var/www/ directory.

More information about phyton & cgi can be found here:

No comments: