CAT-SOOP API: catsoop.thirdparty.cgi
This module defines a number of utilities for use by CGI scripts written in Python.
The global variable maxlen can be set to an integer indicating the maximum size of a POST request. POST requests larger than this size will result in a ValueError being raised during parsing. The default value of this variable is 0, meaning the request size is unlimited.
Members
-
catsoop.thirdparty.cgi.BytesIO (
type)Buffered I/O implementation using an in-memory bytes buffer.
-
catsoop.thirdparty.cgi.FeedParser (
type, lines 136-530)A feed-style parser of email.
-
catsoop.thirdparty.cgi.FieldStorage (
type, lines 317-908)Store a sequence of fields, reading multipart/form-data.
This class provides naming, typing, files stored on disk, and more. At the top level, it is accessible like a dictionary, whose keys are the field names. (Note: None can occur as a field name.) The items are either a Python list (if there's multiple values) or another FieldStorage or MiniFieldStorage object. If it's a single object, it has the following attributes:
name: the field name, if specified; otherwise None
filename: the filename, if specified; otherwise None; this is the client side filename, not the file name on which it is stored (that's a temporary file you don't deal with)
value: the value as a string; for file uploads, this transparently reads the file every time you request the value and returns bytes
file: the file(-like) object from which you can read the data as bytes ; None if the data is stored a simple string
type: the content-type, or None if not specified
type_options: dictionary of options specified on the content-type line
disposition: content-disposition, or None if not specified
disposition_options: dictionary of corresponding options
headers: a dictionary(-like) object (sometimes email.message.Message or a subclass thereof) containing all headers
The class is subclassable, mostly for the purpose of overriding the make_file() method, which is called internally to come up with a file open for reading and writing. This makes it possible to override the default choice of storing all files in a temporary directory and unlinking them as soon as they have been opened.
-
catsoop.thirdparty.cgi.Mapping (
ABCMeta, lines 791-840)A Mapping is a generic container for associating key/value pairs.
This class provides concrete generic implementations of all methods except for getitem, iter, and len.
-
catsoop.thirdparty.cgi.Message (
type, lines 135-978)Basic message object.
A message object is defined as something that has a bunch of RFC 2822 headers and a payload. It may optionally have an envelope header (a.k.a. Unix-From or From_ header). If the message is a container (i.e. a multipart or a message/rfc822), then the payload is a list of Message objects, otherwise it is a string.
Message objects implement part of the `mapping' interface, which assumes there is exactly one occurrence of the header per message. Some headers do in fact appear multiple times (e.g. Received) and for those headers, you must use the explicit API to set or get all the headers. Not all of the mapping methods are implemented.
-
catsoop.thirdparty.cgi.MiniFieldStorage (
type, lines 292-314)Like FieldStorage, for use when no file uploads are possible.
-
catsoop.thirdparty.cgi.StringIO (
type)Text I/O implementation using an in-memory buffer.
The initial_value argument sets the value of object. The newline argument is like the one of TextIOWrapper's constructor.
-
catsoop.thirdparty.cgi.TextIOWrapper (
type)Character and line based layer over a BufferedIOBase object, buffer.
encoding gives the name of the encoding that the stream will be decoded or encoded with. It defaults to locale.getencoding().
errors determines the strictness of encoding and decoding (see help(codecs.Codec) or the documentation for codecs.register) and defaults to "strict".
newline controls how line endings are handled. It can be None, '', '\n', '\r', and '\r\n'. It works as follows:
-
On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
-
On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.
If line_buffering is True, a call to flush is implied when a call to write contains a newline character.
-
-
catsoop.thirdparty.cgi.closelog (
function, lines 125-132)Close the log file.
-
catsoop.thirdparty.cgi.dolog (
function, lines 115-117)Write a log message to the log file. See initlog() for docs.
-
catsoop.thirdparty.cgi.initlog (
function, lines 74-112)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).
-
catsoop.thirdparty.cgi.log (
function, lines 74-112)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).
- catsoop.thirdparty.cgi.logfile (
str)
- catsoop.thirdparty.cgi.logfp (
NoneType)
- catsoop.thirdparty.cgi.maxlen (
int)
-
catsoop.thirdparty.cgi.nolog (
function, lines 120-122)Dummy function, assigned to log when logging is disabled.
-
catsoop.thirdparty.cgi.parse (
function, lines 146-217)Parse a query in the environment or from a file (default stdin)
Arguments, all optional:
fp : file pointer; default: sys.stdin.buffer
environ : environment dictionary; default: os.environ
keep_blank_values: flag indicating whether blank values in percent-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.
separator: str. The symbol to use for separating the query arguments. Defaults to &.
-
catsoop.thirdparty.cgi.parse_header (
function, lines 267-285)Parse a Content-type like header.
Return the main content-type and a dictionary of options.
-
catsoop.thirdparty.cgi.parse_multipart (
function, lines 220-251)Parse multipart input.
Arguments: fp : input file pdict: dictionary containing other parameters of content-type header encoding, errors: request encoding and error handler, passed to FieldStorage
Returns a dictionary just like parse_qs(): keys are the field names, each value is a list of values for that field. For non-file fields, the value is a list of strings.
- catsoop.thirdparty.cgi.print_arguments (
function, lines 1018-1023)
-
catsoop.thirdparty.cgi.print_directory (
function, lines 1005-1015)Dump the current directory as HTML.
-
catsoop.thirdparty.cgi.print_environ (
function, lines 976-985)Dump the shell environment as HTML.
-
catsoop.thirdparty.cgi.print_environ_usage (
function, lines 1026-1069)Dump a list of environment variables used by CGI as HTML.
- catsoop.thirdparty.cgi.print_exception (
function, lines 958-973)
-
catsoop.thirdparty.cgi.print_form (
function, lines 988-1002)Dump the contents of a form as HTML.
-
catsoop.thirdparty.cgi.test (
function, lines 915-955)Robust test CGI script, usable as main program.
Write minimal HTTP headers and dump all information provided to the script in HTML form.
- catsoop.thirdparty.cgi.valid_boundary (
function, lines 1076-1083)