# pydoc > cgi

---
type: CommandReference
command: cgi
mode: pydoc
section: ""
source: pydoc3
---

## Quick Reference

- `cgi.FieldStorage()` — parse form data from environ or stdin
- `form.getvalue('field')` — get a single field value (string or bytes)
- `form.getfirst('field')` — get the first value for a field
- `form.getlist('field')` — get list of all values for a field
- `cgi.parse()` — parse query string into a list of (key, value) tuples
- `cgi.parse_multipart(fp, pdict)` — parse multipart input into a dict of lists
- `cgi.test()` — write minimal HTTP headers and dump all CGI info as HTML

## Name

Support module for CGI (Common Gateway Interface) scripts.

## Synopsis

python
import cgi
form = cgi.FieldStorage()
value = form.getvalue("fieldname")
## Options

### Functions

- `parse(fp=None, environ=os.environ, keep_blank_values=False, strict_parsing=False, separator='&')` — parse a query in the environment or from a file (default stdin). Returns a list of (key, value) tuples.
- `parse_header(header)` — parse a Content-type-like header. Returns the main content-type and a dictionary of options.
- `parse_multipart(fp, pdict, encoding='utf-8', errors='replace')` — parse multipart input. Returns a dictionary like `parse_qs()`: keys are field names, values are lists of strings.
- `print_arguments()` — (no description)
- `print_directory()` — dump the current directory as HTML.
- `print_environ()` — dump the shell environment as HTML.
- `print_environ_usage()` — dump a list of environment variables used by CGI as HTML.
- `print_exception()` — (no description)
- `print_form()` — dump the contents of a form as HTML.
- `test()` — robust test CGI script; writes minimal HTTP headers and dumps all information as HTML.

### Class `FieldStorage`

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

- `__init__(fp=None, headers=None, outerboundary=b'', environ=os.environ, 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.
- `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. Default opens a temporary file and deletes it on close.

Attributes on each item: `name`, `filename`, `value`, `file`, `type`, `type_options`, `disposition`, `disposition_options`, `headers`.

### Class `MiniFieldStorage`

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

- `__init__(name, value)` — constructor from field name and value.

## Examples

Minimal CGI script to read form data:

python
#!/usr/bin/env python3
import cgi
import cgitb; cgitb.enable()

print("Content-Type: text/html\n")
form = cgi.FieldStorage()
if "name" in form:
    name = form.getvalue("name")
    print(f"<h1>Hello, {name}!</h1>")
else:
    print("<form method='post'>")
    print("Name: <input type='text' name='name'>")
    print("<input type='submit'>")
    print("</form>")
## See Also

- [Python CGI module documentation](https://docs.python.org/3.10/library/cgi.html)