Differences between revisions 16 and 18 (spanning 2 versions)
Revision 16 as of 2007-01-31 07:02:44
Size: 2676
Editor: BTNL-TN-DSL-dynamic-151
Comment:
Revision 18 as of 2007-01-31 13:20:37
Size: 4463
Editor: PaulBoddie
Comment: CategoryLanguage is for specific natural languages. Improved the text.
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
The [http://www.python.org/doc/current/lib/module-cgi.html cgi module] is at the core of the Python CGI scripts. The [http://www.python.org/doc/current/lib/module-cgi.html cgi module] is at the core of Python CGI scripts.
Line 6: Line 6:
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. The simplest CGI script that can be considered interesting involves printing out an HTTP header ("Content-type: text/html") and a Web page. In addition, you might want to handle any incoming inputs from things like HTML forms or request parameters. In the earliest days of CGI, shell scripts were sometimes used to do things like this, so the principles are not particularly advanced.
Line 8: Line 8:
Getting Apache's permissions just right can be annoying, and is sadly beyond this page's scope. == Configuration ==

It can be an annoying experience getting the permissions just right on a script so that Web servers like Apache will run it, but the following checklist may be of some use:

 1. Find out which user runs the Web server - it's not often the same one as your own user, and it may be one with very limited permissions.
 1. Check the server configuration to see if it lets you run scripts in a particular directory. Make sure that if you're using a configuration file for a particular directory, the global configuration permits you to define CGI script directories in that directory-local configuration file - some sites stop their users from altering such settings in such a way.
 1. Check the permissions from the top of the filesystem down to the directory where the script resides. The Web server user must be able to read and open/execute all the directories from the top right down to the script.
 1. Make sure your script is readable and executable by the Web server user.
 1. Make sure that the first line of the script refers to an interpreter that the Web server user can run. Things like {{{/usr/bin/env python}}} might not have any meaning to the Web server user because the {{{python}}} program may not be on the user's {{{PATH}}}.
Line 11: Line 19:

The following code attempts to combine simple output of a Web page with the processing of input from users viewing the page. You may wish to choose the actual first line of the script based on one of the first two lines provided below - the first one will probably work only on Windows, whereas the second may only work on UNIX-like systems.
Line 53: Line 63:
 * ["WebProgramming"] - the natural next step beyond simple CGI scripts.
Line 62: Line 73:
= Discussion = ----

=
= Discussion ==
Line 72: Line 85:
There are many frameworks for Python Web Application<br />
TurboGears <br />danjo <br />Zope <br />ModPython <br />Pso <br />Aquarium <br />Cheetah ++++++...
There are many frameworks for Python Web Application
TurboGears danjo Zope ModPython Pso Aquarium Cheetah ++++++...
Line 83: Line 96:
CategoryPythonWebsite CategoryLanguage CategoryPythonWebsite

CGI Scripts

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

The simplest CGI script that can be considered interesting involves printing out an HTTP header ("Content-type: text/html") and a Web page. In addition, you might want to handle any incoming inputs from things like HTML forms or request parameters. In the earliest days of CGI, shell scripts were sometimes used to do things like this, so the principles are not particularly advanced.

Configuration

It can be an annoying experience getting the permissions just right on a script so that Web servers like Apache will run it, but the following checklist may be of some use:

  1. Find out which user runs the Web server - it's not often the same one as your own user, and it may be one with very limited permissions.
  2. Check the server configuration to see if it lets you run scripts in a particular directory. Make sure that if you're using a configuration file for a particular directory, the global configuration permits you to define CGI script directories in that directory-local configuration file - some sites stop their users from altering such settings in such a way.
  3. Check the permissions from the top of the filesystem down to the directory where the script resides. The Web server user must be able to read and open/execute all the directories from the top right down to the script.
  4. Make sure your script is readable and executable by the Web server user.
  5. Make sure that the first line of the script refers to an interpreter that the Web server user can run. Things like /usr/bin/env python might not have any meaning to the Web server user because the python program may not be on the user's PATH.

Sample Code

The following code attempts to combine simple output of a Web page with the processing of input from users viewing the page. You may wish to choose the actual first line of the script based on one of the first two lines provided below - the first one will probably work only on Windows, whereas the second may only work on UNIX-like systems.

   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


Discussion

  • We need a good python CGI framework - Sridhar R


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

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

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