Python Glossary Русский

all Python 3until 3.13Removed from the standard libraryterm

cgi

removed modules

Support for CGI scripts: parsing form fields and headers. Removed in Python 3.13 — use urllib.parse.parse_qs, email.message or a web framework.

Syntax

import cgi  # до 3.13

cgi parsed the request handed to a CGI script: cgi.FieldStorage() read form fields, cgi.parse_header() split a header into its value and parameters. PEP 594 dropped it: CGI, which starts a new process per request, was long ago replaced by WSGI and ASGI. How to port: a GET query string is parsed by urllib.parse.parse_qs (or parse_qsl), a header like 'text/html; charset=utf-8' by email.message.EmailMessage via get_content_type() and get_param(), a multipart/form-data body by the email package or the multipart library. Write new code on a framework (Flask, Django, FastAPI). The legacy-cgi package on PyPI restores the old module for code you cannot touch.

Examples

# Python 3.13+
try:
    import cgi
except ModuleNotFoundError as e:
    print(type(e).__name__)  # → ModuleNotFoundError
# Python 3.13+
from urllib.parse import parse_qs
from email.message import EmailMessage
print(parse_qs('name=Ann&tag=a&tag=b'))  # → {'name': ['Ann'], 'tag': ['a', 'b']}
msg = EmailMessage()
msg['Content-Type'] = 'text/html; charset=utf-8'
print(msg.get_content_type(), msg.get_param('charset'))  # → text/html utf-8
import cgi
print(cgi.parse_header('text/html; charset=utf-8'))
# → ('text/html', {'charset': 'utf-8'})

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy