Introduction to CGI Programming
Python has many uses on the Web. Modules cgi (for access to XHTML forms), Cookie (to read and write cookies), smtplib (to manipulate SMTP messages), urllib (to ma-nipulate Web data), ftplib (to perform client-side FTP tasks) and others provide power-ful extensions that Web programmers can use to write CGI scripts quickly for almost any task. This section introduces Python CGI programming. Sections 28.7–28.9 present more detailed CGI applications. We assume that the reader has installed and configured the Apache Web server. Apache does not usually need any special configuration to run a Py-thon script; a script need merely be placed in the specified cgi-bin directory.
Figure 28.16 gathers all CGI environment variables and values and organizes them in an XHTML table that is displayed in a Web browser. Line 1
is a directive (sometimes called the pound-bang or Shebang) that provides the server with the location of the Python executable. This directive must be the first line in a CGI script. For UNIX-based machines, this value might commonly be
#!/usr/bin/python or #!/usr/local/bin/python
depending on the actual location of the Python executable.
# Fig 28.16: fig28_16.py
# Program to display CGI environment variables
print "Content-type: text/html"
print """<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"
<body><table style = "border: 0">"""
rowNumber = 0
for item in os.environ.keys():
rowNumber += 1
if rowNumber % 2 == 0:
backgroundColor = "white"
backgroundColor = "lightgrey"
print """<tr style = "background-color: %s">
% ( backgroundColor, item,
cgi.escape( os.environ[ item ] ) )
Fig. 28.16 Displaying environment variables
Line 6 imports the cgi module that provides functionalities for writing CGI scripts. In this example, we use the module to format output; in later examples, we use module cgi to perform more complex CGI tasks.
Lines 8–9 print a valid HTTP header. Browsers use HTTP headers to determine how to handle the incoming data, and a valid header must be sent to ensure that the browser dis-plays the information correctly. The blank line below the header is required; without this line, the content will not be delivered properly to the client. Lines 11–13 print the XHTML DOCTYPE string to the browser.
The environ data member (line 23) of module os holds all the environment vari-ables. This data member acts like a dictionary; therefore, we can access its keys via the keys method and its values via the  operator. In lines 23–34, we print a new row in the table for each item returned by method os.environment.keys. This row contains the key and the key’s value. Notice that we pass each environment variable to function cgi.escape. This function formats text in an “XHTML-safe” way—special XHTML characters such as < and & are formatted so that they appear in the document as they should. After we have printed all the environment variables, we close the table, body and html tags (line 36).
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.