cgi.py

Support module for CGI (Common Gateway Interface) scripts.

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

XXX Perhaps there should be a slimmed version that doesn't contain all those backwards compatible and debugging classes and functions?

History

Michael McLay started this module. Steve Majewski changed the interface to SvFormContentDict and FormContentDict. The multipart parsing was inspired by code submitted by Andreas Paepcke. Guido van Rossum rewrote, reformatted and documented the module and is currently responsible for its maintenance.

__version__ = "2.6"

Imports

import sys
import os
import urllib
import mimetools
import rfc822
import UserDict
from StringIO import StringIO
__all__ =

Logging support

logfile = "" Filename to log to, if not empty
logfp = None File object to log to, if not None
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).

global logfp, log
if logfile and not logfp:
  
try:
  logfp = open(logfile, "a")
except IOError:
pass

if not logfp:
  log = nolog
else:
  log = dolog
log(*allargs)
dolog(fmt, *args):
Write a log message to the log file. See initlog() for docs.
logfp.write(fmt%args + "\n")
nolog(*allargs):
Dummy function, assigned to log when logging is disabled.
pass
log = initlog # The current logging function

Parsing functions

Maximum input we will accept when REQUEST_METHOD is POST 0 ==> unlimited input
maxlen = 0
parse(fp=None, environ=os.environ, 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.

if fp is None:
   fp = sys.stdin

if not 'REQUEST_METHOD' in environ:
  'REQUEST_METHOD'] = 'GET' For testing stand-alone

if environ['REQUEST_METHOD'] == 'POST':
  ctype, pdict = parse_header(environ['CONTENT_TYPE'])
if ctype == 'multipart/form-data':
  return parse_multipart(fp, pdict)
elif ctype == 'application/x-www-form-urlencoded':
clength = int(environ['CONTENT_LENGTH'])
if maxlen and clength > maxlen:
  raise ValueError, 'Maximum content length exceeded'
qs = fp.read(clength)
else:
qs = '' Unknown content-type

if 'QUERY_STRING' in environ:
  
if qs:
  qs = qs + '&'
qs = qs + environ['QUERY_STRING']
elif sys.argv[1:]:
if qs:
  qs = qs + '&'
qs = qs + sys.argv[1]
environ['QUERY_STRING'] = qs # XXX Shouldn't, really
elif 'QUERY_STRING' in environ:
qs = environ['QUERY_STRING']
else:
if sys.argv[1:]:
  qs = sys.argv[1]
else:
qs = ""
environ['QUERY_STRING'] = qs XXX Shouldn't, really
return parse_qs(qs, keep_blank_values, strict_parsing)

Last modified: Tue Sep 23 01:48:43 CDT 2003