2012-10-06 08:49:34 -03:00
|
|
|
:mod:`http` --- HTTP modules
|
|
|
|
============================
|
|
|
|
|
2014-12-23 10:28:28 -04:00
|
|
|
.. module:: http
|
|
|
|
:synopsis: HTTP status codes and messages
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
**Source code:** :source:`Lib/http/__init__.py`
|
|
|
|
|
2014-12-23 10:28:28 -04:00
|
|
|
.. index::
|
|
|
|
pair: HTTP; protocol
|
|
|
|
single: HTTP; http (standard module)
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
--------------
|
2014-12-23 10:28:28 -04:00
|
|
|
|
2015-01-20 00:30:46 -04:00
|
|
|
:mod:`http` is a package that collects several modules for working with the
|
2012-10-06 08:49:34 -03:00
|
|
|
HyperText Transfer Protocol:
|
|
|
|
|
|
|
|
* :mod:`http.client` is a low-level HTTP protocol client; for high-level URL
|
|
|
|
opening use :mod:`urllib.request`
|
|
|
|
* :mod:`http.server` contains basic HTTP server classes based on :mod:`socketserver`
|
|
|
|
* :mod:`http.cookies` has utilities for implementing state management with cookies
|
|
|
|
* :mod:`http.cookiejar` provides persistence of cookies
|
2014-12-23 10:28:28 -04:00
|
|
|
|
2022-05-05 19:39:02 -03:00
|
|
|
|
|
|
|
The :mod:`http` module also defines the following enums that help you work with http related code:
|
2014-12-23 10:28:28 -04:00
|
|
|
|
|
|
|
.. class:: HTTPStatus
|
|
|
|
|
2014-12-25 08:14:09 -04:00
|
|
|
.. versionadded:: 3.5
|
|
|
|
|
2014-12-23 10:28:28 -04:00
|
|
|
A subclass of :class:`enum.IntEnum` that defines a set of HTTP status codes,
|
|
|
|
reason phrases and long descriptions written in English.
|
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
|
|
|
>>> from http import HTTPStatus
|
|
|
|
>>> HTTPStatus.OK
|
2021-03-31 01:17:26 -03:00
|
|
|
HTTPStatus.OK
|
2014-12-23 10:28:28 -04:00
|
|
|
>>> HTTPStatus.OK == 200
|
|
|
|
True
|
2019-11-05 19:29:33 -04:00
|
|
|
>>> HTTPStatus.OK.value
|
2014-12-23 10:28:28 -04:00
|
|
|
200
|
|
|
|
>>> HTTPStatus.OK.phrase
|
|
|
|
'OK'
|
|
|
|
>>> HTTPStatus.OK.description
|
|
|
|
'Request fulfilled, document follows'
|
|
|
|
>>> list(HTTPStatus)
|
2021-03-31 01:17:26 -03:00
|
|
|
[HTTPStatus.CONTINUE, HTTPStatus.SWITCHING_PROTOCOLS, ...]
|
2014-12-23 10:28:28 -04:00
|
|
|
|
2015-01-20 00:30:46 -04:00
|
|
|
.. _http-status-codes:
|
|
|
|
|
|
|
|
HTTP status codes
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Supported,
|
2022-05-05 19:39:02 -03:00
|
|
|
`IANA-registered status codes <https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>`_
|
|
|
|
available in :class:`http.HTTPStatus` are:
|
2015-01-20 00:30:46 -04:00
|
|
|
|
|
|
|
======= =================================== ==================================================================
|
|
|
|
Code Enum Name Details
|
|
|
|
======= =================================== ==================================================================
|
2024-04-13 11:33:20 -03:00
|
|
|
``100`` ``CONTINUE`` HTTP Semantics :rfc:`9110`, Section 15.2.1
|
|
|
|
``101`` ``SWITCHING_PROTOCOLS`` HTTP Semantics :rfc:`9110`, Section 15.2.2
|
2015-01-20 00:30:46 -04:00
|
|
|
``102`` ``PROCESSING`` WebDAV :rfc:`2518`, Section 10.1
|
2020-03-14 11:12:01 -03:00
|
|
|
``103`` ``EARLY_HINTS`` An HTTP Status Code for Indicating Hints :rfc:`8297`
|
2024-04-13 11:33:20 -03:00
|
|
|
``200`` ``OK`` HTTP Semantics :rfc:`9110`, Section 15.3.1
|
|
|
|
``201`` ``CREATED`` HTTP Semantics :rfc:`9110`, Section 15.3.2
|
|
|
|
``202`` ``ACCEPTED`` HTTP Semantics :rfc:`9110`, Section 15.3.3
|
|
|
|
``203`` ``NON_AUTHORITATIVE_INFORMATION`` HTTP Semantics :rfc:`9110`, Section 15.3.4
|
|
|
|
``204`` ``NO_CONTENT`` HTTP Semantics :rfc:`9110`, Section 15.3.5
|
|
|
|
``205`` ``RESET_CONTENT`` HTTP Semantics :rfc:`9110`, Section 15.3.6
|
|
|
|
``206`` ``PARTIAL_CONTENT`` HTTP Semantics :rfc:`9110`, Section 15.3.7
|
2015-01-20 00:30:46 -04:00
|
|
|
``207`` ``MULTI_STATUS`` WebDAV :rfc:`4918`, Section 11.1
|
|
|
|
``208`` ``ALREADY_REPORTED`` WebDAV Binding Extensions :rfc:`5842`, Section 7.1 (Experimental)
|
|
|
|
``226`` ``IM_USED`` Delta Encoding in HTTP :rfc:`3229`, Section 10.4.1
|
2024-04-13 11:33:20 -03:00
|
|
|
``300`` ``MULTIPLE_CHOICES`` HTTP Semantics :rfc:`9110`, Section 15.4.1
|
|
|
|
``301`` ``MOVED_PERMANENTLY`` HTTP Semantics :rfc:`9110`, Section 15.4.2
|
|
|
|
``302`` ``FOUND`` HTTP Semantics :rfc:`9110`, Section 15.4.3
|
|
|
|
``303`` ``SEE_OTHER`` HTTP Semantics :rfc:`9110`, Section 15.4.4
|
|
|
|
``304`` ``NOT_MODIFIED`` HTTP Semantics :rfc:`9110`, Section 15.4.5
|
|
|
|
``305`` ``USE_PROXY`` HTTP Semantics :rfc:`9110`, Section 15.4.6
|
|
|
|
``307`` ``TEMPORARY_REDIRECT`` HTTP Semantics :rfc:`9110`, Section 15.4.8
|
|
|
|
``308`` ``PERMANENT_REDIRECT`` HTTP Semantics :rfc:`9110`, Section 15.4.9
|
|
|
|
``400`` ``BAD_REQUEST`` HTTP Semantics :rfc:`9110`, Section 15.5.1
|
|
|
|
``401`` ``UNAUTHORIZED`` HTTP Semantics :rfc:`9110`, Section 15.5.2
|
|
|
|
``402`` ``PAYMENT_REQUIRED`` HTTP Semantics :rfc:`9110`, Section 15.5.3
|
|
|
|
``403`` ``FORBIDDEN`` HTTP Semantics :rfc:`9110`, Section 15.5.4
|
|
|
|
``404`` ``NOT_FOUND`` HTTP Semantics :rfc:`9110`, Section 15.5.5
|
|
|
|
``405`` ``METHOD_NOT_ALLOWED`` HTTP Semantics :rfc:`9110`, Section 15.5.6
|
|
|
|
``406`` ``NOT_ACCEPTABLE`` HTTP Semantics :rfc:`9110`, Section 15.5.7
|
|
|
|
``407`` ``PROXY_AUTHENTICATION_REQUIRED`` HTTP Semantics :rfc:`9110`, Section 15.5.8
|
|
|
|
``408`` ``REQUEST_TIMEOUT`` HTTP Semantics :rfc:`9110`, Section 15.5.9
|
|
|
|
``409`` ``CONFLICT`` HTTP Semantics :rfc:`9110`, Section 15.5.10
|
|
|
|
``410`` ``GONE`` HTTP Semantics :rfc:`9110`, Section 15.5.11
|
|
|
|
``411`` ``LENGTH_REQUIRED`` HTTP Semantics :rfc:`9110`, Section 15.5.12
|
|
|
|
``412`` ``PRECONDITION_FAILED`` HTTP Semantics :rfc:`9110`, Section 15.5.13
|
|
|
|
``413`` ``CONTENT_TOO_LARGE`` HTTP Semantics :rfc:`9110`, Section 15.5.14
|
|
|
|
``414`` ``URI_TOO_LONG`` HTTP Semantics :rfc:`9110`, Section 15.5.15
|
|
|
|
``415`` ``UNSUPPORTED_MEDIA_TYPE`` HTTP Semantics :rfc:`9110`, Section 15.5.16
|
|
|
|
``416`` ``RANGE_NOT_SATISFIABLE`` HTTP Semantics :rfc:`9110`, Section 15.5.17
|
|
|
|
``417`` ``EXPECTATION_FAILED`` HTTP Semantics :rfc:`9110`, Section 15.5.18
|
2020-03-15 09:24:23 -03:00
|
|
|
``418`` ``IM_A_TEAPOT`` HTCPCP/1.0 :rfc:`2324`, Section 2.3.2
|
2024-04-13 11:33:20 -03:00
|
|
|
``421`` ``MISDIRECTED_REQUEST`` HTTP Semantics :rfc:`9110`, Section 15.5.20
|
|
|
|
``422`` ``UNPROCESSABLE_CONTENT`` HTTP Semantics :rfc:`9110`, Section 15.5.21
|
2015-01-20 00:30:46 -04:00
|
|
|
``423`` ``LOCKED`` WebDAV :rfc:`4918`, Section 11.3
|
|
|
|
``424`` ``FAILED_DEPENDENCY`` WebDAV :rfc:`4918`, Section 11.4
|
2020-03-14 11:12:01 -03:00
|
|
|
``425`` ``TOO_EARLY`` Using Early Data in HTTP :rfc:`8470`
|
2024-04-13 11:33:20 -03:00
|
|
|
``426`` ``UPGRADE_REQUIRED`` HTTP Semantics :rfc:`9110`, Section 15.5.22
|
2015-01-20 00:30:46 -04:00
|
|
|
``428`` ``PRECONDITION_REQUIRED`` Additional HTTP Status Codes :rfc:`6585`
|
|
|
|
``429`` ``TOO_MANY_REQUESTS`` Additional HTTP Status Codes :rfc:`6585`
|
|
|
|
``431`` ``REQUEST_HEADER_FIELDS_TOO_LARGE`` Additional HTTP Status Codes :rfc:`6585`
|
2019-08-23 14:19:15 -03:00
|
|
|
``451`` ``UNAVAILABLE_FOR_LEGAL_REASONS`` An HTTP Status Code to Report Legal Obstacles :rfc:`7725`
|
2024-04-13 11:33:20 -03:00
|
|
|
``500`` ``INTERNAL_SERVER_ERROR`` HTTP Semantics :rfc:`9110`, Section 15.6.1
|
|
|
|
``501`` ``NOT_IMPLEMENTED`` HTTP Semantics :rfc:`9110`, Section 15.6.2
|
|
|
|
``502`` ``BAD_GATEWAY`` HTTP Semantics :rfc:`9110`, Section 15.6.3
|
|
|
|
``503`` ``SERVICE_UNAVAILABLE`` HTTP Semantics :rfc:`9110`, Section 15.6.4
|
|
|
|
``504`` ``GATEWAY_TIMEOUT`` HTTP Semantics :rfc:`9110`, Section 15.6.5
|
|
|
|
``505`` ``HTTP_VERSION_NOT_SUPPORTED`` HTTP Semantics :rfc:`9110`, Section 15.6.6
|
2015-01-20 00:30:46 -04:00
|
|
|
``506`` ``VARIANT_ALSO_NEGOTIATES`` Transparent Content Negotiation in HTTP :rfc:`2295`, Section 8.1 (Experimental)
|
|
|
|
``507`` ``INSUFFICIENT_STORAGE`` WebDAV :rfc:`4918`, Section 11.5
|
|
|
|
``508`` ``LOOP_DETECTED`` WebDAV Binding Extensions :rfc:`5842`, Section 7.2 (Experimental)
|
|
|
|
``510`` ``NOT_EXTENDED`` An HTTP Extension Framework :rfc:`2774`, Section 7 (Experimental)
|
|
|
|
``511`` ``NETWORK_AUTHENTICATION_REQUIRED`` Additional HTTP Status Codes :rfc:`6585`, Section 6
|
|
|
|
======= =================================== ==================================================================
|
|
|
|
|
|
|
|
In order to preserve backwards compatibility, enum values are also present
|
2015-01-20 02:02:28 -04:00
|
|
|
in the :mod:`http.client` module in the form of constants. The enum name is
|
|
|
|
equal to the constant name (i.e. ``http.HTTPStatus.OK`` is also available as
|
|
|
|
``http.client.OK``).
|
2017-10-26 15:49:19 -03:00
|
|
|
|
|
|
|
.. versionchanged:: 3.7
|
|
|
|
Added ``421 MISDIRECTED_REQUEST`` status code.
|
2019-08-23 14:19:15 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.8
|
|
|
|
Added ``451 UNAVAILABLE_FOR_LEGAL_REASONS`` status code.
|
2020-03-14 11:12:01 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.9
|
2020-03-15 09:24:23 -03:00
|
|
|
Added ``103 EARLY_HINTS``, ``418 IM_A_TEAPOT`` and ``425 TOO_EARLY`` status codes.
|
2022-05-05 19:39:02 -03:00
|
|
|
|
2024-04-13 11:33:20 -03:00
|
|
|
.. versionchanged:: 3.13
|
|
|
|
Implemented RFC9110 naming for status constants. Old constant names are preserved for
|
|
|
|
backwards compatibility.
|
|
|
|
|
2022-08-30 15:11:44 -03:00
|
|
|
HTTP status category
|
|
|
|
--------------------
|
|
|
|
|
2022-09-02 17:11:18 -03:00
|
|
|
.. versionadded:: 3.12
|
2022-08-30 15:11:44 -03:00
|
|
|
|
|
|
|
The enum values have several properties to indicate the HTTP status category:
|
|
|
|
|
2024-04-13 11:33:20 -03:00
|
|
|
==================== ======================== ======================================
|
2022-08-30 15:11:44 -03:00
|
|
|
Property Indicates that Details
|
2024-04-13 11:33:20 -03:00
|
|
|
==================== ======================== ======================================
|
|
|
|
``is_informational`` ``100 <= status <= 199`` HTTP Semantics :rfc:`9110`, Section 15
|
|
|
|
``is_success`` ``200 <= status <= 299`` HTTP Semantics :rfc:`9110`, Section 15
|
|
|
|
``is_redirection`` ``300 <= status <= 399`` HTTP Semantics :rfc:`9110`, Section 15
|
|
|
|
``is_client_error`` ``400 <= status <= 499`` HTTP Semantics :rfc:`9110`, Section 15
|
|
|
|
``is_server_error`` ``500 <= status <= 599`` HTTP Semantics :rfc:`9110`, Section 15
|
|
|
|
==================== ======================== ======================================
|
2022-08-30 15:11:44 -03:00
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
|
|
|
>>> from http import HTTPStatus
|
|
|
|
>>> HTTPStatus.OK.is_success
|
|
|
|
True
|
|
|
|
>>> HTTPStatus.OK.is_client_error
|
|
|
|
False
|
|
|
|
|
2022-05-05 19:39:02 -03:00
|
|
|
.. class:: HTTPMethod
|
|
|
|
|
|
|
|
.. versionadded:: 3.11
|
|
|
|
|
|
|
|
A subclass of :class:`enum.StrEnum` that defines a set of HTTP methods and descriptions written in English.
|
|
|
|
|
|
|
|
Usage::
|
|
|
|
|
|
|
|
>>> from http import HTTPMethod
|
2022-11-13 16:46:28 -04:00
|
|
|
>>>
|
|
|
|
>>> HTTPMethod.GET
|
|
|
|
<HTTPMethod.GET>
|
|
|
|
>>> HTTPMethod.GET == 'GET'
|
2022-05-05 19:39:02 -03:00
|
|
|
True
|
2022-11-13 16:46:28 -04:00
|
|
|
>>> HTTPMethod.GET.value
|
2022-05-05 19:39:02 -03:00
|
|
|
'GET'
|
2022-11-13 16:46:28 -04:00
|
|
|
>>> HTTPMethod.GET.description
|
|
|
|
'Retrieve the target.'
|
2022-05-05 19:39:02 -03:00
|
|
|
>>> list(HTTPMethod)
|
2022-11-13 16:46:28 -04:00
|
|
|
[<HTTPMethod.CONNECT>,
|
|
|
|
<HTTPMethod.DELETE>,
|
|
|
|
<HTTPMethod.GET>,
|
|
|
|
<HTTPMethod.HEAD>,
|
|
|
|
<HTTPMethod.OPTIONS>,
|
|
|
|
<HTTPMethod.PATCH>,
|
|
|
|
<HTTPMethod.POST>,
|
|
|
|
<HTTPMethod.PUT>,
|
|
|
|
<HTTPMethod.TRACE>]
|
2022-05-05 19:39:02 -03:00
|
|
|
|
|
|
|
.. _http-methods:
|
|
|
|
|
|
|
|
HTTP methods
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Supported,
|
|
|
|
`IANA-registered methods <https://www.iana.org/assignments/http-methods/http-methods.xhtml>`_
|
|
|
|
available in :class:`http.HTTPMethod` are:
|
|
|
|
|
|
|
|
=========== =================================== ==================================================================
|
|
|
|
Method Enum Name Details
|
|
|
|
=========== =================================== ==================================================================
|
2024-04-13 11:33:20 -03:00
|
|
|
``GET`` ``GET`` HTTP Semantics :rfc:`9110`, Section 9.3.1
|
|
|
|
``HEAD`` ``HEAD`` HTTP Semantics :rfc:`9110`, Section 9.3.2
|
|
|
|
``POST`` ``POST`` HTTP Semantics :rfc:`9110`, Section 9.3.3
|
|
|
|
``PUT`` ``PUT`` HTTP Semantics :rfc:`9110`, Section 9.3.4
|
|
|
|
``DELETE`` ``DELETE`` HTTP Semantics :rfc:`9110`, Section 9.3.5
|
|
|
|
``CONNECT`` ``CONNECT`` HTTP Semantics :rfc:`9110`, Section 9.3.6
|
|
|
|
``OPTIONS`` ``OPTIONS`` HTTP Semantics :rfc:`9110`, Section 9.3.7
|
|
|
|
``TRACE`` ``TRACE`` HTTP Semantics :rfc:`9110`, Section 9.3.8
|
2022-05-05 19:39:02 -03:00
|
|
|
``PATCH`` ``PATCH`` HTTP/1.1 :rfc:`5789`
|
|
|
|
=========== =================================== ==================================================================
|