<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>11.1.2 Using the cgi module</TITLE>
<META NAME="description" CONTENT="11.1.2 Using the cgi module">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="node192.html">
<LINK REL="previous" HREF="cgi-intro.html">
<LINK REL="up" HREF="module-cgi.html">
<LINK REL="next" HREF="node192.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="node192.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-cgi.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="cgi-intro.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="node192.html">11.1.3 Old classes</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-cgi.html">11.1 cgi  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="cgi-intro.html">11.1.1 Introduction</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION0013120000000000000000">
11.1.2 Using the cgi module</A>
</H2>

<P>
Begin by writing "<tt class=samp>import cgi</tt>".  Do not use "<tt class=samp>from cgi import
*</tt>" -- the module defines all sorts of names for its own use or for
backward compatibility that you don't want in your namespace.

<P>
It's best to use the <tt class=class>FieldStorage</tt> class.  The other classes
defined in this module are provided mostly for backward compatibility.
Instantiate it exactly once, without arguments.  This reads the form
contents from standard input or the environment (depending on the
value of various environment variables set according to the CGI
standard).  Since it may consume standard input, it should be
instantiated only once.

<P>
The <tt class=class>FieldStorage</tt> instance can be accessed as if it were a Python 
dictionary.  For instance, the following code (which assumes that the 
<tt>content-type</tt> header and blank line have already been printed)
checks that the fields <tt>name</tt> and <tt>addr</tt> are both set to a
non-empty string:

<P>
<dl><dd><pre class=verbatim>
form = cgi.FieldStorage()
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 "&lt;H1&gt;Error&lt;/H1&gt;"
    print "Please fill in the name and addr fields."
    return
...further form processing here...
</pre></dl>

<P>
Here the fields, accessed through "<tt class=samp>form[<var>key</var>]</tt>", are
themselves instances of <tt class=class>FieldStorage</tt> (or
<tt class=class>MiniFieldStorage</tt>, depending on the form encoding).

<P>
If the submitted form data contains more than one field with the same
name, the object retrieved by "<tt class=samp>form[<var>key</var>]</tt>" is not a
<tt class=class>FieldStorage</tt> or <tt class=class>MiniFieldStorage</tt>
instance but a list of such instances.  If you expect this possibility
(i.e., when your HTML form comtains multiple fields with the same
name), use the <tt class=function>type()</tt> function to determine whether you
have a single instance or a list of instances.  For example, here's
code that concatenates any number of username fields, separated by
commas:

<P>
<dl><dd><pre class=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
</pre></dl>

<P>
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
leasure from the file attribute:

<P>
<dl><dd><pre class=verbatim>
fileitem = form["userfile"]
if fileitem.file:
    # It's an uploaded file; count lines
    linecount = 0
    while 1:
        line = fileitem.file.readline()
        if not line: break
        linecount = linecount + 1
</pre></dl>

<P>
The file upload draft standard entertains the possibility of uploading
multiple files from one field (using a recursive
<span class=mimetype>multipart/*</span> encoding).  When this occurs, the item will be
a dictionary-like <tt class=class>FieldStorage</tt> item.  This can be determined
by testing its <tt class=member>type</tt> attribute, which should be
<span class=mimetype>multipart/form-data</span> (or perhaps another MIME type matching
<span class=mimetype>multipart/*</span>).  In this case, it can be iterated over
recursively just like the top-level form object.

<P>
When a form is submitted in the ``old'' format (as the query string or
as a single data part of type
<span class=mimetype>application/x-www-form-urlencoded</span>), the items will actually
be instances of the class <tt class=class>MiniFieldStorage</tt>.  In this case, the
list, file and filename attributes are always <tt>None</tt>.

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="node192.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-cgi.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="cgi-intro.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="node192.html">11.1.3 Old classes</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-cgi.html">11.1 cgi  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="cgi-intro.html">11.1.1 Introduction</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
