Web server programs
CGI programs are executed by a web server program in response to a user request. Examples of web server programs include Apache ( http://www.apache.org/ ) which runs on all major operating systems and Internet Information Server (or IIS) which runs on Microsoft Windows only. Apache is the most popular web server by a very large margin, running 63.22% of over 14 million active Internet domains, with IIS running 26.14% of active domains (figures from http://www.netcraft.com/survey for Feb 2002).
Web server programs also serve static HTML format web pages. When the web server detects that a requested URL (Uniform Resource Locator) is a CGI program , then instead of sending the program file as text to the web browser, it loads and runs the program, supplying input if there is any (see below) and sending the output of the program to the browser which requested the URL. The web server will implement a set of rules to decide whether or not a requested file is a CGI program, e.g. from it having a file extension of .pl or .cgi and it being installed in a particular folder or directory, e.g. one named cgi-bin .
CGI program input
A CGI program can obtain input from part of a URL after a ? mark e.g:
http://copsewood.net/python/formfields.cgi?user=Brian&this=that
This URL can be input by the user, linked from a web page, or generated automatically.
A CGI can also obtain similar input as posted from a user submitted HTML form to the program URL. Here is an example <FORM> tag which specifies a remote CGI program which will process and handle the submitted form data:
<form action="../cgi-bin/responseform.cgi" method="post">
This URL is relative to the location of the static HTML page containing the form, but absolute URLs will work just as well. The CGI program doesn't have to be on the same web site as the form that provides it with input, though it often will be.
CGI program output
In the simplest case, CGI programs can start with a suitable HTTP (HyperText Transport Protocol) header and send their output as plain text. This CGI program is about as simple as they get:
#!C:/python21/python # Simplest Python CGI Program print "Content-type: text/plain\n" print "hello CGI world!"
When installed as python/simple.cgi relative to the site root (in this case http://127.0.0.1/ ) it displays the plain text: hello CGI world! in the browser.
There are 3 active lines of code in this program.
#!C:/python21/python
Tells a (Windows based) Apache server to interpret the rest of the script using a python interpreter installed in c:\python21\python (note use of Unix/Internet style forward slashes to delimit the path) . The #! characters must be the first 2 characters in the script file, so that the web-server program interprets the rest of the first line as an interpreter path. This first line of the program isn't Python code: it tells the web-server that the rest of the program is Python code. On a Unix type system this line might be #!/usr/bin/python , or the path of the Python interpreter.
print "Content-type: text/plain\n"
This outputs the HTTP header stating that the rest of the output will be plain text. Normally we will be using "Content-type: text/html\n" in order to send formatted HTML output. The HTTP standard expects 2 newlines after HTTP headers (most of the time you only need the one), so we have to specify an extra \n in addition to the one Python print outputs by default.
print "hello CGI world!"
The output of this print statement goes straight to the browser and is displayed directly in the browser window.
Of course for something as simple as this you would probably prefer to use a static file rather than a Python CGI program. We can also get more attractive formatting by making the CGI program create its output in HTML format.
The reason for using CGI programs instead of static HTML files is to generate the information sent to the browser when the request is made by the user using data current at that time. Our next example tells the user the local time at the server at the time the request is serviced.
#!/usr/local/bin/python # Change top line to reflect where Python is installed on your system # Python local time CGI Program import time print "Content-type: text/html\n" print "<html><Head><Title>Hello Python CGI World</Title></Head>" print "<Body><H1>Hello Python CGI World !</H1>" to=time.localtime(time.time()) # time.time() parameter not needed Python >= 2.1 print "<p>The local time on this server is: %s </p>" % time.asctime(to) print "</body></html>"
Pointing our browser at the URL for this CGI gave the output:
The local time on this server is: Mon Mar 18 08:11:38 2002
Requesting the same URL a few seconds later gave an updated time.
The Python cgi module will parse the usual kinds of input to CGI programs. This works for additional information passed within the URL and for form data.
The URL encoding approach is useful where the URL itself is auto generated or for test purposes. For example a coded URL can be sent as part of a CGI generated web page or email. The recipient can confirm subscription to a mailing list simply by clicking on a link within this message. If using an email this can contain a subscription confirmation request with a URL containing a special randomly generated code which is sent to the email address requested, so if the confirmation-required message is suitably worded and the user clicks on the generated confirmation link this establishes:
a. That the person reading email at the address receiving this URL is consenting to join the mailing list.
b. That the person who provided this address to the server using a web form or other means is likely either to be the owner of this email address or to be acting upon their request.
The additional information part of the URL is much used in search engine queries and appears after a question mark (?) within the URL, so this is sometimes called a "query string". This consists of name=value pairs separated from each other using ampersands (&).
The following program: inputfields.cgi uses the FieldStorage class within the cgi module.
#!/usr/local/bin/python # Python CGI Program to get URL or Form data import cgi input=cgi.FieldStorage() print "Content-type: text/html\n" print "<html><Head><Title>CGI Input Fields</Title></Head>" print "<Body><H1> CGI Input Fields</H1>" print "<ul>" for key in input.keys(): print "<li>%s: %s</li>" % (key,input[key].value) print "</ul>" print "</body></html>"
This program is simple and works well enough for now, but we will need to develop it further to handle the situation where a key is used to access more than one value, which can happen with multiple selection form dialog boxes. Note that we need to use the value attribute of the object stored in the dictionary in which the cgi.FieldStorage class stores the form response. This value attribute will be a string if only one value for the relevant key was available.
When called using the URL:
http://copsewood.net/pytest/inputfields.cgi?user=Eric&address=Eric@python.org
This program displayed the following output as a HTML list:
CGI Form Fields
The same CGI program can be made to give the same output by posting the data using an HTML form. This approach is easier than hand-crafting URLs for applications requiring the end user to input the data to send to the CGI program.
Here is the form: