Wednesday, July 14, 2010

Python scripts as CGI on a web server

Today I wanted to run python scripts on a server. I have already Apache installed on my Ubuntu machine at home.
I dropped a python script (called rubbish.py) at /usr/lib/cgi-bin/rubbish.py But I couldn't see the output in my browser at: localhost/cgi-bin/rubbish.py (there was an error).
So I looked for help from the python cgi-script documentation. And their sample script works, it gives an html output that I can see in my browser. The main thing I had forgotten seems to be the need for the comment #!/usr/bin/env python as a first line of the file.
Here is what I see at the address http://localhost/cgi-bin/rubbish.py :


I wanted to run the script without cgi-bin, so i installed modwsgi.
Then restarted the Apache with sudo /etc/init.d/apache2 restart
I added a little python script into the /var/www/ folder. But it doesn't execute. Instead it loads the file. I saw somewhere that I have to change the .htaccess file. To tell the server that this is not just a file but a script the line AddHandler python-program .py does this. Well somehow it doesn't word. And the Apache doc says: "In general, you should never use .htaccess files unless you don't have access to the main server configuration file." I should change the httpd.conf file located at /etc/apache2/ on my computer. Pffffiou, :-) I'll do this another time, I already spent the whole afternoon on it.
  • I got inspiration from this forum (first result on google for "LAMP server python"), but then needed more help to get to the correct cgi script formatting (the first line with #!/usr/bin/env python).

No comments: