@@ -45,7 +45,7 @@ \subsection{Introduction}
4545generate a minimal header section looks like this:
4646
4747\begin {verbatim }
48- print "Content-type : text/html" # HTML is following
48+ print "Content-Type : text/html" # HTML is following
4949print # blank line, end of headers
5050\end {verbatim }
5151
@@ -59,9 +59,6 @@ \subsection{Introduction}
5959print "Hello, world!"
6060\end {verbatim }
6161
62- (It may not be fully legal HTML according to the letter of the
63- standard, but any browser will understand it.)
64-
6562\subsection {Using the cgi module }
6663\nodename {Using the cgi module}
6764
@@ -77,61 +74,69 @@ \subsection{Using the cgi module}
7774standard). Since it may consume standard input, it should be
7875instantiated only once.
7976
80- The \class {FieldStorage} instance can be accessed as if it were a Python
81- dictionary. For instance, the following code (which assumes that the
82- \code {content-type} header and blank line have already been printed)
77+ The \class {FieldStorage} instance can be indexed like a Python
78+ dictionary, and also supports the standard dictionary methods
79+ \function {has_key()} and \function {keys()}.
80+ Form fields containing empty strings are ignored
81+ and do not appear in the dictionary; to keep such values, provide
82+ the optional \samp {keep_blank_values} argument when creating the
83+ \class {FieldStorage} instance.
84+
85+ For instance, the following code (which assumes that the
86+ \code {Content-Type} header and blank line have already been printed)
8387checks that the fields \code {name} and \code {addr} are both set to a
8488non-empty string:
8589
8690\begin {verbatim }
8791form = cgi.FieldStorage()
8892form_ok = 0
8993if form.has_key("name") and form.has_key("addr"):
90- if form["name"].value != "" and form["addr"].value != "":
91- form_ok = 1
94+ form_ok = 1
9295if not form_ok:
9396 print "<H1>Error</H1>"
9497 print "Please fill in the name and addr fields."
9598 return
99+ print "<p>name:", form["name"].value
100+ print "<p>addr:", form["addr"].value
96101...further form processing here...
97102\end {verbatim }
98103
99104Here the fields, accessed through \samp {form[\var {key}]}, are
100105themselves instances of \class {FieldStorage} (or
101106\class {MiniFieldStorage}, depending on the form encoding).
107+ The \member {value} attribute of the instance yields the string value
108+ of the field. The \function {getvalue()} method returns this string value
109+ directly; it also accepts an optional second argument as a default to
110+ return if the requested key is not present.
102111
103112If the submitted form data contains more than one field with the same
104113name, the object retrieved by \samp {form[\var {key}]} is not a
105114\class {FieldStorage} or \class {MiniFieldStorage}
106- instance but a list of such instances. If you expect this possibility
115+ instance but a list of such instances. Similarly, in this situation,
116+ \samp {form.getvalue(\var {key})} would return a list of strings.
117+ If you expect this possibility
107118(i.e., when your HTML form contains multiple fields with the same
108119name), use the \function {type()} function to determine whether you
109120have a single instance or a list of instances. For example, here's
110121code that concatenates any number of username fields, separated by
111122commas:
112123
113124\begin {verbatim }
114- username = form[ "username"]
115- if type(username ) is type([]):
125+ value = form.getvalue( "username", "")
126+ if type(value ) is type([]):
116127 # Multiple username fields specified
117- usernames = ""
118- for item in username:
119- if usernames:
120- # Next item -- insert comma
121- usernames = usernames + "," + item.value
122- else:
123- # First item -- don't insert comma
124- usernames = item.value
128+ usernames = ",".join(value)
125129else:
126- # Single username field specified
127- usernames = username. value
130+ # Single or no username field specified
131+ usernames = value
128132\end {verbatim }
129133
130- If a field represents an uploaded file, the value attribute reads the
134+ If a field represents an uploaded file, accessing the value via the
135+ \member {value} attribute or the \function {getvalue()} method reads the
131136entire file in memory as a string. This may not be what you want.
132- You can test for an uploaded file by testing either the filename
133- attribute or the file attribute. You can then read the data at
134- leisure from the file attribute:
137+ You can test for an uploaded file by testing either the \member { filename}
138+ attribute or the \member { file} attribute. You can then read the data at
139+ leisure from the \member { file} attribute:
135140
136141\begin {verbatim }
137142fileitem = form["userfile"]
@@ -157,7 +162,8 @@ \subsection{Using the cgi module}
157162as a single data part of type
158163\mimetype {application/x-www-form-urlencoded}), the items will actually
159164be instances of the class \class {MiniFieldStorage}. In this case, the
160- list, file and filename attributes are always \code {None}.
165+ \member {list}, \member {file}, and \member {filename} attributes are
166+ always \code {None}.
161167
162168
163169\subsection {Old classes }
@@ -233,23 +239,22 @@ \subsection{Functions}
233239\begin {funcdesc }{parse_multipart}{fp, pdict}
234240Parse input of type \mimetype {multipart/form-data} (for
235241file uploads). Arguments are \var {fp} for the input file and
236- \var {pdict} for the dictionary containing other parameters of
237- \code {content-type } header
242+ \var {pdict} for a dictionary containing other parameters in
243+ the \code {Content-Type } header.
238244
239245Returns a dictionary just like \function {parse_qs()} keys are the
240246field names, each value is a list of values for that field. This is
241247easy to use but not much good if you are expecting megabytes to be
242248uploaded --- in that case, use the \class {FieldStorage} class instead
243- which is much more flexible. Note that \code {content-type} is the
244- raw, unparsed contents of the \code {content-type} header.
249+ which is much more flexible.
245250
246251Note that this does not parse nested multipart parts --- use
247252\class {FieldStorage} for that.
248253\end {funcdesc }
249254
250255\begin {funcdesc }{parse_header}{string}
251- Parse a header like \code {content-type} into a main
252- content-type and a dictionary of parameters.
256+ Parse a MIME header (such as \code {Content-Type}) into a main
257+ value and a dictionary of parameters.
253258\end {funcdesc }
254259
255260\begin {funcdesc }{test}{}
@@ -432,7 +437,7 @@ \subsection{Debugging CGI scripts}
432437\begin {verbatim }
433438import sys
434439import traceback
435- print "Content-type : text/html"
440+ print "Content-Type : text/html"
436441print
437442sys.stderr = sys.stdout
438443try:
@@ -454,7 +459,7 @@ \subsection{Debugging CGI scripts}
454459\begin {verbatim }
455460import sys
456461sys.stderr = sys.stdout
457- print "Content-type : text/plain"
462+ print "Content-Type : text/plain"
458463print
459464...your code here...
460465\end {verbatim }
0 commit comments