1. Home
  2. Computing & Technology
  3. Python

Testing CGI With Python

By Al Lukaszewski, About.com

1 of 4

The Test CGI Script

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.

#!/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>"
Let's take this apart line-by-line.

1 of 4

Explore Python

More from About.com

  1. Home
  2. Computing & Technology
  3. Python

©2008 About.com, a part of The New York Times Company.

All rights reserved.