What is CGI?

CGI Scripting is a type of server side programming that powers a lot of the useful sites on the web. CGI Scripts power search engines, online shopping sites, and many if not all news sites, discussion forums, blogs, etc.

Server Side means that the code runs on a remote server, and the only the output is sent to the client. So in the case of a google search, I first go to their homepage:

Google's pages are mostly simple HTML markup, but there are a few special statements that create a "Form", that is responsible for displaying the text box that you type your query into, and the buttons that you can press to begin your search. Some of the code that generates this form is displayed below. Do you see the line where "name=q" is? That's the text box that you type into. The buttons are the pretty obvious last couple lines. The really important one is hiding at the top:

< form action='/search' name=f>
Action essentially tells the form what script to submit it's input to, so this is really key if you want a CGI script that interacts with a user.


< form action="/search" name=f>
< input type=hidden name=oe value="UTF-8">
< input maxLength=256 size=55 name=q value="">
< input type=submit value="Google Search" name=btnG>
< input type=submit value="I'm Feeling Lucky" name=btnI>
          

Now when you click the button on that form to submit your search string, a CGI script (which I don't have access to so I can't show you), takes that string and runs the search. Then from your search results, the CGI script on the server takes those results and formats them as HTML, and outputs them to you.

Now not all CGI scripts are like this, some are run everytime you access a page, without you pressing any buttons or inputing any data. We'll have an example or two in a little bit. For now I hope that this helps give those of you who are looking into CGI scripting for the first time some idea of what you'll be trying to accomplish with your scripts.

A (very) simple script:

First let's take a very quick look at test.py:
Hello World!
At first you won't see many differences between this and any other python code that we've written. In fact you might say that this looks like just a simple "Hello World" Program, and you'd be right. The important differences between code that you run at the command line and CGI scripts like this one is that the #!("pound-bang") hack is required in CGI's, and that all your output must be valid HTML.

Initial Content

Before printing anything else in your CGI script you should be certain to print the following "special" lines:
Hello World!
These lines send the correct header information to your clients web browser. Without those lines the web browser isn't sure what to do with the output of your CGI script.

Debugging and Coding Tips

To simplify debugging there are several things you can do. Old school CGI scriptors, especially from the Perl camp we'll suggest that you do something like this:
Hello World!
Another thing worth doing is including the following import statements:
Hello World!
Both of the above are ways to get errors that occur during your script to be properly formatted and outputed to the browser for testing. The cgitb module is more advanced and probably the more Python-y way to do things, I highly recommend it.

One last thing to keep in mind:Always run your script at the commandline before trying it in your browser! If your script has any major syntax errors in it(incorrect indentation, missing modules and misspellings) it will simply refuse to run and you will see:

500: Internal Server Error

... rather then the friendly and exciting CGI script output you were expecting. cgitb and other techniques will handle runtime errors, but not syntax errors, those you have to deal with before cgitb can be of any help.

So what errors will I actually see? Well, you won't see syntax errors, so you won't see something like this:

Hello World!

But this will show up in your browser as an error(in cgitb or whatever):

Hello World!

A "no-input" example

This CGI script just prints the current time (on the server) that you asked for the page.

Here's the code:

Hello World!

Click here to test out wittime.py

A Game: Rock Paper Scissors Spock Lizard

Rock Paper Scissors Spock Lizard is a variation on the classic game or Rock Paper Scissors. The addition of two new "signs" makes the game a little more interesting and a little harder to predict. The code is not the most exciting in the world, and it's pretty repetitive, so I'm including links to just download and check the code out at your leisure.

The Required HTML input page
The python code / CGI Script

Try playing the game here: http://www.penzilla.net/tutorials/python/cgi/rpssl.html

Sorting IP Addresses

I wrote the following program to help out a friend who wanted to look at his web traffic a bit. He had a number of php hit counters that stored the ip address of anyone who visited his site. He was interested in things like who visited his site most and things like that.

The following simple script grabs these hits[1-8].txt files and read's all the ip addresses from them. It then sorts the IP addresses and counts the number of times each address occurs.

It is designed to run as a CGI to let you check this information from your browser.

Hello World!