home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / i / introducti < prev   
Text File  |  1996-11-14  |  2KB  |  44 lines

  1. <TITLE>Introduction to the CGI module -- Python library reference</TITLE>
  2. Next: <A HREF="../u/using_the_cgi_module" TYPE="Next">Using the cgi module</A>  
  3. Prev: <A HREF="../c/cgi" TYPE="Prev">cgi</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.1. Introduction</H2>
  7. A CGI script is invoked by an HTTP server, usually to process user
  8. input submitted through an HTML <CODE><FORM></CODE> or <CODE><ISINPUT></CODE> element.
  9. <P>
  10. Most often, CGI scripts live in the server's special <CODE>cgi-bin</CODE>
  11. directory.  The HTTP server places all sorts of information about the
  12. request (such as the client's hostname, the requested URL, the query
  13. string, and lots of other goodies) in the script's shell environment,
  14. executes the script, and sends the script's output back to the client.
  15. <P>
  16. The script's input is connected to the client too, and sometimes the
  17. form data is read this way; at other times the form data is passed via
  18. the ``query string'' part of the URL.  This module (<CODE>cgi.py</CODE>) is intended
  19. to take care of the different cases and provide a simpler interface to
  20. the Python script.  It also provides a number of utilities that help
  21. in debugging scripts, and the latest addition is support for file
  22. uploads from a form (if your browser supports it -- Grail 0.3 and
  23. Netscape 2.0 do).
  24. <P>
  25. The output of a CGI script should consist of two sections, separated
  26. by a blank line.  The first section contains a number of headers,
  27. telling the client what kind of data is following.  Python code to
  28. generate a minimal header section looks like this:
  29. <P>
  30. <UL COMPACT><CODE>    print "Content-type: text/html"    # HTML is following<P>
  31.     print                # blank line, end of headers<P>
  32. </CODE></UL>
  33. The second section is usually HTML, which allows the client software
  34. to display nicely formatted text with header, in-line images, etc.
  35. Here's Python code that prints a simple piece of HTML:
  36. <P>
  37. <UL COMPACT><CODE>    print "<TITLE>CGI script output</TITLE>"<P>
  38.     print "<H1>This is my first CGI script</H1>"<P>
  39.     print "Hello, world!"<P>
  40. </CODE></UL>
  41. (It may not be fully legal HTML according to the letter of the
  42. standard, but any browser will understand it.)
  43. <P>
  44.