FieldStorage

FieldStorage.py

This module provides that latest version of the now deprecated standard Python cgi.FieldStorage class with a slight modification so that fields passed in the body of a POST request override any fields passed in the query string.

class WebUtils.FieldStorage.FieldStorage(fp=None, headers=None, outerboundary=b'', environ=None, keep_blank_values=False, strict_parsing=False, limit=None, encoding='utf-8', errors='replace', max_num_fields=None, separator='&')

Bases: object

Store a sequence of fields, reading multipart/form-data.

This is a slightly modified version of the FieldStorage class in the now deprecated cgi module of the standard library.

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 can be subclassed, 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.

Parameters in the query string which have not been sent via POST are appended to the field list. This is different from the behavior of Python versions before 2.6 which completely ignored the query string in POST request, but it’s also different from the behavior of the later Python versions which append values from the query string to values sent via POST for parameters with the same name. With other words, our FieldStorage class overrides the query string parameters with the parameters sent via POST.

FieldStorageClass = None
__init__(fp=None, headers=None, outerboundary=b'', environ=None, keep_blank_values=False, strict_parsing=False, limit=None, encoding='utf-8', errors='replace', max_num_fields=None, separator='&')

Constructor. Read multipart/* until last part. Arguments, all optional: fp: file pointer; default: sys.stdin.buffer

Not used when the request method is GET. Can be a TextIOWrapper object or an object whose read() and readline() methods return bytes.

headers: header dictionary-like object; default: taken from environ as per CGI spec

outerboundary: terminating multipart boundary (for internal use only)

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.

limit: used internally to read parts of multipart/form-data forms, to exit from the reading loop when reached. It is the difference between the form content-length and the number of bytes already read.

encoding, errors: the encoding and error handler used to decode the binary stream to strings. Must be the same as the charset defined for the page sending the form (content-type : meta http-equiv or header)

max_num_fields: int. If set, then __init__ throws a ValueError if there are more than n fields read by parse_qsl().

bufsize = 8192
getfirst(key, default=None)

Return the first value received.

getlist(key)

Return list of received values.

getvalue(key, default=None)

Dictionary style get() method, including ‘value’ lookup.

keys()

Dictionary style keys() method.

make_file()

Overridable: return a readable & writable file.

The file will be used as follows: - data is written to it - seek(0) - data is read from it The file is opened in binary mode for files, in text mode for other fields. This version opens a temporary file for reading and writing, and immediately deletes (unlinks) it. The trick (on Unix!) is that the file can still be used, but it can’t be opened by another process, and it will automatically be deleted when it is closed or when the current process terminates. If you want a more permanent file, you derive a class which overrides this method. If you want a visible temporary file that is nevertheless automatically deleted when the script terminates, try defining a __del__ method in a derived class which unlinks the temporary files you have created.

read_binary()

Internal: read binary data.

read_lines()

Internal: read lines until EOF or outerboundary.

read_lines_to_eof()

Internal: read lines until EOF.

read_lines_to_outerboundary()

Internal: read lines until outerboundary.

Data is read as bytes: boundaries and line ends must be converted to bytes for comparisons.

read_multi(environ, keep_blank_values, strict_parsing)

Internal: read a part that is itself multipart.

read_single()

Internal: read an atomic part.

read_urlencoded()

Internal: read data in query string format.

skip_lines()

Internal: skip lines until outer boundary if defined.

class WebUtils.FieldStorage.MiniFieldStorage(name, value)

Bases: object

Like FieldStorage, for use when no file uploads are possible.

__init__(name, value)

Constructor from field name and value.

disposition = None
disposition_options = {}
file = None
filename = None
headers = {}
list = None
type = None
type_options = {}
WebUtils.FieldStorage.hasSeparator()

Check whether the separator parameter is supported.

WebUtils.FieldStorage.isBinaryType(ctype, pdict=None)

“Check whether the given MIME type uses binary data.

WebUtils.FieldStorage.parse_header(line)

Parse a Content-type like header.

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

WebUtils.FieldStorage.valid_boundary(s)