Python CGI
Python CGI
TEN
CGI Scripting
10.1 The Python cgi Module
This section is a summary of the CGI material from lectures:
CGI scripts
Python CGI
• The Python cgi module handles both GET and POST requests
• The programmer doesn’t need to know if it is GET or POST
• It gives simple dictionary-like access to the fields:
• The key difference to using a regular dictionary is that you need to add .value to get the value attribute out
of the object returned by the dictionary lookup. The object is called a MiniFieldStorage, and it has attributes
to help if the field is a file or some other type of form field.
1 import cgi
2
3 fields = cgi.FieldStorage()
4
5 first = fields['first'].value
6 last = fields['last'].value
7
8 print """<html>
9 <body>
10 <h1>Hello %s %s</h1>
11 </body>
12 </html>""" % (first, last)
• Since POST requests use standard input, it means that you can only call FieldStorage once, otherwise all
of the POST data will have already been read.
Checking input
• Typically, if the fields are not specified you want to send back an error page which asks them to fill out the
necessary missing fields.
3 FIRST = 'first'
4
5 fields = cgi.FieldStorage()
6
7 first = 'NONE'
8 if FIRST in fields:
9 first = fields[FIRST].value
• Error messages (on standard error) are recorded in the web server log not sent to the browser
3 sys.stderr = sys.stdout
4 import cgitb
5 cgitb.enable()
• You should use this in any CGI script you are developing
• This and the previous technique should only be done for development systems – showing the error message
to the user is a bit of a security risk since it gives hackers more information about the program internals. It
is much better to have the errors go to a log file in a real system.
10.2 Walkthroughs
Note: Walkthrough 1 is critical for Stage 3 of the major project as it explains how to set up a CGI script on our
system. Please ask us if you have any problems getting it to work.
1. Firstly write the following program into a file hello.cgi in your public_html/cgi-bin directory:
1 #!/usr/bin/python
2 print 'Content-Type: text/html\r\n\r'
3
4 print """<html>
5 <head>
6 <title>Hello Web</title>
7 </head>
8 <body>
9 <h1>Hello Web!!</h1>
10 </body>
11 </html>"""
3. To see what is happening, you can run this program directly from the shell prompt which produces the
following HTML output
1 % ./hello.cgi
2 Content-Type: text/html
3
4 <html>
5 <head>
6 <title>Hello Web</title>
7 </head>
8 <body>
9 <h1>Hello Web!!</h1>
10 </body>
11 </html>
4. Now run your script through a web browser by going to the URL
http://gpu02.icrar.org/∼username/cgi-bin/hello.cgi
you should see a web page with the text Hello Web!!.
5. Note that the file suffix must be .cgi otherwise the web server may not run the script (depending on the
Apache configuration).
All CGI scripts must first print a header Content-Type: text/html informing the web browser what
type of content is being sent. text/html tells the browser that the data is a plain text file containing HTML.
Following the Content-Type header, there must be a blank line. In the example above, this is handled by an
empty print statement on line 3. Leaving this out is a common mistake.
1. Firstly we need to create a HTML page hello2.html including a form which will ask for the user’s name.
This needs to go into the public_html directory (not the cgi-bin directory). The HTML should look
something like this:
<html>
<head>
<title>Hello, who are you?</title>
</head>
<body>
<h1>Hello, who are you?</h1>
<p>
<form action="cgi-bin/hello2.cgi" method="GET">
Please enter your name:
<input type="text" name="name" width="20"><br/>
<input type="submit">
</form>
</p>
</body>
</html>
2. Put this page in your public_html directory and make it world readable chmod a+r hello.html.
3. Now we need to write the CGI script that is declared as the action property of the form element, in this
case hello2.cgi. This script retrieves the name from the CGI request and pastes it into the HTML output.
4. The code should look something like this, which you can download from the course website
1 #!/usr/bin/python
2 import cgi
3 NAMEFIELD = 'name'
4
5 fields = cgi.FieldStorage()
6
7 name = 'Anonymous'
8 if NAMEFIELD in fields:
9 name = fields[NAMEFIELD].value
10
5. Make sure this file is executable chmod a+rx hello2.cgi and copy it into your public_html/cgi-bin
directory.
6. Now point your browser at the HTML page and test the form
http://gpu02.icrar.org/∼username/hello2.html
The tail command with the -f is very useful because it sits there checking to see if anything has been added to
the end of the error.log file, and if so it prints it to the screen.
10.3 Exercises
These exercises combine what we have done previously in Python with the CGI module.
If you don’t know how to do any of these things once you have completed the lab, please come and ask us.