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 cgidisp Module

The other module,, is the one in which you have to modify or insert an instance to the class Dispatcher for your application using one argument, namely parDict. For example, under class Dispatcher, if you define a method like

def cmd_myHello(self,parDict):
print "<H1>Hello</H1>"

then this function is immediately available to the outside world. You can call it on the command line this way: -fun myHello
with URL (GET method)
and with HTML forms as
<FORM METHOD="post" ACTION="/cgi-bin/>
<INPUT TYPE=hidden name=fun value=myHello>
It's that easy!

The dispatch method under the class Dispatcher is called from with one argument. This argument is the name of the function to be executed. Here is the interesting part. After prefixing the function name with the “cmd_” string, the dispatch method checks to see if such a function is available with hassattr. The Dispatcher maps the command to the function and executes it. This way, you do not have to use a lookup table to keep track of available functions. The additional overhead of adding a new command to the new function is not there; you just have to write the function and call it through the command line. The functionality is already there. This kind of pattern is possible with Python, since it is a highly dynamic language.

Please note that when calling the method, we are not using the prefix cmd_ of the method. This is explained later.

The main section of the Dispatcher class contains the following:

class Dispatcher:
   def __init__(self):
      self.debug = None
   def dispatch(self, command,args=None):
      mname = 'cmd_' + command
      if hasattr(self, mname):
      method = getattr(self, mname)
         if not args:
            return method()
            return method(args)
         print "<PRE>" self.error(command)<\n>
         print "</PRE>"
   def cmd_Hello(self,parDict):
      print " Hello World !"
   def cmd_ShowDict(self):
      print "<PRE></H1>Debug Info:</H1><HR>"
      for k,v in parDict.items():
            print "%-30s :  %s " %(k,v)
      print "</PRE>"
   def error(self,s):
      print " #<B>Error<B>: <BB>Function ( %s ) not available\n " %s

All your parameters are available in the parDict dictionary whether they are input from URL, FORM or command line—there is no difference. You can check for their existence in this way:

if parDict['param']:
   print " yes ", parDict['param']
   print " No "
The None object is returned when there is no parameter, i.e., when you try to access an unspecified parameter.

The instances inside the class Dispatcher are of two types: those that are prefixed by the “cmd_” string are qualified for calling from outside; internal instances are not visible outside. For example, the error instance cannot be called from CGI, but the instances cmd_Hello and cmd_ShowDict can be called. This convention is made to differentiate between the instances that are for internal (used inside the class Dispatcher) and external (by cgimodel/cgidisp) use.

So, add a “cmd_” prefix to the instances you want to use with CGI. For example, cmd_TopPage can be called with -fun TopPage

on the command line and
will be the corresponding URL. The -fun is mandatory. This way, you can indicate which function you want to call. Obviously, you can have as many functions as you want, and they are CGI-ready. This is the exact requirement of larger CGI projects.

A couple of functions come with the module for free. The function DisplayFile displays colorized Python source code on the Web. This one relies on the module, available with the standard Python distribution. -fun DisplayFile -fileName

URL equivalent:
Note the name=value and the & to separate the name,value pairs—the traditional method of specification for CGI.

The method cmd_ShowDict shows all dictionary items in the parDict dictionary and is useful for checking whether you have supplied the correct parameters.



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!


Geek Guide
The DevOps Toolbox

Tools and Technologies for Scale and Reliability
by Linux Journal Editor Bill Childers

Get your free copy today

Sponsored by IBM

Upcoming Webinar
8 Signs You're Beyond Cron

Scheduling Crontabs With an Enterprise Scheduler
11am CDT, April 29th
Moderated by Linux Journal Contributor Mike Diehl

Sign up now

Sponsored by Skybot