[Python-checkins] CVS: python/dist/src/Doc/lib libcgi.tex,1.27,1.28
Moshe Zadka
python-dev@python.org
Fri, 25 Aug 2000 14:47:58 -0700
Update of /cvsroot/python/python/dist/src/Doc/lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv8221/Doc/lib
Modified Files:
libcgi.tex
Log Message:
Closing patch #101120 -- After everyone agreed.
Index: libcgi.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libcgi.tex,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -r1.27 -r1.28
*** libcgi.tex 2000/07/16 19:01:09 1.27
--- libcgi.tex 2000/08/25 21:47:55 1.28
***************
*** 46,50 ****
\begin{verbatim}
! print "Content-type: text/html" # HTML is following
print # blank line, end of headers
\end{verbatim}
--- 46,50 ----
\begin{verbatim}
! print "Content-Type: text/html" # HTML is following
print # blank line, end of headers
\end{verbatim}
***************
*** 60,66 ****
\end{verbatim}
- (It may not be fully legal HTML according to the letter of the
- standard, but any browser will understand it.)
-
\subsection{Using the cgi module}
\nodename{Using the cgi module}
--- 60,63 ----
***************
*** 77,84 ****
standard). Since it may consume standard input, it should be
instantiated only once.
! The \class{FieldStorage} instance can be accessed as if it were a Python
! dictionary. For instance, the following code (which assumes that the
! \code{content-type} header and blank line have already been printed)
checks that the fields \code{name} and \code{addr} are both set to a
non-empty string:
--- 74,88 ----
standard). Since it may consume standard input, it should be
instantiated only once.
+
+ The \class{FieldStorage} instance can be indexed like a Python
+ dictionary, and also supports the standard dictionary methods
+ \function{has_key()} and \function{keys()}.
+ Form fields containing empty strings are ignored
+ and do not appear in the dictionary; to keep such values, provide
+ the optional \samp{keep_blank_values} argument when creating the
+ \class {FieldStorage} instance.
! For instance, the following code (which assumes that the
! \code{Content-Type} header and blank line have already been printed)
checks that the fields \code{name} and \code{addr} are both set to a
non-empty string:
***************
*** 88,97 ****
form_ok = 0
if form.has_key("name") and form.has_key("addr"):
! if form["name"].value != "" and form["addr"].value != "":
! form_ok = 1
if not form_ok:
print "<H1>Error</H1>"
print "Please fill in the name and addr fields."
return
...further form processing here...
\end{verbatim}
--- 92,102 ----
form_ok = 0
if form.has_key("name") and form.has_key("addr"):
! form_ok = 1
if not form_ok:
print "<H1>Error</H1>"
print "Please fill in the name and addr fields."
return
+ print "<p>name:", form["name"].value
+ print "<p>addr:", form["addr"].value
...further form processing here...
\end{verbatim}
***************
*** 100,108 ****
themselves instances of \class{FieldStorage} (or
\class{MiniFieldStorage}, depending on the form encoding).
If the submitted form data contains more than one field with the same
name, the object retrieved by \samp{form[\var{key}]} is not a
\class{FieldStorage} or \class{MiniFieldStorage}
! instance but a list of such instances. If you expect this possibility
(i.e., when your HTML form contains multiple fields with the same
name), use the \function{type()} function to determine whether you
--- 105,119 ----
themselves instances of \class{FieldStorage} (or
\class{MiniFieldStorage}, depending on the form encoding).
+ The \member{value} attribute of the instance yields the string value
+ of the field. The \function{getvalue()} method returns this string value
+ directly; it also accepts an optional second argument as a default to
+ return if the requested key is not present.
If the submitted form data contains more than one field with the same
name, the object retrieved by \samp{form[\var{key}]} is not a
\class{FieldStorage} or \class{MiniFieldStorage}
! instance but a list of such instances. Similarly, in this situation,
! \samp{form.getvalue(\var{key})} would return a list of strings.
! If you expect this possibility
(i.e., when your HTML form contains multiple fields with the same
name), use the \function{type()} function to determine whether you
***************
*** 112,136 ****
\begin{verbatim}
! username = form["username"]
! if type(username) is type([]):
# Multiple username fields specified
! usernames = ""
! for item in username:
! if usernames:
! # Next item -- insert comma
! usernames = usernames + "," + item.value
! else:
! # First item -- don't insert comma
! usernames = item.value
else:
! # Single username field specified
! usernames = username.value
\end{verbatim}
! If a field represents an uploaded file, the value attribute reads the
entire file in memory as a string. This may not be what you want.
! You can test for an uploaded file by testing either the filename
! attribute or the file attribute. You can then read the data at
! leisure from the file attribute:
\begin{verbatim}
--- 123,141 ----
\begin{verbatim}
! value = form.getvalue("username", "")
! if type(value) is type([]):
# Multiple username fields specified
! usernames = ",".join(value)
else:
! # Single or no username field specified
! usernames = value
\end{verbatim}
! If a field represents an uploaded file, accessing the value via the
! \member{value} attribute or the \function{getvalue()} method reads the
entire file in memory as a string. This may not be what you want.
! You can test for an uploaded file by testing either the \member{filename}
! attribute or the \member{file} attribute. You can then read the data at
! leisure from the \member{file} attribute:
\begin{verbatim}
***************
*** 158,162 ****
\mimetype{application/x-www-form-urlencoded}), the items will actually
be instances of the class \class{MiniFieldStorage}. In this case, the
! list, file and filename attributes are always \code{None}.
--- 163,168 ----
\mimetype{application/x-www-form-urlencoded}), the items will actually
be instances of the class \class{MiniFieldStorage}. In this case, the
! \member{list}, \member{file}, and \member{filename} attributes are
! always \code{None}.
***************
*** 234,239 ****
Parse input of type \mimetype{multipart/form-data} (for
file uploads). Arguments are \var{fp} for the input file and
! \var{pdict} for the dictionary containing other parameters of
! \code{content-type} header
Returns a dictionary just like \function{parse_qs()} keys are the
--- 240,245 ----
Parse input of type \mimetype{multipart/form-data} (for
file uploads). Arguments are \var{fp} for the input file and
! \var{pdict} for a dictionary containing other parameters in
! the \code{Content-Type} header.
Returns a dictionary just like \function{parse_qs()} keys are the
***************
*** 241,246 ****
easy to use but not much good if you are expecting megabytes to be
uploaded --- in that case, use the \class{FieldStorage} class instead
! which is much more flexible. Note that \code{content-type} is the
! raw, unparsed contents of the \code{content-type} header.
Note that this does not parse nested multipart parts --- use
--- 247,251 ----
easy to use but not much good if you are expecting megabytes to be
uploaded --- in that case, use the \class{FieldStorage} class instead
! which is much more flexible.
Note that this does not parse nested multipart parts --- use
***************
*** 249,254 ****
\begin{funcdesc}{parse_header}{string}
! Parse a header like \code{content-type} into a main
! content-type and a dictionary of parameters.
\end{funcdesc}
--- 254,259 ----
\begin{funcdesc}{parse_header}{string}
! Parse a MIME header (such as \code{Content-Type}) into a main
! value and a dictionary of parameters.
\end{funcdesc}
***************
*** 433,437 ****
import sys
import traceback
! print "Content-type: text/html"
print
sys.stderr = sys.stdout
--- 438,442 ----
import sys
import traceback
! print "Content-Type: text/html"
print
sys.stderr = sys.stdout
***************
*** 455,459 ****
import sys
sys.stderr = sys.stdout
! print "Content-type: text/plain"
print
...your code here...
--- 460,464 ----
import sys
sys.stderr = sys.stdout
! print "Content-Type: text/plain"
print
...your code here...