CGI Resources

CGI script help

Writing your CGI script

The biggest problem with this practical is getting your CGI script in the right place and getting it to work properly. Here are some hints:

For security reasons a wrapper has been provided but this makes the process of setting up a directory more complex. Get help from a demonstrator!

On pandora, your HTML files go in the WWW subdirectory of your home directory, while CGI scripts go in WWW/cgi-bin/

When you write a CGI script, ensure it is executable:

chmod a+x ./cgiscript.py

then run it by hand with:

./cgiscript.py

(like any other Python script) to ensure that it is free of syntax errors and to check that it is executable.

Do not run the script with

python cgiscript.py

It will save you problems if you write the script using an editor such as nedit, emacs or vi on the Linux machines rather than writing the script in something like Notepad under Windows and then moving the script to the Linux machine. This is because when you transfer a file from Windows to Linux, you may end up with the Windows line-termination characters which will confuse Linux. If you get an error like 'program not found' even when you run the script by hand with ./cgiscript.pl, this is likely to be the problem. The shell will be reading the name of the Python interpreter from the first line of the script, but will see the Windows return character and think that is part of the name of the Python interpreter. If this is your problem, then do the following:

perl -pi -e 's/\r//g;' cgiscript.py

(replace cgiscript.py with whatever you have called your Python CGI script!) This command uses perl's -p flag to loop around the file automatically printing each line; the -i option to change the file in-place and the -e option to specify some code to run (which removes MSDOS return characters).

Overall structure of Python script

The CGI module is accessed by using the Python code:

import cgi

We then need to output an HTML MIME-TYPE header followed by a blank line:

print ("Content-Type: text/html\n")

(The \n prints the blank line.) If the header is not printed, the web browser will not know the type of data which follows (HTML in this case).

The following example code prints the standard header and then prints some HTML:

#!/usr/bin/env python3
print ("Content-Type: text/html\n")
print ('''
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
''')

[Run script]

Extracting data from a form

The following code sample is used to extract a value from a form on a web page. First you create a dictionary using the cgi.FieldStorage() method, then the index to the dictionary is the name= attribute given to a form element. Code like this is used to extract values when the name is unique and only one value is associated with a given name attribute.

#!/usr/bin/env python3

import cgi

# Print the HTML MIME-TYPE header
print ("Content-Type: text/html\n")

# Now grab the content of the form
form = cgi.FieldStorage()

# ...and grab some of the form elements
name    = form["name"].value
seq     = form["sequence"].value
seqtype = form.getvalue("seqtype")  # Alternative way of doing the same thing

Extracting multiple values

Checkboxes and selection lists can return multiple values for the same name. In this case, you can use:

#!/usr/bin/env python3

import cgi

# Print the HTML MIME-TYPE header
print ("Content-Type: text/html\n")

# Now grab the content of the form
form = cgi.FieldStorage()

antype  = form.getlist("antype")

You can then access the elements of the list in the usual way:

for a in antype:
   ...Do comething with a...

Debugging information

Adding the following lines to your code allows you to obtain additional debugging information to the browser:

#!/usr/bin/env python3

import cgi

# Useful debugging output
import cgitb

# Send errors to browser
cgitb.enable()  

Alternatively, errors can be sent to a log file:

# Send errors to log file
cgitb.enable(display=0, logdir="/path/to/logdir")
Continue