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