Simple CGI Script in Python

From XPUB & Lens-Based wiki
Revision as of 16:52, 12 January 2011 by Michael Murtaugh (talk | contribs)

The Python documentation is a pretty good place to start: http://docs.python.org/library/cgi.html

print "Content-Type: text/html"     # HTML is following
print                               # blank line, end of headers

Show Headers

HTTP headers are simply environment variables. Use the os.environ dictionary to get at them.

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os

print "Content-type: text/html"
print

# show all headers as an HTML definition list

print "<dl>"
for key in os.environ:
    print "<dt>" + key + "</dt>"
    print "<dd>" + os.environ[key] + "</dd>"
print "</dl>"