blob: a5d1cdf42ee5975f5a03d36447f7c738090d5cdd [file] [log] [blame]
Guido van Rossuma12ef941995-02-27 17:53:25 +00001\section{Built-in module \sectcode{cgi}}
2\stmodindex{cgi}
3\indexii{WWW}{server}
4\indexii{CGI}{protocol}
5\indexii{HTTP}{protocol}
6\indexii{MIME}{headers}
7\index{URL}
8
Guido van Rossum86751151995-02-28 17:14:32 +00009\renewcommand{\indexsubitem}{(in module cgi)}
10
Guido van Rossuma12ef941995-02-27 17:53:25 +000011This module makes it easy to write Python scripts that run in a WWW
12server using the Common Gateway Interface. It was written by Michael
13McLay and subsequently modified by Steve Majewski and Guido van
14Rossum.
15
16When a WWW server finds that a URL contains a reference to a file in a
17particular subdirectory (usually \code{/cgibin}), it runs the file as
18a subprocess. Information about the request such as the full URL, the
19originating host etc., is passed to the subprocess in the shell
20environment; additional input from the client may be read from
21standard input. Standard output from the subprocess is sent back
22across the network to the client as the response from the request.
23The CGI protocol describes what the environment variables passed to
24the subprocess mean and how the output should be formatted. The
25official reference documentation for the CGI protocol can be found on
26the World-Wide Web at
27\code{<URL:http://hoohoo.ncsa.uiuc.edu/cgi/overview.html>}. The
28\code{cgi} module was based on version 1.1 of the protocol and should
29also work with version 1.0.
30
31The \code{cgi} module defines several classes that make it easy to
32access the information passed to the subprocess from a Python script;
33in particular, it knows how to parse the input sent by an HTML
34``form'' using either a POST or a GET request (these are alternatives
35for submitting forms in the HTTP protocol).
36
37The formatting of the output is so trivial that no additional support
38is needed. All you need to do is print a minimal set of MIME headers
39describing the output format, followed by a blank line and your actual
40output. E.g. if you want to generate HTML, your script could start as
41follows:
42
43\begin{verbatim}
44# Header -- one or more lines:
45print "Content-type: text/html"
46# Blank line separating header from body:
47print
48# Body, in HTML format:
49print "<TITLE>The Amazing SPAM Homepage!</TITLE>"
50# etc...
51\end{verbatim}
52
53The server will add some header lines of its own, but it won't touch
54the output following the header.
55
56The \code{cgi} module defines the following functions:
57
58\begin{funcdesc}{parse}{}
59Read and parse the form submitted to the script and return a
60dictionary containing the form's fields. This should be called at
61most once per script invocation, as it may consume standard input (if
62the form was submitted through a POST request). The keys in the
63resulting dictionary are the field names used in the submission; the
64values are {\em lists} of the field values (since field name may be
65used multiple times in a single form). As a side effect, it sets
66\code{environ['QUERY_STRING']} to the raw query string, if it isn't
67already set.
68\end{funcdesc}
69
70\begin{funcdesc}{print_environ_usage}{}
71Print a piece of HTML listing the environment variables that may be
72set by the CGI protocol.
73This is mainly useful when learning about writing CGI scripts.
74\end{funcdesc}
75
76\begin{funcdesc}{print_environ}{}
77Print a piece of HTML text showing the entire contents of the shell
78environment. This is mainly useful when debugging a CGI script.
79\end{funcdesc}
80
81\begin{funcdesc}{print_form}{form}
82Print a piece of HTML text showing the contents of the \var{form}.
83This is mainly useful when debugging a CGI script.
84\end{funcdesc}
85
86\begin{funcdesc}{escape}{string}
87Convert special characters in \var{string} to HTML escapes. In
88particular, ``\code{\&}'' is replaced with ``\code{\&amp;}'',
89``\code{<}'' is replaced with ``\code{\&lt;}'', and ``\code{>}'' is
90replaced with ``\code{\&gt;}''. This is useful when printing (almost)
91arbitrary text in an HTML context. Note that for inclusion in quoted
92tag attributes (e.g. \code{<A HREF="...">}), some additional
93characters would have to be converted --- in particular the string
94quote. There is currently no function that does this.
95\end{funcdesc}
96
97The module defines the following classes. Since the base class
98initializes itself by calling \code{parse()}, at most one instance of
99at most one of these classes should be created per script invocation:
100
101\begin{funcdesc}{FormContentDict}{}
102This class behaves like a (read-only) dictionary and has the same keys
103and values as the dictionary returned by \code{parse()} (i.e. each
104field name maps to a list of values). Additionally, it initializes
105its data member \code{query_string} to the raw query sent from the
106server.
107\end{funcdesc}
108
109\begin{funcdesc}{SvFormContentDict}{}
110This class, derived from \code{FormContentDict}, is a little more
111user-friendly when you are expecting that each field name is only used
112once in the form. When you access for a particular field (using
113\code{form[fieldname]}), it will return the string value of that item
114if it is unique, or raise \code{IndexError} if the field was specified
115more than once in the form. (If the field wasn't specified at all,
116\code{KeyError} is raised.) To access fields that are specified
117multiple times, use \code{form.getlist(fieldname)}. The
Guido van Rossum86751151995-02-28 17:14:32 +0000118\code{values()} and \code{items()} methods return mixed lists ---
Guido van Rossuma12ef941995-02-27 17:53:25 +0000119containing strings for singly-defined fields, and lists of strings for
120multiply-defined fields.
121\end{funcdesc}
122
123(It currently defines some more classes, but these are experimental
124and/or obsolescent, and are thus not documented --- see the source for
125more informations.)
126
127The module defines the following variable:
128
129\begin{datadesc}{environ}
130The shell environment, exactly as received from the http server. See
131the CGI documentation for a description of the various fields.
132\end{datadesc}