http — HTTP modules¶
Source code:Lib/http/__init__.py
http is a package that collects several modules for working with theHyperText Transfer Protocol:
http.clientis a low-level HTTP protocol client; for high-level URLopening useurllib.requesthttp.servercontains basic HTTP server classes based onsocketserverhttp.cookieshas utilities for implementing state management with cookieshttp.cookiejarprovides persistence of cookies
Thehttp module also defines the following enums that help you work with http related code:
- classhttp.HTTPStatus¶
Added in version 3.5.
A subclass of
enum.IntEnumthat defines a set of HTTP status codes,reason phrases and long descriptions written in English.Usage:
>>>fromhttpimportHTTPStatus>>>HTTPStatus.OKHTTPStatus.OK>>>HTTPStatus.OK==200True>>>HTTPStatus.OK.value200>>>HTTPStatus.OK.phrase'OK'>>>HTTPStatus.OK.description'Request fulfilled, document follows'>>>list(HTTPStatus)[HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...]
HTTP status codes¶
Supported,IANA-registered status codesavailable inhttp.HTTPStatus are:
Code | Enum Name | Details |
|---|---|---|
|
| HTTP SemanticsRFC 9110, Section 15.2.1 |
|
| HTTP SemanticsRFC 9110, Section 15.2.2 |
|
| WebDAVRFC 2518, Section 10.1 |
|
| An HTTP Status Code for Indicating HintsRFC 8297 |
|
| HTTP SemanticsRFC 9110, Section 15.3.1 |
|
| HTTP SemanticsRFC 9110, Section 15.3.2 |
|
| HTTP SemanticsRFC 9110, Section 15.3.3 |
|
| HTTP SemanticsRFC 9110, Section 15.3.4 |
|
| HTTP SemanticsRFC 9110, Section 15.3.5 |
|
| HTTP SemanticsRFC 9110, Section 15.3.6 |
|
| HTTP SemanticsRFC 9110, Section 15.3.7 |
|
| WebDAVRFC 4918, Section 11.1 |
|
| WebDAV Binding ExtensionsRFC 5842, Section 7.1 (Experimental) |
|
| Delta Encoding in HTTPRFC 3229, Section 10.4.1 |
|
| HTTP SemanticsRFC 9110, Section 15.4.1 |
|
| HTTP SemanticsRFC 9110, Section 15.4.2 |
|
| HTTP SemanticsRFC 9110, Section 15.4.3 |
|
| HTTP SemanticsRFC 9110, Section 15.4.4 |
|
| HTTP SemanticsRFC 9110, Section 15.4.5 |
|
| HTTP SemanticsRFC 9110, Section 15.4.6 |
|
| HTTP SemanticsRFC 9110, Section 15.4.8 |
|
| HTTP SemanticsRFC 9110, Section 15.4.9 |
|
| HTTP SemanticsRFC 9110, Section 15.5.1 |
|
| HTTP SemanticsRFC 9110, Section 15.5.2 |
|
| HTTP SemanticsRFC 9110, Section 15.5.3 |
|
| HTTP SemanticsRFC 9110, Section 15.5.4 |
|
| HTTP SemanticsRFC 9110, Section 15.5.5 |
|
| HTTP SemanticsRFC 9110, Section 15.5.6 |
|
| HTTP SemanticsRFC 9110, Section 15.5.7 |
|
| HTTP SemanticsRFC 9110, Section 15.5.8 |
|
| HTTP SemanticsRFC 9110, Section 15.5.9 |
|
| HTTP SemanticsRFC 9110, Section 15.5.10 |
|
| HTTP SemanticsRFC 9110, Section 15.5.11 |
|
| HTTP SemanticsRFC 9110, Section 15.5.12 |
|
| HTTP SemanticsRFC 9110, Section 15.5.13 |
|
| HTTP SemanticsRFC 9110, Section 15.5.14 |
|
| HTTP SemanticsRFC 9110, Section 15.5.15 |
|
| HTTP SemanticsRFC 9110, Section 15.5.16 |
|
| HTTP SemanticsRFC 9110, Section 15.5.17 |
|
| HTTP SemanticsRFC 9110, Section 15.5.18 |
|
| HTCPCP/1.0RFC 2324, Section 2.3.2 |
|
| HTTP SemanticsRFC 9110, Section 15.5.20 |
|
| HTTP SemanticsRFC 9110, Section 15.5.21 |
|
| WebDAVRFC 4918, Section 11.3 |
|
| WebDAVRFC 4918, Section 11.4 |
|
| Using Early Data in HTTPRFC 8470 |
|
| HTTP SemanticsRFC 9110, Section 15.5.22 |
|
| Additional HTTP Status CodesRFC 6585 |
|
| Additional HTTP Status CodesRFC 6585 |
|
| Additional HTTP Status CodesRFC 6585 |
|
| An HTTP Status Code to Report Legal ObstaclesRFC 7725 |
|
| HTTP SemanticsRFC 9110, Section 15.6.1 |
|
| HTTP SemanticsRFC 9110, Section 15.6.2 |
|
| HTTP SemanticsRFC 9110, Section 15.6.3 |
|
| HTTP SemanticsRFC 9110, Section 15.6.4 |
|
| HTTP SemanticsRFC 9110, Section 15.6.5 |
|
| HTTP SemanticsRFC 9110, Section 15.6.6 |
|
| Transparent Content Negotiation in HTTPRFC 2295, Section 8.1 (Experimental) |
|
| WebDAVRFC 4918, Section 11.5 |
|
| WebDAV Binding ExtensionsRFC 5842, Section 7.2 (Experimental) |
|
| An HTTP Extension FrameworkRFC 2774, Section 7 (Experimental) |
|
| Additional HTTP Status CodesRFC 6585, Section 6 |
In order to preserve backwards compatibility, enum values are also presentin thehttp.client module in the form of constants. The enum name isequal to the constant name (i.e.http.HTTPStatus.OK is also available ashttp.client.OK).
Changed in version 3.7:Added421MISDIRECTED_REQUEST status code.
Added in version 3.8:Added451UNAVAILABLE_FOR_LEGAL_REASONS status code.
Added in version 3.9:Added103EARLY_HINTS,418IM_A_TEAPOT and425TOO_EARLY status codes.
Changed in version 3.13:Implemented RFC9110 naming for status constants. Old constant names are preserved forbackwards compatibility:413REQUEST_ENTITY_TOO_LARGE,414REQUEST_URI_TOO_LONG,416REQUESTED_RANGE_NOT_SATISFIABLE and422UNPROCESSABLE_ENTITY.
HTTP status category¶
Added in version 3.12.
The enum values have several properties to indicate the HTTP status category:
Property | Indicates that | Details |
|---|---|---|
|
| HTTP SemanticsRFC 9110, Section 15 |
|
| HTTP SemanticsRFC 9110, Section 15 |
|
| HTTP SemanticsRFC 9110, Section 15 |
|
| HTTP SemanticsRFC 9110, Section 15 |
|
| HTTP SemanticsRFC 9110, Section 15 |
Usage:
>>>fromhttpimportHTTPStatus>>>HTTPStatus.OK.is_successTrue>>>HTTPStatus.OK.is_client_errorFalse
- classhttp.HTTPMethod¶
Added in version 3.11.
A subclass of
enum.StrEnumthat defines a set of HTTP methods and descriptions written in English.Usage:
>>>fromhttpimportHTTPMethod>>>>>>HTTPMethod.GET<HTTPMethod.GET>>>>HTTPMethod.GET=='GET'True>>>HTTPMethod.GET.value'GET'>>>HTTPMethod.GET.description'Retrieve the target.'>>>list(HTTPMethod)[<HTTPMethod.CONNECT>, <HTTPMethod.DELETE>, <HTTPMethod.GET>, <HTTPMethod.HEAD>, <HTTPMethod.OPTIONS>, <HTTPMethod.PATCH>, <HTTPMethod.POST>, <HTTPMethod.PUT>, <HTTPMethod.TRACE>]
HTTP methods¶
Supported,IANA-registered methodsavailable inhttp.HTTPMethod are:
Method | Enum Name | Details |
|---|---|---|
|
| HTTP SemanticsRFC 9110, Section 9.3.1 |
|
| HTTP SemanticsRFC 9110, Section 9.3.2 |
|
| HTTP SemanticsRFC 9110, Section 9.3.3 |
|
| HTTP SemanticsRFC 9110, Section 9.3.4 |
|
| HTTP SemanticsRFC 9110, Section 9.3.5 |
|
| HTTP SemanticsRFC 9110, Section 9.3.6 |
|
| HTTP SemanticsRFC 9110, Section 9.3.7 |
|
| HTTP SemanticsRFC 9110, Section 9.3.8 |
|
| HTTP/1.1RFC 5789 |