HomePython Page 3 - Writing CGI Programs in Python
Your First CGI program in Python - Python
And now for something completely different... Python has a very extensive, well documented and portable module library that provides a large variety of useful functions. The Internet-related collection is particularly impressive, with modules to deal with everything from parsing and retrieving URL's to retrieving mail from POP servers and everything in between.
In this section, I'm going to assume you have Python installed and have set up your web server to actually execute Python scripts (instead of, for example, sending the source code to the client.) There are so many variations of operating systems and web servers that I'm going to have to rely on you following the appropriate directions that came with your copy of Python.
A tip for Unix users: often a CGI script has to have the executable attribute set ("chmod +x script.py") and/or have a special extension or be in a special CGI directory. Consult your friendly local web administrator for details.
Please note that if you're using Apache, using mod_python is not absolutely necessary but is recommended for most applications. It doesn't affect anything in your program other than the (perceived) execution speed because it eliminates the start-up time for the interpreter with each connection.
Here is a (very) simple script to test your setup:
# Tell the browser how to render the text
print "Content-Type: text/plain\n\n"
print "Hello, Python!" # print a test string
Let's go over the script line-by-line to familiarize ourselves with the basic components of a Python script.
The first line is necessary for most Linux/Unix systems, but is harmlessly useless on all other systems. It is simply a signal to the shell that This Is A Python Program. You will need to change the path if Python is not installed in the usual place. (Sometimes it is in /usr/local/bin/python for example.) Other systems, such as Windows or Macintosh, will have to follow different procedures to inform the OS to execute this as a Python file. Since the first line begins with a # character, it is a comment and has no actual effect in the Python program itself.
Next we have another comment describing what the first line of actual code does: simply print a string (to "standard output") describing what kind of content follows. This is necessary for most browsers to know how to display the information. In this case it's just plain text, but we might want 'Content-Type: text/html' in the future. The '\n' is a special character that says "print a new line." Normally the print command will automatically add a newline for you, but in this case we need two (because that's what browsers expect.) The signals a special character. Use to actually print a slash.
Finally, we simply say hello to the world! Note that Python statements do not end with a semicolon or other punctuation, just a newline.
If everything went according to plan, visiting the URL of the above script should print "Hello, Python!" to your browser window.