As you know, Python CGI programming relies on the cgi and cgitb modules. Within the cgi module, however, is a little used function that you can use to create a CGI test script.
While debugging software is available for most web servers, such software often puts you out of control of the testing process. This is a program that you can use as a drop-in replacement for any CGI script that is not working. It is a great way of gathering the salient environment data without weeding your way through Apache's configuration or log files.
Let's take this apart line-by-line.
#!/usr/bin/python import sys sys.stderr = sys.stdout import os from cgi import escape print "Content-type: text/html" print print "<html><head><title>Situation snapshot</title></head><body><p>" print "Running:" print "<b>Python %s</b><br><br>" %(sys.version) print "Environmental variables:<br>" print "<ul>" for k in sorted(os.environ): print "<li><b>%s:</b>\t\t%s<br>" %(escape(k), escape(os.environ[k])) print "</ul></p></body></html>"
The first line is, as you know, the "bang" line (sometimes called the "shebang" line) and tells the web server where to find the interpreter for the program (nb: always use absolute paths for this). If you are on Windows, you should substitute for this line the following:
After importing the sys module, we redirect all error output (stderror) to the screen (stdout). In this way, we ensure that every problem that Python hits is reflected in the output.
Next we import cgitb and os. cgitb prettifies and reports any errors that are thrown in execution. As we will be working with operating system variables, we need the os module.
Importing cgitb is not enough. One must remember to enable it -- the next line does this.
Finally, we import a single function, escape from the cgi module. The purpose of this function is to escape characters such as ampersand ('&'), greater than ('>'), and less than ('<') to HTML-safe sequences ('$amp;', '>', '<', respectively).
The four subsequent print statements set the framework for the HTML output. The fifth calls on the version function of the sys module to determine the version of Python being used, the compiler used to create the binary, and on what kind of system it is running.
After the output "Environmental variables", we use a for-loop to work through the dictionary os.environ. We bracket this with the "<ul>...</ul>" tags so the output can be in bulleted format.
The keys of the os.environ dictionary change with the environment into which you import the os module. If you use it in a terminal shell, you will get output like this (taken from my terminal session on an Ubuntu Linux system):
LESSCLOSE: /usr/bin/lesspipe %s %s
LESSOPEN: | /usr/bin/lesspipe %s
In the case of Apache, you get output similar to the following:
HTTP_USER_AGENT: Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:126.96.36.199) Gecko/20070327 Ubuntu/dapper-security Firefox/188.8.131.52
SERVER_SIGNATURE: <address>Apache/2.0.55 (Ubuntu) mod_jk/1.2.14 mod_python/3.1.4 Python/2.4.3 PHP/5.1.2 Server at 192.168.0.2 Port 80</address>
SERVER_SOFTWARE: Apache/2.0.55 (Ubuntu) mod_jk/1.2.14 mod_python/3.1.4 Python/2.4.3 PHP/5.1.2
This for-loop will work on Python 2.4 and later. If it does not work for you, you may find that your Python installation needs updating. In the meantime, you can substitute these three lines for the one beginning "for k in...":
keys = os.environ.keys() keys.sort() for k in keys:
With either for-loop, you will get the same output. The result will be a small Python CGI script that you can use anywhere to figure out the environment in which you are programming. I should note in closing that parts of this tutorial were inspired by the coding of Jeff Bauer and Carey Evans and their script of a similar nature.