home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / u / using_the_ < prev    next >
Text File  |  1996-11-14  |  4KB  |  88 lines

  1. <TITLE>Using the cgi module -- Python library reference</TITLE>
  2. Next: <A HREF="../o/old_classes" TYPE="Next">Old classes</A>  
  3. Prev: <A HREF="../i/introduction_to_the_cgi_module" TYPE="Prev">Introduction to the CGI module</A>  
  4. Up: <A HREF="../c/cgi" TYPE="Up">cgi</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H2>10.1.2. Using the cgi module</H2>
  7. Begin by writing <CODE>import cgi</CODE>.  Don't use <CODE>from cgi import *</CODE> -- the
  8. module defines all sorts of names for its own use or for backward 
  9. compatibility that you don't want in your namespace.
  10. <P>
  11. It's best to use the <CODE>FieldStorage</CODE> class.  The other classes define in this 
  12. module are provided mostly for backward compatibility.  Instantiate it 
  13. exactly once, without arguments.  This reads the form contents from 
  14. standard input or the environment (depending on the value of various 
  15. environment variables set according to the CGI standard).  Since it may 
  16. consume standard input, it should be instantiated only once.
  17. <P>
  18. The <CODE>FieldStorage</CODE> instance can be accessed as if it were a Python 
  19. dictionary.  For instance, the following code (which assumes that the 
  20. <CODE>Content-type</CODE> header and blank line have already been printed) checks that 
  21. the fields <CODE>name</CODE> and <CODE>addr</CODE> are both set to a non-empty string:
  22. <P>
  23. <UL COMPACT><CODE>    form = cgi.FieldStorage()<P>
  24.     form_ok = 0<P>
  25.     if form.has_key("name") and form.has_key("addr"):<P>
  26.         if form["name"].value != "" and form["addr"].value != "":<P>
  27.             form_ok = 1<P>
  28.     if not form_ok:<P>
  29.         print "<H1>Error</H1>"<P>
  30.         print "Please fill in the name and addr fields."<P>
  31.         return<P>
  32.     ...further form processing here...<P>
  33. </CODE></UL>
  34. Here the fields, accessed through <CODE>form[key]</CODE>, are themselves instances
  35. of <CODE>FieldStorage</CODE> (or <CODE>MiniFieldStorage</CODE>, depending on the form encoding).
  36. <P>
  37. If the submitted form data contains more than one field with the same
  38. name, the object retrieved by <CODE>form[key]</CODE> is not a <CODE>(Mini)FieldStorage</CODE>
  39. instance but a list of such instances.  If you expect this possibility
  40. (i.e., when your HTML form comtains multiple fields with the same
  41. name), use the <CODE>type()</CODE> function to determine whether you have a single
  42. instance or a list of instances.  For example, here's code that
  43. concatenates any number of username fields, separated by commas:
  44. <P>
  45. <UL COMPACT><CODE>    username = form["username"]<P>
  46.     if type(username) is type([]):<P>
  47.         # Multiple username fields specified<P>
  48.         usernames = ""<P>
  49.         for item in username:<P>
  50.             if usernames:<P>
  51.                 # Next item -- insert comma<P>
  52.                 usernames = usernames + "," + item.value<P>
  53.             else:<P>
  54.                 # First item -- don't insert comma<P>
  55.                 usernames = item.value<P>
  56.     else:<P>
  57.         # Single username field specified<P>
  58.         usernames = username.value<P>
  59. </CODE></UL>
  60. If a field represents an uploaded file, the value attribute reads the 
  61. entire file in memory as a string.  This may not be what you want.  You can 
  62. test for an uploaded file by testing either the filename attribute or the 
  63. file attribute.  You can then read the data at leasure from the file 
  64. attribute:
  65. <P>
  66. <UL COMPACT><CODE>    fileitem = form["userfile"]<P>
  67.     if fileitem.file:<P>
  68.         # It's an uploaded file; count lines<P>
  69.         linecount = 0<P>
  70.         while 1:<P>
  71.             line = fileitem.file.readline()<P>
  72.             if not line: break<P>
  73.             linecount = linecount + 1<P>
  74. </CODE></UL>
  75. The file upload draft standard entertains the possibility of uploading
  76. multiple files from one field (using a recursive <CODE>multipart/*</CODE>
  77. encoding).  When this occurs, the item will be a dictionary-like
  78. FieldStorage item.  This can be determined by testing its type
  79. attribute, which should have the value <CODE>multipart/form-data</CODE> (or
  80. perhaps another string beginning with <CODE>multipart/</CODE>  It this case, it
  81. can be iterated over recursively just like the top-level form object.
  82. <P>
  83. When a form is submitted in the ``old'' format (as the query string or as a 
  84. single data part of type <CODE>application/x-www-form-urlencoded</CODE>), the items 
  85. will actually be instances of the class <CODE>MiniFieldStorage</CODE>.  In this case,
  86. the list, file and filename attributes are always <CODE>None</CODE>.
  87. <P>
  88.