| Size: 1580 Comment: simple CGI example | Size: 2634 Comment:  | 
| Deletions are marked like this. | Additions are marked like this. | 
| Line 1: | Line 1: | 
| #pragma section-numbers off | |
| Line 16: | Line 17: | 
| import cgitb; cgitb.enable() # for troubleshooting | |
| Line 18: | Line 20: | 
| Line 32: | Line 33: | 
| if form.has_key( "message" ): message = form["message"].value else: message = "(no message)" | message = form.getvalue("message", "(no message)") | 
| Line 57: | Line 55: | 
| * [http://webpython.codepoint.net/ Python CGI tutorial] - setup in a shared host, forms, debug, shell commands, cookies, etc | |
| Line 60: | Line 59: | 
| * [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.org.uk/python/cgi.shtml Voidspace Python CGI collection] - Working Python CGI scripts to use and/or study | |
| Line 63: | Line 64: | 
| (none yet!) | We need a good python CGI framework - Sridhar R Nevow and [http://srid.bsdnerds.org/hacking/wallaby/ Wallaby] Define "framework," though. Do you mean something like a Django-type deal or something that just makes it easier to write CGI apps? ---- Yes.... There are many frameworks for Python Web Application TurboGears danjo Zope ModPython Pso Aquarium Cheetah ++++++... But it would be Nice if python provides native support for Session Handling, JSON - like XML-RPC Standard Environment for RPC + WSGI and future technologies.... for Easy Web Development -Vinoth vinoth.3v@gmail.com CategoryPythonWebsite CategoryLanguage | 
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 import cgitb; cgitb.enable()  # for troubleshooting
   5 
   6 print "Content-type: text/html"
   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 message = form.getvalue("message", "(no message)")
  21 
  22 print """
  23 
  24   <p>Previous message: %s</p>
  25 
  26   <p>form:</p>
  27 
  28   <form method="post" action="index.cgi">
  29     <p>message: <input type="text" name="message"/></p>
  30   </form>
  31 
  32 </body>
  33 
  34 </html>
  35 """ % message
See Also
- [http://www.python.org/doc/current/lib/module-cgi.html cgi module documentation] 
- [http://www.python.org/doc/current/lib/module-Cookie.html Cookie module documentation] 
- [http://webpython.codepoint.net/ Python CGI tutorial] - setup in a shared host, forms, debug, shell commands, cookies, etc 
- [http://www.cs.virginia.edu/~lab2q/lesson_7/ python CGI tutorial] - w/ hints about maintaining sessions either through forms or through cookies 
- [http://gnosis.cx/publish/programming/feature_5min_python.html python CGI tutorial] - w/ hints about printing out tracebacks 
- [http://www.python.org/doc/current/lib/internet.html other internet protocol module documentation] 
- [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.org.uk/python/cgi.shtml Voidspace Python CGI collection] - Working Python CGI scripts to use and/or study 
Discussion
- We need a good python CGI framework - Sridhar R - Nevow and [http://srid.bsdnerds.org/hacking/wallaby/ Wallaby] Define "framework," though. Do you mean something like a Django-type deal or something that just makes it easier to write CGI apps? 
 
Yes....
There are many frameworks for Python Web Application TurboGears danjo Zope ModPython Pso Aquarium Cheetah ++++++...
But it would be Nice if python provides native support for Session Handling, JSON - like XML-RPC Standard Environment for RPC + WSGI and future technologies.... for Easy Web Development
-Vinoth vinoth.3v@gmail.com
