~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Doc/library/cgi.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
:mod:`cgi` --- Common Gateway Interface support.
3
 
================================================
 
1
:mod:`cgi` --- Common Gateway Interface support
 
2
===============================================
4
3
 
5
4
.. module:: cgi
6
5
   :synopsis: Helpers for running Python scripts via the Common Gateway Interface.
92
91
various environment variables set according to the CGI standard).  Since it may
93
92
consume standard input, it should be instantiated only once.
94
93
 
95
 
The :class:`FieldStorage` instance can be indexed like a Python dictionary, and
96
 
also supports the standard dictionary methods :meth:`has_key` and :meth:`keys`.
97
 
The built-in :func:`len` is also supported.  Form fields containing empty
98
 
strings are ignored and do not appear in the dictionary; to keep such values,
99
 
provide a true value for the optional *keep_blank_values* keyword parameter when
100
 
creating the :class:`FieldStorage` instance.
 
94
The :class:`FieldStorage` instance can be indexed like a Python dictionary.
 
95
It allows membership testing with the :keyword:`in` operator, and also supports
 
96
the standard dictionary method :meth:`keys` and the built-in function
 
97
:func:`len`.  Form fields containing empty strings are ignored and do not appear
 
98
in the dictionary; to keep such values, provide a true value for the optional
 
99
*keep_blank_values* keyword parameter when creating the :class:`FieldStorage`
 
100
instance.
101
101
 
102
102
For instance, the following code (which assumes that the
103
103
:mailheader:`Content-Type` header and blank line have already been printed)
105
105
string::
106
106
 
107
107
   form = cgi.FieldStorage()
108
 
   if not (form.has_key("name") and form.has_key("addr")):
 
108
   if "name" not in form or "addr" not in form:
109
109
       print "<H1>Error</H1>"
110
110
       print "Please fill in the name and addr fields."
111
111
       return
136
136
If a field represents an uploaded file, accessing the value via the
137
137
:attr:`value` attribute or the :func:`getvalue` method reads the entire file in
138
138
memory as a string.  This may not be what you want. You can test for an uploaded
139
 
file by testing either the :attr:`filename` attribute or the :attr:`file`
140
 
attribute.  You can then read the data at leisure from the :attr:`file`
 
139
file by testing either the :attr:`filename` attribute or the :attr:`!file`
 
140
attribute.  You can then read the data at leisure from the :attr:`!file`
141
141
attribute::
142
142
 
143
143
   fileitem = form["userfile"]
157
157
The file upload draft standard entertains the possibility of uploading multiple
158
158
files from one field (using a recursive :mimetype:`multipart/\*` encoding).
159
159
When this occurs, the item will be a dictionary-like :class:`FieldStorage` item.
160
 
This can be determined by testing its :attr:`type` attribute, which should be
 
160
This can be determined by testing its :attr:`!type` attribute, which should be
161
161
:mimetype:`multipart/form-data` (or perhaps another MIME type matching
162
162
:mimetype:`multipart/\*`).  In this case, it can be iterated over recursively
163
163
just like the top-level form object.
165
165
When a form is submitted in the "old" format (as the query string or as a single
166
166
data part of type :mimetype:`application/x-www-form-urlencoded`), the items will
167
167
actually be instances of the class :class:`MiniFieldStorage`.  In this case, the
168
 
:attr:`list`, :attr:`file`, and :attr:`filename` attributes are always ``None``.
 
168
:attr:`!list`, :attr:`!file`, and :attr:`filename` attributes are always ``None``.
169
169
 
170
170
A form submitted via POST that also has a query string will contain both
171
171
:class:`FieldStorage` and :class:`MiniFieldStorage` items.
213
213
provide valid input to your scripts.  For example, if a curious user appends
214
214
another ``user=foo`` pair to the query string, then the script would crash,
215
215
because in this situation the ``getvalue("user")`` method call returns a list
216
 
instead of a string.  Calling the :meth:`toupper` method on a list is not valid
 
216
instead of a string.  Calling the :meth:`~str.upper` method on a list is not valid
217
217
(since lists do not have a method of this name) and results in an
218
218
:exc:`AttributeError` exception.
219
219