Module cgi
[hide private]
[frames] | no frames]

Module cgi

Support module for CGI (Common Gateway Interface) scripts.

This module defines a number of utilities for use by CGI scripts written in Python.


Version: 2.6

Classes [hide private]
MiniFieldStorage
Like FieldStorage, for use when no file uploads are possible.
FieldStorage
Store a sequence of fields, reading multipart/form-data.
FormContentDict
Form content as dictionary with a list of values per field.
SvFormContentDict
Form content as dictionary expecting a single value per field.
InterpFormContentDict
This class is present for backwards compatibility only.
FormContent
This class is present for backwards compatibility only.
Functions [hide private]
 
initlog(*allargs)
Write a log message, if there is a log file.
 
dolog(fmt, *args)
Write a log message to the log file.
 
nolog(*allargs)
Dummy function, assigned to log when logging is disabled.
 
log(*allargs)
Write a log message, if there is a log file.
 
parse(fp=None, environ={'LESS': '-X -i -Psline %l/%L$ -Pmline %l/%L (%p)$', 'DPKG_ARC..., keep_blank_values=0, strict_parsing=0)
Parse a query in the environment or from a file (default stdin)
 
parse_qs(qs, keep_blank_values=0, strict_parsing=0)
Parse a query given as a string argument.
 
parse_qsl(qs, keep_blank_values=0, strict_parsing=0)
Parse a query given as a string argument.
 
parse_multipart(fp, pdict)
Parse multipart input.
 
parse_header(line)
Parse a Content-type like header.
 
test(environ={'LESS': '-X -i -Psline %l/%L$ -Pmline %l/%L (%p)$', 'DPKG_ARC...)
Robust test CGI script, usable as main program.
 
print_exception(type=None, value=None, tb=None, limit=None)
 
print_environ(environ={'LESS': '-X -i -Psline %l/%L$ -Pmline %l/%L (%p)$', 'DPKG_ARC...)
Dump the shell environment as HTML.
 
print_form(form)
Dump the contents of a form as HTML.
 
print_directory()
Dump the current directory as HTML.
 
print_arguments()
 
print_environ_usage()
Dump a list of environment variables used by CGI as HTML.
 
escape(s, quote=None)
Replace special characters "&", "<" and ">" to HTML-safe sequences.
 
valid_boundary(s, _vb_pattern='^[ -~]{0,200}[!-~]$')
Variables [hide private]
  logfile = ''
  logfp = None
  maxlen = 0

Imports: attrgetter, sys, os, urllib, mimetools, rfc822, UserDict, StringIO


Function Details [hide private]

initlog(*allargs)

 
Write a log message, if there is a log file.

Even though this function is called initlog(), you should always
use log(); log is a variable that is set either to initlog
(initially), to dolog (once the log file has been opened), or to
nolog (when logging is disabled).

The first argument is a format string; the remaining arguments (if
any) are arguments to the % operator, so e.g.
    log("%s: %s", "a", "b")
will write "a: b" to the log file, followed by a newline.

If the global logfp is not None, it should be a file object to
which log data is written.

If the global logfp is None, the global logfile may be a string
giving a filename to open, in append mode.  This file should be
world writable!!!  If the file can't be opened, logging is
silently disabled (since there is no safe place where we could
send an error message).

dolog(fmt, *args)

 

Write a log message to the log file. See initlog() for docs.

log(*allargs)

 
Write a log message, if there is a log file.

Even though this function is called initlog(), you should always
use log(); log is a variable that is set either to initlog
(initially), to dolog (once the log file has been opened), or to
nolog (when logging is disabled).

The first argument is a format string; the remaining arguments (if
any) are arguments to the % operator, so e.g.
    log("%s: %s", "a", "b")
will write "a: b" to the log file, followed by a newline.

If the global logfp is not None, it should be a file object to
which log data is written.

If the global logfp is None, the global logfile may be a string
giving a filename to open, in append mode.  This file should be
world writable!!!  If the file can't be opened, logging is
silently disabled (since there is no safe place where we could
send an error message).

parse(fp=None, environ={'LESS': '-X -i -Psline %l/%L$ -Pmline %l/%L (%p)$', 'DPKG_ARC..., keep_blank_values=0, strict_parsing=0)

 
Parse a query in the environment or from a file (default stdin)

Arguments, all optional:

fp              : file pointer; default: sys.stdin

environ         : environment dictionary; default: os.environ

keep_blank_values: flag indicating whether blank values in
    URL encoded forms should be treated as blank strings.
    A true value indicates that blanks should be retained as
    blank strings.  The default false value indicates that
    blank values are to be ignored and treated as if they were
    not included.

strict_parsing: flag indicating what to do with parsing errors.
    If false (the default), errors are silently ignored.
    If true, errors raise a ValueError exception.

parse_qs(qs, keep_blank_values=0, strict_parsing=0)

 
Parse a query given as a string argument.

Arguments:

qs: URL-encoded query string to be parsed

keep_blank_values: flag indicating whether blank values in
    URL encoded queries should be treated as blank strings.
    A true value indicates that blanks should be retained as
    blank strings.  The default false value indicates that
    blank values are to be ignored and treated as if they were
    not included.

strict_parsing: flag indicating what to do with parsing errors.
    If false (the default), errors are silently ignored.
    If true, errors raise a ValueError exception.

parse_qsl(qs, keep_blank_values=0, strict_parsing=0)

 
Parse a query given as a string argument.

Arguments:

qs: URL-encoded query string to be parsed

keep_blank_values: flag indicating whether blank values in
    URL encoded queries should be treated as blank strings.  A
    true value indicates that blanks should be retained as blank
    strings.  The default false value indicates that blank values
    are to be ignored and treated as if they were  not included.

strict_parsing: flag indicating what to do with parsing errors. If
    false (the default), errors are silently ignored. If true,
    errors raise a ValueError exception.

Returns a list, as G-d intended.

parse_multipart(fp, pdict)

 

Parse multipart input.

Arguments: fp : input file pdict: dictionary containing other parameters of content-type header

Returns a dictionary just like parse_qs(): keys are the field names, each value is a list of values for that field. This is easy to use but not much good if you are expecting megabytes to be uploaded -- in that case, use the FieldStorage class instead which is much more flexible. Note that content-type is the raw, unparsed contents of the content-type header.

XXX This does not parse nested multipart parts -- use FieldStorage for that.

XXX This should really be subsumed by FieldStorage altogether -- no point in having two implementations of the same parsing algorithm. Also, FieldStorage protects itself better against certain DoS attacks by limiting the size of the data read in one chunk. The API here does not support that kind of protection. This also affects parse() since it can call parse_multipart().

parse_header(line)

 

Parse a Content-type like header.

Return the main content-type and a dictionary of options.

test(environ={'LESS': '-X -i -Psline %l/%L$ -Pmline %l/%L (%p)$', 'DPKG_ARC...)

 

Robust test CGI script, usable as main program.

Write minimal HTTP headers and dump all information provided to the script in HTML form.

escape(s, quote=None)

 

Replace special characters "&", "<" and ">" to HTML-safe sequences. If the optional flag quote is true, the quotation mark character (") is also translated.