Differences between revisions 2 and 4 (spanning 2 versions)
Revision 2 as of 2004-07-15 07:55:36
Size: 1609
Editor: dsl254-010-130
Comment:
Revision 4 as of 2004-09-14 17:09:21
Size: 1854
Editor: cache6-popl
Comment: Added voidspace link
Deletions are marked like this. Additions are marked like this.
Line 61: Line 61:
 * [http://www.devshed.com/index2.php?option=content&task=view&id=198&pop=1&page=0&hide_js=1 Writing CGI Scripts in Python]
 * [http://www.voidspace.xennos.com/ Voidspace Python CGI collection] - Working Python CGI scripts to use and/or study

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 print
   8 
   9 print """
  10 <html>
  11 
  12 <head><title>Sample CGI Script</title></head>
  13 
  14 <body>
  15 
  16   <h3> Sample CGI Script </h3>
  17 """
  18 
  19 form = cgi.FieldStorage()
  20 if form.has_key( "message" ):
  21     message = form["message"].value
  22 else:
  23     message = "(no message)"
  24 
  25 print """
  26 
  27   <p>Previous message: %s</p>
  28 
  29   <p>form:</p>
  30 
  31   <form method="post" action="index.cgi">
  32     <p>message: <input type="text" name="message"/></p>
  33   </form>
  34 
  35 </body>
  36 
  37 </html>
  38 """ % message

See Also

Discussion

  • (none yet!)

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

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