cgi
When a WWW server finds that a URL contains a reference to a file in a
particular subdirectory (usually /cgibin
), it runs the file as
a subprocess. Information about the request such as the full URL, the
originating host etc., is passed to the subprocess in the shell
environment; additional input from the client may be read from
standard input. Standard output from the subprocess is sent back
across the network to the client as the response from the request.
The CGI protocol describes what the environment variables passed to
the subprocess mean and how the output should be formatted. The
official reference documentation for the CGI protocol can be found on
the World-Wide Web at
<URL:http://hoohoo.ncsa.uiuc.edu/cgi/overview.html>
. The
cgi
module was based on version 1.1 of the protocol and should
also work with version 1.0.
The cgi
module defines several classes that make it easy to
access the information passed to the subprocess from a Python script;
in particular, it knows how to parse the input sent by an HTML
``form'' using either a POST or a GET request (these are alternatives
for submitting forms in the HTTP protocol).
The formatting of the output is so trivial that no additional support is needed. All you need to do is print a minimal set of MIME headers describing the output format, followed by a blank line and your actual output. E.g. if you want to generate HTML, your script could start as follows:
# Header -- one or more lines:
print "Content-type: text/html"
# Blank line separating header from body:
print
# Body, in HTML format:
print "<TITLE>The Amazing SPAM Homepage!</TITLE>"
# etc...
The server will add some header lines of its own, but it won't touch
the output following the header.
The cgi
module defines the following functions:
urllib.unquote()
. As a side effect, this function sets
environ['QUERY_STRING']
to the raw query string, if it isn't
already set.
FormContentDict
class defined
below, or a subclass thereof).
This is mainly useful when debugging a CGI script.
&
'' is replaced with ``&
'',
``<
'' is replaced with ``<
'', and ``>
'' is
replaced with ``>
''. This is useful when printing (almost)
arbitrary text in an HTML context. Note that for inclusion in quoted
tag attributes (e.g. <A HREF="...">
), some additional
characters would have to be converted --- in particular the string
quote. There is currently no function that does this.
parse()
, at most one instance of
at most one of these classes should be created per script invocation:
parse()
(i.e. each
field name maps to a list of values). Additionally, it initializes
its data member query_string
to the raw query sent from the
server.
FormContentDict
, is a little more
user-friendly when you are expecting that each field name is only used
once in the form. When you access for a particular field (using
form[fieldname]
), it will return the string value of that item
if it is unique, or raise IndexError
if the field was specified
more than once in the form. (If the field wasn't specified at all,
KeyError
is raised.) To access fields that are specified
multiple times, use form.getlist(fieldname)
. The
values()
and items()
methods return mixed lists ---
containing strings for singly-defined fields, and lists of strings for
multiply-defined fields.
The module defines the following variable: