ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH
advanced | search help

Reference
ActivePython 2.4
Python Documentation
Library Reference
11. Internet Protocols and Support
11.2 cgi -- Common Gateway Interface support.
11.2.1 Introduction
11.2.2 Using the cgi module
11.2.3 Higher Level Interface
11.2.4 Old classes
11.2.5 Functions
11.2.6 Caring about security
11.2.7 Installing your CGI script on a Unix system
11.2.8 Testing your CGI script
11.2.9 Debugging CGI scripts
11.2.10 Common problems and solutions

MyASPN >> Reference >> ActivePython 2.4 >> Python Documentation >> Library Reference >> 11. Internet Protocols and Support >> 11.2 cgi -- Common Gateway Interface support.
ActivePython 2.4 documentation

11.2.1 Introduction

A CGI script is invoked by an HTTP server, usually to process user input submitted through an HTML <FORM> or <ISINDEX> element.

Most often, CGI scripts live in the server's special cgi-bin directory. The HTTP server places all sorts of information about the request (such as the client's hostname, the requested URL, the query string, and lots of other goodies) in the script's shell environment, executes the script, and sends the script's output back to the client.

The script's input is connected to the client too, and sometimes the form data is read this way; at other times the form data is passed via the ``query string'' part of the URL. This module is intended to take care of the different cases and provide a simpler interface to the Python script. It also provides a number of utilities that help in debugging scripts, and the latest addition is support for file uploads from a form (if your browser supports it).

The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is following. Python code to generate a minimal header section looks like this:

print "Content-Type: text/html"     # HTML is following
print                               # blank line, end of headers

The second section is usually HTML, which allows the client software to display nicely formatted text with header, in-line images, etc. Here's Python code that prints a simple piece of HTML:

print "<TITLE>CGI script output</TITLE>"
print "<H1>This is my first CGI script</H1>"
print "Hello, world!"

See About this document... for information on suggesting changes.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved