Python as a CGI Program

June 15th, 2005

I’ve been using Python as an offline scripting language for some time. It never really occurred to me to try and get it working on the internet, CGI programs seem a little outdated to me when PHP is nearly always readily available. However I like the way it uses whitespace instead of a syntax soup and the fact that it has a lot of documentation. I’ll continue learning it offline but it would be nice to get some practical use out of it, so here’s my first foray into using Python as a CGI program:

I’m going to use a very simple “Hello World” example:

#!/usr/bin/python
print "Content-type: text/html\n\n"
print "Hello World!"

My first attempts ended dismally as I forgot to put a newline between the Content-type header resulting in a “500 Server Error” and a:

Premature end of script headers

Entered into the server’s error logs. Remember that “\n” is one *nix line terminator, and another “\n” is a blank line. A further notice is on the first “shebang” line is that you are going to have to contact your server’s administrator on what the path to the Python interpreter is. Personally I just guessed /usr/bin and it was right, you could try /usr/local/bin, but if you can’t find it just email the server people, I’m sure they wont mind.

Once you have the above in a Python file (I called mine snake.py) upload it to your cgi-bin directory. If you can get at the file permissions with your FTP program, set the file’s mode to “755″. This gives everyone read and execute privileges, so that the file can be run by the interpreter. A little tip for those who are confined to cPanel: you can set the file permissions by using “File Manager” that can be found somewhere amongst all the icons when you login.

Next just point your browser to the script. You should get a nice “Hello World!” For the more adventurous among you, scrub the code and put this in its place:

#!/usr/bin/python
import cgi
cgi.test()

It displays a lot of information about your current version of Python and some variables.

2 Responses to “Python as a CGI Program”

  1. Jon Says:
    June 20th, 2005 at 4:00 am

    There is one drawback with Python it is slow. It should be named Turtle or something like that, perhaps snakes are slow too…

    You should try to time a really long loop that multiplies a couple of numbers in Python compared to PHP. I think PHP would win.

  2. Alex Says:
    June 20th, 2005 at 9:21 pm

    I’m not sure how it compares to PHP.

    There’s a link to a benchmark page, but I don’t really understand it, or really care about understanding it.

    They’re both interpreted languages, so I guess they’re both pretty slow anyway, neither being suited to large calculations. I’m still using PHP a lot, but I’m just intrigued by Python for some reason.

    I found a year old “AJAX” Python implementation here, which is highly interesting. Of course PHP will be the king of web programming for some time yet, but I reckon when it needs to offload to a Perl program for one reason or another, Python will be there instead.

    Oh, and since Google uses it heavily, I see it as a worthwhile investment. ;)