home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Using the cgi module -- Python library reference</TITLE>
- Next: <A HREF="../o/old_classes" TYPE="Next">Old classes</A>
- Prev: <A HREF="../i/introduction_to_the_cgi_module" TYPE="Prev">Introduction to the CGI module</A>
- Up: <A HREF="../c/cgi" TYPE="Up">cgi</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>10.1.2. Using the cgi module</H2>
- Begin by writing <CODE>import cgi</CODE>. Don't use <CODE>from cgi import *</CODE> -- 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 <CODE>FieldStorage</CODE> class. The other classes define 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 <CODE>FieldStorage</CODE> instance can be accessed as if it were a Python
- dictionary. For instance, the following code (which assumes that the
- <CODE>Content-type</CODE> header and blank line have already been printed) checks that
- the fields <CODE>name</CODE> and <CODE>addr</CODE> are both set to a non-empty string:
- <P>
- <UL COMPACT><CODE> form = cgi.FieldStorage()<P>
- form_ok = 0<P>
- if form.has_key("name") and form.has_key("addr"):<P>
- if form["name"].value != "" and form["addr"].value != "":<P>
- form_ok = 1<P>
- if not form_ok:<P>
- print "<H1>Error</H1>"<P>
- print "Please fill in the name and addr fields."<P>
- return<P>
- ...further form processing here...<P>
- </CODE></UL>
- Here the fields, accessed through <CODE>form[key]</CODE>, are themselves instances
- of <CODE>FieldStorage</CODE> (or <CODE>MiniFieldStorage</CODE>, depending on the form encoding).
- <P>
- If the submitted form data contains more than one field with the same
- name, the object retrieved by <CODE>form[key]</CODE> is not a <CODE>(Mini)FieldStorage</CODE>
- 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 <CODE>type()</CODE> 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>
- <UL COMPACT><CODE> username = form["username"]<P>
- if type(username) is type([]):<P>
- # Multiple username fields specified<P>
- usernames = ""<P>
- for item in username:<P>
- if usernames:<P>
- # Next item -- insert comma<P>
- usernames = usernames + "," + item.value<P>
- else:<P>
- # First item -- don't insert comma<P>
- usernames = item.value<P>
- else:<P>
- # Single username field specified<P>
- usernames = username.value<P>
- </CODE></UL>
- 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>
- <UL COMPACT><CODE> fileitem = form["userfile"]<P>
- if fileitem.file:<P>
- # It's an uploaded file; count lines<P>
- linecount = 0<P>
- while 1:<P>
- line = fileitem.file.readline()<P>
- if not line: break<P>
- linecount = linecount + 1<P>
- </CODE></UL>
- The file upload draft standard entertains the possibility of uploading
- multiple files from one field (using a recursive <CODE>multipart/*</CODE>
- encoding). When this occurs, the item will be a dictionary-like
- FieldStorage item. This can be determined by testing its type
- attribute, which should have the value <CODE>multipart/form-data</CODE> (or
- perhaps another string beginning with <CODE>multipart/</CODE> It 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 <CODE>application/x-www-form-urlencoded</CODE>), the items
- will actually be instances of the class <CODE>MiniFieldStorage</CODE>. In this case,
- the list, file and filename attributes are always <CODE>None</CODE>.
- <P>
-