Chapter 2. CGI

Table of Contents

2.1. CGI scripts environment
2.2. A stupid one-purpose Web server
2.3. CGI scripts that do something useful
2.4. Debugging CGI scripts
2.5. Controls to perform in a CGI script
2.6. CGI for helicases

2.1. CGI scripts environment

Exercise 2.1. Installing a CGI program

Put the following code in a file called hello.py and install it in your cgi-bin directory (/usr/lib/cgi-bin/you).

#! /local/bin/python
print "Content-type: text/plain"
print
print "Hello, world"
	  
Check that your file has the appropriate permissions.

Once installed, try it with the proper url, e.g: http://leeloo.sis.pasteur.fr/cgi-bin/IEB/hello.py.

Exercise 2.2. A simple CGI program called from a form

We want to write a CGI script to handle the cgi/form1.html form seen during the HTML course. Put the following code in a file called form1.py and install it in your cgi-bin directory. This script uses te cgi Python module.

#! /local/bin/python
import cgi

# get input
query = cgi.parse()

# document returned
print "Content-type: text/html"
print
print '<html>\n'

print "<head>\n"
print "<title>Form1</title>\n"
print "</HEAD>\n"

print "<body>\n"
print "<p>seq_type: ", query['seq_type'], "</p>\n"
print "<p>seq_name: ", query['seq_name'], "</p>\n"
print "<p>seq: ", query['seq'], "</p>\n";
print "</body>\n"

print "</html>\n"
	  
Copy the form in your own documents directory on the Web server and click on submit.

Exercise 2.3. MIME type

What's wrong with the following CGI (saved it in hello_html.py):

#! /local/bin/python
print "Content-type: text/plain"
print
print "<html>\n"

print "<head>\n"
print "<title>Hello, world</title>\n"
print "</head>\n"

print "<body>\n"
print "<h1>Hello, world</h1>\n"
print "Hello, world"
print "</body>\n"

print "</html>\n"
	  

Exercise 2.4. CGI environment

Write a CGI, print_env.py, that prints some of the CGI environment variables:

  • PATH
  • REMOTE_ADDR
  • CONTENT_LENGTH
  • REQUEST_URI
  • QUERY_STRING

Try it: http://leeloo.sis.pasteur.fr/cgi-bin/IEB/print_env.py. How do you specify something to get it in the QUERY_STRING environment variable?

Then write another variant that prints all the environment variables. Try it: http://leeloo.sis.pasteur.fr/cgi-bin/IEB/print_all_env.py.