Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Introduction to CGI Programming - Python

Chapter: Internet & World Wide Web HOW TO PROGRAM - Python

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Introduction to CGI Programming - Python

This section introduces Python CGI programming.

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

#!c:\Python\python.exe

 

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.

 

    #!c:\Python\python.exe

    # Fig 28.16: fig28_16.py

    # Program to display CGI environment variables

 

    import os

    import cgi

 

    print "Content-type: text/html"

    print

      print """<!DOCTYPE html PUBLIC

      "-//W3C//DTD XHTML 1.0 Transitional//EN"

      "DTD/xhtml1-transitional.dtd">"""

        print """

      <html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en"

      lang="en">

      <head><title>Environment Variables</title></head>

      <body><table style = "border: 0">"""

      rowNumber = 0

 

      for item in os.environ.keys():

      rowNumber += 1

      if rowNumber % 2 == 0:

      backgroundColor = "white"

      else:

      backgroundColor = "lightgrey"

 

      print """<tr style = "background-color: %s">

<td>%s</td><td>%s</td></tr>""" \

% ( backgroundColor, item,

cgi.escape( os.environ[ item ] ) )

 

print """</table></body></html>"""

 

 

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).


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.