cgimodel: CGI Programming Made Easy with Python
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.
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/cgimodel.py?fun=DisplayFile&fileName= cgimodel.pycgimodel">cgimodel</A>
With HTML FORMS (for POST method), the same would be
<FORM METHOD="post" ACTION="/cgi-bin/cgimodel.py"> <INPUT TYPE=hidden name=fun value=DisplayFile> <INPUT TYPE=hidden name=fileName value=cgimodel.py> <INPUT TYPE=SUBMIT VALUE="cgimodel"> </FORM>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.
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: cgimodel.py (see Listing 1) and cgidisp.py (see Listing 2).
cgimodel.py 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 cgimodel.py 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 value2The same is true for both URL and FORMS.
You do not have to modify anything in cgimodel.py. You just have to use it. The main section of cgimodel contains the following lines:
d = Dispatcher()
parDict = CollectArgs(parDict)
print mime_html
fun=parDict['fun']
if not fun:
print "usage: cgimodel -fun functionName"
d.ShowAvailableFunc()
TraceIt(parDict)
else:
try:
d.dispatch(fun,parDict)
except:
TraceIt(parDict)
cgimodel.py 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.
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 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Build a Skype Server for Your Home Phone System
- Why Python?
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Validate an E-Mail Address with PHP, the Right Way
- Tech Tip: Really Simple HTTP Server with Python
- Understanding the Linux Kernel
1 hour 26 min ago - General
3 hours 56 min ago - Kernel Problem
13 hours 59 min ago - BASH script to log IPs on public web server
18 hours 26 min ago - DynDNS
22 hours 1 min ago - Reply to comment | Linux Journal
22 hours 34 min ago - All the articles you talked
1 day 57 min ago - All the articles you talked
1 day 1 hour ago - All the articles you talked
1 day 1 hour ago - myip
1 day 5 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!
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