1. Home
  2. Computing & Technology
  3. Python

Testing CGI With Python

From , former About.com Guide

2 of 4

Testing CGI With Python: What You Need to Import

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:

#!c:/python25/python.exe

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.

Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Web Development
  5. Python CGI Testing: How to Test CGI With Python

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

All rights reserved.