See All Titles |
![]() ![]() The cgi ModuleThe cgi module accepts sys.stdin and environment variables set by the server as input sources. The output is sent directly to sys.stdout, carrying an HTTP header and the data itself. A very simple header example would be print "Content-type: text/html" print Note that it is necessary to have a new line at the end of the header information. In most cases, the previous line is all you will use in your scripts. The FieldStorage class, which is implemented by this module, is able to read both the standard input (for POST calls) and the query string (for GET calls). In order to parse the contents of an HTML Form, you need to create an instance of this class. This instance carries the following attributes:
Each individual form field is defined as an instance of the MiniFieldStorage class, whereas on the contrary, multipart data (such as uploaded files) is defined as an instance of the FieldStorage class itself. Each instance is accessed as a dictionary whose keys are the Form's field names, and the values are their contents. These dictionaries also implement methods such as .keys() and .has_key(). If a specific form field has multiple values (for example, a selection list), a list of multiple MiniFieldStorage instances is generated and assigned to the appropriate key value in the dictionary. The use of MiniFieldStorage is pretty much transparent when using CGI, thus, you don't have to worry about these implementation details. Note that uploaded files are read directly to the memory by accessing the value attribute of the class instance. Also note that Python 2.0 provides a new method called getvalue() to the objects of the FieldStorage class, that implements the same functionality of a dictionary's get() method by returning the value attribute of the given object. FunctionsThe following list shows some general functions exposed by the cgi module.
For CGI debugging, the following functions are available:
The following functions are not part of the CGI module, but they are very useful for CGI processing too.
|
© 2002, O'Reilly & Associates, Inc. |