cgi - Support module for CGI (Common Gateway Interface) scripts.
| Use Case | Command | Description |
|---|---|---|
| Parse form data (GET/POST) | FieldStorage() | Read query string or multipart/form-data |
| Get a single field value | form.getvalue('name') | Return value for key, or default |
| Get first value of a field | form.getfirst('name') | Return first value (if multiple) |
| Get all values of a field | form.getlist('name') | Return list of values |
| Dump environment as HTML | print_environ() | Print shell environment variables |
| Dump form contents as HTML | print_form(form) | Print all form fields |
| Test CGI script | test() | Run robust test CGI (dumps all info) |
https://docs.python.org/3.10/library/cgi.html
The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.
This module defines a number of utilities for use by CGI scripts written in Python.
builtins.object
FieldStorage
MiniFieldStorage
class FieldStorage(builtins.object)
| FieldStorage(fp=None, headers=None, outerboundary=b'', environ=environ({...}), keep_blank_values=0, strict_parsing=0, limit=None, encoding='utf-8', errors='replace', max_num_fields=None, separator='&')
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 Nonefilename: 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 bytesfile: the file(-like) object from which you can read the data as bytes; None if the data is stored a simple stringtype: the content-type, or None if not specifiedtype_options: dictionary of options specified on the content-type linedisposition: content-disposition, or None if not specifieddisposition_options: dictionary of corresponding optionsheaders: a dictionary(-like) object (sometimes email.message.Message or a subclass thereof) containing all headersThe 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.
Methods defined here:
__bool__(self)__contains__(self, key) — Dictionary style __contains__ method.__del__(self)__enter__(self)__exit__(self, *args)__getattr__(self, name)__getitem__(self, key) — Dictionary style indexing.__init__(self, fp=None, headers=None, outerboundary=b'', environ=environ({...}), keep_blank_values=0, strict_parsing=0, limit=None, encoding='utf-8', errors='replace', max_num_fields=None, separator='&') — Constructor. Read multipart/* until last part.fp: file pointer; default: sys.stdin.buffer (not used when the request method is GET). Can be: 1. a TextIOWrapper object 2. an object whose read() and readline() methods return bytesheaders: header dictionary-like object; default: taken from environ as per CGI specouterboundary: terminating multipart boundary (for internal use only)environ: environment dictionary; default: os.environkeep_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 readencoding, 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().__iter__(self)__len__(self) — Dictionary style len(x) support.__repr__(self) — Return a printable representation.getfirst(self, key, default=None) — Return the first value received.getlist(self, key) — Return list of received values.getvalue(self, key, default=None) — Dictionary style get() method, including 'value' lookup.keys(self) — Dictionary style keys() method.make_file(self) — 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(self) — Internal: read binary data.read_lines(self) — Internal: read lines until EOF or outerboundary.read_lines_to_eof(self) — Internal: read lines until EOF.read_lines_to_outerboundary(self) — Internal: read lines until outerboundary. Data is read as bytes: boundaries and line ends must be converted to bytes for comparisons.read_multi(self, environ, keep_blank_values, strict_parsing) — Internal: read a part that is itself multipart.read_single(self) — Internal: read an atomic part.read_urlencoded(self) — Internal: read data in query string format.skip_lines(self) — Internal: skip lines until outer boundary if defined.Data descriptors defined here:
__dict__ — dictionary for instance variables (if defined)__weakref__ — list of weak references to the object (if defined)Data and other attributes defined here:
FieldStorageClass = Nonebufsize = 8192class MiniFieldStorage(builtins.object)
| MiniFieldStorage(name, value)
Like FieldStorage, for use when no file uploads are possible.
Methods defined here:
__init__(self, name, value) — Constructor from field name and value.__repr__(self) — Return printable representation.Data descriptors defined here:
__dict__ — dictionary for instance variables (if defined)__weakref__ — list of weak references to the object (if defined)Data and other attributes defined here:
disposition = Nonedisposition_options = {}file = Nonefilename = Noneheaders = {}list = Nonetype = Nonetype_options = {}parse(fp=None, environ=environ({...}), keep_blank_values=0, strict_parsing=0, separator='&') — 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 &.parse_header(line) — Parse a Content-type like header. Return the main content-type and a dictionary of options.parse_multipart(fp, pdict, encoding='utf-8', errors='replace', separator='&') — 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.print_arguments()print_directory() — Dump the current directory as HTML.print_environ(environ=environ({...})) — Dump the shell environment as HTML.print_environ_usage() — Dump a list of environment variables used by CGI as HTML.print_exception(type=None, value=None, tb=None, limit=None)print_form(form) — Dump the contents of a form as HTML.test(environ=environ({...})) — Robust test CGI script, usable as main program. Write minimal HTTP headers and dump all information provided to the script in HTML form.__all__ = ['MiniFieldStorage', 'FieldStorage', 'parse', 'parse_multipa...'
2.6
/usr/lib/python3.10/cgi.py
Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-01 16:50 @216.73.216.239
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)