cgimodel: CGI Programming Made Easy with Python
The other module, cgidisp.py, 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:
cgimodel.py -fun myHellowith URL (GET method)
cgimodel.py?-fun=myHelloand with HTML forms as
<FORM METHOD="post" ACTION="/cgi-bin/cgimodel.py> <INPUT TYPE=hidden name=fun value=myHello> <INPUT TYPE=SUBMIT VALUE="Say Hello"> </FORM>It's that easy!
The dispatch method under the class Dispatcher is called from cgimodel.py 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()
else:
return method(args)
else:
print "<PRE>" self.error(command)<\n>
self.ShowAvailableFunc()
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
return
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'] else: 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
cgimodel.py -fun TopPage
on the command line and
cgimodel.py?-fun=TopPagewill 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 py2html.py, available with the standard Python distribution.
cgimodel.py -fun DisplayFile -fileName cgimodel.py
URL equivalent:
cgimodel.py?-fun=DisplayFile&fileName=cgimodel.pyNote 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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Reply to comment | Linux Journal
5 hours 51 min ago - Reply to comment | Linux Journal
6 hours 7 min ago - Favorite (and easily brute-forced) pw's
7 hours 58 min ago - Have you tried Boxen? It's a
13 hours 50 min ago - seo services in india
18 hours 22 min ago - For KDE install kio-mtp
18 hours 22 min ago - Evernote is much more...
20 hours 23 min ago - Reply to comment | Linux Journal
1 day 5 hours ago - Dynamic DNS
1 day 5 hours ago - Reply to comment | Linux Journal
1 day 6 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Citation!
The points is it solves the
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?
jens
Re: cgimodel: CGI Programming Made Easy with Python
The author has done a very good job of designing a fantastic framework for cgi's. I am now writing cgi's with python!
peter