⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 using_the_cgi_module.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>11.2.2 Using the cgi module</title>
<META NAME="description" CONTENT="11.2.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" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" HREF="node241.html" tppabs="http://www.python.org/doc/current/lib/node241.html">
<LINK REL="previous" href="cgi-intro.html" tppabs="http://www.python.org/doc/current/lib/cgi-intro.html">
<LINK REL="up" href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html">
<LINK REL="next" HREF="node241.html" tppabs="http://www.python.org/doc/current/lib/node241.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="cgi-intro.html" tppabs="http://www.python.org/doc/current/lib/cgi-intro.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node241.html" tppabs="http://www.python.org/doc/current/lib/node241.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="cgi-intro.html" tppabs="http://www.python.org/doc/current/lib/cgi-intro.html">11.2.1 Introduction</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html">11.2 cgi  </A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node241.html" tppabs="http://www.python.org/doc/current/lib/node241.html">11.2.3 Old classes</A>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION0013220000000000000000">
11.2.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 indexed like a Python
dictionary, and also supports the standard dictionary methods
<tt class="function">has_key()</tt> and <tt class="function">keys()</tt>.
Form fields containing empty strings are ignored
and do not appear in the dictionary; to keep such values, provide
the optional "<tt class="samp">keep_blank_values</tt>" argument when creating the
<tt class="class">FieldStorage</tt> instance.

<P>
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>
<dl><dd><pre class="verbatim">
form = cgi.FieldStorage()
form_ok = 0
if form.has_key("name") and form.has_key("addr"):
    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
print "&lt;p&gt;name:", form["name"].value
print "&lt;p&gt;addr:", form["addr"].value
...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).
The <tt class="member">value</tt> attribute of the instance yields the string value
of the field.  The <tt class="function">getvalue()</tt> 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.

<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.  Similarly, in this situation,
"<tt class="samp">form.getvalue(<var>key</var>)</tt>" 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 <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">
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
</pre></dl>

<P>
If a field represents an uploaded file, accessing the value via the
<tt class="member">value</tt> attribute or the <tt class="function">getvalue()</tt> 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 <tt class="member">filename</tt>
attribute or the <tt class="member">file</tt> attribute.  You can then read the data at
leisure from the <tt class="member">file</tt> 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
<tt class="member">list</tt>, <tt class="member">file</tt>, and <tt class="member">filename</tt> attributes are
always <code>None</code>.

<P>

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="cgi-intro.html" tppabs="http://www.python.org/doc/current/lib/cgi-intro.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node241.html" tppabs="http://www.python.org/doc/current/lib/node241.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="cgi-intro.html" tppabs="http://www.python.org/doc/current/lib/cgi-intro.html">11.2.1 Introduction</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-cgi.html" tppabs="http://www.python.org/doc/current/lib/module-cgi.html">11.2 cgi  </A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node241.html" tppabs="http://www.python.org/doc/current/lib/node241.html">11.2.3 Old classes</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -