Program to display CGI environment variables : Introduction « CGI Web « Python Tutorial






#!c:\Python\python.exe
import os
import cgi

def printHeader( title ):
   print """Content-type: text/html

<?xml version = "1.0" encoding = "UTF-8"?>    
<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title>%s</title></head>
<body>""" % title

rowNumber = 0
backgroundColor = "white"

printHeader( "Environment Variables" )
print """<table style = "border: 0">"""

# print table of cgi variables and values
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,
      cgi.escape( item ), cgi.escape( os.environ[ item ] ) )

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








22.1.Introduction
22.1.1.A Simple CGI Script
22.1.2.CGI Environment
22.1.3.A Script for Displaying the Environment
22.1.4.Your First Python CGI Script: Hello Apache
22.1.5.Programming Web Services
22.1.6.Processing Parameters Passed to CGI Scripts
22.1.7.Displays the current date and time in a Web browser.
22.1.8.Program to display CGI environment variables
22.1.9.Output current time
22.1.10.escape strings
22.1.11.Output html list
22.1.12.print web page content