Differences between revisions 7 and 8
Revision 7 as of 2005-03-25 15:14:54
Size: 1895
Editor: a84-231-127-5
Comment: Removed excessive linefeed in content type declaration.
Revision 8 as of 2005-03-28 07:19:01
Size: 1962
Editor: dsl-TN-090
Comment:
Deletions are marked like this. Additions are marked like this.
Line 66: Line 66:
    Nevow and [http://srid.bsdnerds.org/hacking/wallaby/ Wallaby]

CGI Scripts

The [http://www.python.org/doc/current/lib/module-cgi.html cgi module] is at the core of the Python CGI scripts.

Basically, you just need to print out an HTTP header ("Content-type: text/html"), a web page, and handle any forms you may have received.

Getting Apache's permissions just right can be annoying, and is sadly beyond this page's scope.

Sample Code

   1 #!/usr/bin/env python
   2 
   3 import cgi
   4 
   5 print "Content-type: text/html"
   6 print
   7 
   8 print """
   9 <html>
  10 
  11 <head><title>Sample CGI Script</title></head>
  12 
  13 <body>
  14 
  15   <h3> Sample CGI Script </h3>
  16 """
  17 
  18 form = cgi.FieldStorage()
  19 if form.has_key( "message" ):
  20     message = form["message"].value
  21 else:
  22     message = "(no message)"
  23 
  24 print """
  25 
  26   <p>Previous message: %s</p>
  27 
  28   <p>form:</p>
  29 
  30   <form method="post" action="index.cgi">
  31     <p>message: <input type="text" name="message"/></p>
  32   </form>
  33 
  34 </body>
  35 
  36 </html>
  37 """ % message

See Also

Discussion

CgiScripts (last edited 2014-04-16 06:53:27 by DaleAthanasias)

Unable to edit the page? See the FrontPage for instructions.