cgimodel: CGI Programming Made Easy with Python

Always look on the bright side of life and at a method for debugging CGI programs on the command line.

The Common Gateway Interface (CGI) is a way in which you can let others from all over the world execute a program that resides on your computer. CGI is dynamic, since it runs in real time. You can decorate the CGI output with HTML (Hyper Text Markup Language). Most of the time, CGI is used as a front end for existing applications. CGI can be easy or complex, depending on the complexity of your project. Most CGI developers know the frustration which comes with debugging the CGI programs.

We present a very simple and robust way of doing CGI programming with Python. Debugging your CGI is easy, since you can do it on the command line, and integrating existing applications to work with CGI is just one step.

For our work, we chose Python, an object-oriented scripting language with a clear syntax. It is very easy to use, widely available and is free software.

Our intended audience is both experienced and novice CGI programmers. We will use the words “function” and “method” interchangeably. Note that CGI can be written in any computer language.

The GET and POST Methods

There are two ways of invoking CGI programs: through a URL with all data included, or by submitting HTML forms.

The two methods defined in HTTP to send your data to the CGI are GET and POST. When the method is GET, the CGI program gets the input from the QUERY_STRING environment variable. When the method is POST, the CGI program gets the input from standard input (STDIN). In both cases, one has to parse the input to obtain the input argument name,value pairs.

CGI may or may not be complicated, but when you have a larger application with many features, you might have problems in testing, debugging, etc. This is true with all software projects. Debugging becomes problematic with CGIs. For example, when the method is GET, you have to set up environment variables QUERY_STRING and REQUEST_METHOD. When the method is POST, you must set up REQUEST_METHOD and CONTENT_LENGTH (number of bytes) to read from standard input (STDIN). Moreover, when your program crashes, it is not visible to your browser—you do not know what happened. The only message you get in this situation is the error report made by the web server.

You can use either of these methods (GET/POST) depending on your need. If you will be sending more data to CGI, use the POST method. When you have less data to be sent to CGI, use GET to put all the data inside the URL. For example, on one line, type:

<A HREF="/cgi/

With HTML FORMS (for POST method), the same would be

<FORM METHOD="post" ACTION="/cgi-bin/">
<INPUT TYPE=hidden name=fun value=DisplayFile>
<INPUT TYPE=hidden name=fileName>
We all know the difficulties of and have adopted different styles for debugging CGI programs. Our intention is to build CGI that does not work in the traditional way, but like other programs which work on the command line. This means you can test your CGI the way you test any other program on the command line. When it works on the command line, it is guaranteed to exhibit the same behavior on the Web.

The cgimodel Module

Let us see how we can make life easier with cgimodel, which lets you integrate your existing application in an elegant way without much hassle. Basically it consists of two modules: (see Listing 1) and (see Listing 2).

Listing 1.

Listing 2. is a wrapper to Python's CGI module. It also encapsulates reading from the command line, so there is no real difference in invoking from HTML FORMS or a URL or the command line.

The CollectArgs function in the module takes care of collecting arguments including name,value parameters from CGI or the command line. On the UNIX command line, you can supply the name,value parameters like this:

-name1 value1 -name2 value2

or like this:

name1 value1 name2 value2
The same is true for both URL and FORMS.

You do not have to modify anything in You just have to use it. The main section of cgimodel contains the following lines:

d = Dispatcher()
parDict = CollectArgs(parDict)
print mime_html
if not fun:
   print "usage: cgimodel -fun functionName"
      TraceIt(parDict) tries to call the function you have given as an argument to the parameter -fun.

When there is no such function available, it tells you the names of functions that can be called. If there is an exception (because of a syntax error, etc.) in the program, the exception will be traced back and reported. You can use this feature to e-mail the exception to yourself and make your CGI program more stable.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Anonymous's picture
Phospho.ELM: a database of phosphorylation sites—update 2008
Francesca Diella, Cathryn M. Gould, Claudia Chica, Allegra Via, and  Toby J. Gibson
Nucleic Acids Res. . 2008 January; 36(Database issue): D240–D244. Published online 2008 January. doi: 10.1093/nar/gkm772.
PMCID: PMC2238828

 Phospho.ELM: A database of experimentally verified phosphorylation sites in eukaryotic proteins
Francesca Diella, Scott Cameron, Christine Gemünd, Rune Linding, Allegra Via, Bernhard Kuster, Thomas Sicheritz-Pontén, Nikolaj Blom, and  Toby J Gibson
BMC Bioinformatics. . 2004; 5: 79. Published online 2004 June 22. doi: 10.1186/1471-2105-5-79.
PMCID: PMC449700

 SIRW: a web server for the Simple Indexing and Retrieval System that combines sequence motif searches with keyword searches
Chenna Ramu
Nucleic Acids Res. . 2003 July 1; 31(13): 3771–3774. 
PMCID: PMC168953

 ELM server: a new resource for investigating short functional sites in modular eukaryotic proteins
Pål Puntervoll, Rune Linding,,
Nucleic Acids Res. . 2003 July 1; 31(13): 3625–3630. 
PMCID: PMC168952

Gene2EST: a BLAST2 server for searching expressed sequence tag (EST) databases with eukaryotic gene-sized queries
Christine Gemünd, Chenna Ramu, Brigitte Altenberg-Greulich, and  Toby
 J. Gibson
Nucleic Acids Res. . 2001 March 15; 29(6): 1272–1277. 

The points is it solves the

Anonymous's picture

The points is it solves the problem of debugging cgi's,
since you can do it on the command line. But isn't
that unix/linux specific ? Will it work for windows?


Re: cgimodel: CGI Programming Made Easy with Python

Anonymous's picture

The author has done a very good job of designing a fantastic framework for cgi's. I am now writing cgi's with python!


One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix