Skip to content

Commit e4db769

Browse files
Issue #21793: Added http.HTTPStatus enums (i.e. HTTPStatus.OK,
HTTPStatus.NOT_FOUND). Patch by Demian Brecht.
1 parent ab47932 commit e4db769

File tree

6 files changed

+353
-216
lines changed

6 files changed

+353
-216
lines changed

Doc/library/http.rst

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,130 @@
11
:mod:`http` --- HTTP modules
22
============================
33

4-
``http`` is a package that collects several modules for working with the
4+
.. module:: http
5+
:synopsis: HTTP status codes and messages
6+
7+
.. index::
8+
pair: HTTP; protocol
9+
single: HTTP; http (standard module)
10+
11+
**Source code:** :source:`Lib/http/__init__.py`
12+
13+
:mod:`http` is a also package that collects several modules for working with the
514
HyperText Transfer Protocol:
615

716
* :mod:`http.client` is a low-level HTTP protocol client; for high-level URL
817
opening use :mod:`urllib.request`
918
* :mod:`http.server` contains basic HTTP server classes based on :mod:`socketserver`
1019
* :mod:`http.cookies` has utilities for implementing state management with cookies
1120
* :mod:`http.cookiejar` provides persistence of cookies
21+
22+
:mod:`http` is also a module that defines a number of HTTP status codes and
23+
associated messages through the :class:`http.HTTPStatus` enum:
24+
25+
.. class:: HTTPStatus
26+
27+
A subclass of :class:`enum.IntEnum` that defines a set of HTTP status codes,
28+
reason phrases and long descriptions written in English.
29+
30+
Usage::
31+
32+
>>> from http import HTTPStatus
33+
>>> HTTPStatus.OK
34+
<HTTPStatus.OK: 200>
35+
>>> HTTPStatus.OK == 200
36+
True
37+
>>> http.HTTPStatus.OK.value
38+
200
39+
>>> HTTPStatus.OK.phrase
40+
'OK'
41+
>>> HTTPStatus.OK.description
42+
'Request fulfilled, document follows'
43+
>>> list(HTTPStatus)
44+
[<HTTPStatus.CONTINUE: 100>, <HTTPStatus.SWITCHING_PROTOCOLS: 101>, ...]
45+
46+
.. versionadded:: 3.5
47+
Added the *HTTPStatus* Enum
48+
49+
The supported HTTP status codes are:
50+
51+
=== ==============================
52+
100 Continue
53+
101 Switching Protocols
54+
102 Processing
55+
200 OK
56+
201 Created
57+
202 Accepted
58+
203 Non-Authoritative Information
59+
204 No Content
60+
205 Reset Content
61+
206 Partial Content
62+
207 Multi-Status
63+
208 Already Reported
64+
226 IM Used
65+
300 Multiple Choices
66+
301 Moved Permanently
67+
302 Found
68+
303 See Other
69+
304 Not Modified
70+
305 Use Proxy
71+
306 Switch Proxy
72+
307 Temporary Redirect
73+
308 Permanent Redirect
74+
400 Bad Request
75+
401 Unauthorized
76+
402 Payment Required
77+
403 Forbidden
78+
404 Not Found
79+
405 Method Not Allowed
80+
406 Not Acceptable
81+
407 Proxy Authentication Required
82+
408 Request Timeout
83+
409 Conflict
84+
410 Gone
85+
411 Length Required
86+
412 Precondition Failed
87+
413 Request Entity Too Large
88+
414 Request URI Too Long
89+
415 Unsupported Media Type
90+
416 Request Range Not Satisfiable
91+
417 Expectation Failed
92+
418 I'm a teapot
93+
419 Authentication Timeout
94+
420 Method Failure *(Spring framework)*
95+
422 Unprocessable Entity
96+
423 Locked
97+
424 Failed Dependency
98+
426 Upgrade Required
99+
428 Precondition Required
100+
429 Too Many Requests
101+
431 Request Header Field Too Large
102+
440 Login Timeout *(Microsoft)*
103+
444 No Response *(Nginx)*
104+
449 Retry With *(Microsoft)*
105+
450 Blocked By Windows Parental Controls *(Microsoft)*
106+
494 Request Header Too Large *(Nginx)*
107+
495 Cert Error *(Nginx)*
108+
496 No Cert *(Nginx)*
109+
497 HTTP To HTTPS *(Nginx)*
110+
499 Client Closed Request *(Nginx)*
111+
500 Internal Server Error
112+
501 Not Implemented
113+
502 Bad Gateway
114+
503 Service Unavailable
115+
504 Gateway Timeout
116+
505 HTTP Version Not Supported
117+
506 Variant Also Negotiates
118+
507 Insufficient Storage
119+
508 Loop Detected
120+
509 Bandwidth Limit Exceeded
121+
510 Not Extended
122+
511 Network Authentication Required
123+
520 Origin Error *(CloudFlare)*
124+
521 Web Server Is Down *(CloudFlare)*
125+
522 Connection Timed Out *(CloudFlare)*
126+
523 Proxy Declined Request *(CloudFlare)*
127+
524 A Timeout Occurred *(CloudFlare)*
128+
598 Network Read Timeout Error
129+
599 Network Connect Timeout Error
130+
=== ==============================

Doc/whatsnew/3.5.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ Changes in the Python API
451451
**without** caching ``None`` in :data:`sys.path_importer_cache` which is
452452
different than the typical case (:issue:`22834`).
453453

454+
* HTTP status code and messages from `http.client` and `http.server` were
455+
refactored into a common :class:`~http.HTTPStatus` enum. The values in
456+
`http.client` and `http.server` remain available for backwards compatibility.
457+
(Contributed by Demian Brecht in :issue:`21793`.)
458+
454459
Changes in the C API
455460
--------------------
456461

Lib/http/__init__.py

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,164 @@
1-
# This directory is a Python package.
1+
from enum import IntEnum
2+
3+
__all__ = ['HTTPStatus']
4+
5+
class HTTPStatus(IntEnum):
6+
"""HTTP status codes and reason phrases
7+
8+
Status codes from the following RFCs are all observed:
9+
10+
* RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616
11+
* RFC 6585: Additional HTTP Status Codes
12+
* RFC 3229: Delta encoding in HTTP
13+
* RFC 4918: HTTP Extensions for WebDAV, obsoletes 2518
14+
* RFC 5842: Binding Extensions to WebDAV
15+
* RFC 7238: Permanent Redirect
16+
* RFC 2324: Hyper Text Coffee Pot Control Protocol (HTCPCP/1.0)
17+
* RFC 2295: Transparent Content Negotiation in HTTP
18+
* RFC 2774: An HTTP Extension Framework
19+
20+
Non-standard vendor codes include:
21+
22+
* Spring framework: 420
23+
* Nginx: 444, 494, 495, 496, 497, 499
24+
* Microsoft: 440, 449, 450
25+
* Cloudflare: 520, 521, 522, 523, 524, 598, 599
26+
"""
27+
def __new__(cls, value, phrase, description=''):
28+
obj = int.__new__(cls, value)
29+
obj._value_ = value
30+
31+
obj.phrase = phrase
32+
obj.description = description
33+
return obj
34+
35+
# informational
36+
CONTINUE = 100, 'Continue', 'Request received, please continue'
37+
SWITCHING_PROTOCOLS = (101, 'Switching Protocols',
38+
'Switching to new protocol; obey Upgrade header')
39+
PROCESSING = 102, 'Processing'
40+
41+
# success
42+
OK = 200, 'OK', 'Request fulfilled, document follows'
43+
CREATED = 201, 'Created', 'Document created, URL follows'
44+
ACCEPTED = (202, 'Accepted',
45+
'Request accepted, processing continues off-line')
46+
NON_AUTHORITATIVE_INFORMATION = (203,
47+
'Non-Authoritative Information', 'Request fulfilled from cache')
48+
NO_CONTENT = 204, 'No Content', 'Request fulfilled, nothing follows'
49+
RESET_CONTENT = 205, 'Reset Content', 'Clear input form for further input'
50+
PARTIAL_CONTENT = 206, 'Partial Content', 'Partial content follows'
51+
MULTI_STATUS = 207, 'Multi-Status'
52+
ALREADY_REPORTED = 208, 'Already Reported'
53+
IM_USED = 226, 'IM Used'
54+
55+
# redirection
56+
MULTIPLE_CHOICES = (300, 'Multiple Choices',
57+
'Object has several resources -- see URI list')
58+
MOVED_PERMANENTLY = (301, 'Moved Permanently',
59+
'Object moved permanently -- see URI list')
60+
FOUND = 302, 'Found', 'Object moved temporarily -- see URI list'
61+
SEE_OTHER = 303, 'See Other', 'Object moved -- see Method and URL list'
62+
NOT_MODIFIED = (304, 'Not Modified',
63+
'Document has not changed since given time')
64+
USE_PROXY = (305, 'Use Proxy',
65+
'You must use proxy specified in Location to access this resource')
66+
SWITCH_PROXY = 306, 'Switch Proxy'
67+
TEMPORARY_REDIRECT = (307, 'Temporary Redirect',
68+
'Object moved temporarily -- see URI list')
69+
PERMANENT_REDIRECT = (308, 'Permanent Redirect',
70+
'Object moved temporarily -- see URI list')
71+
72+
# client error
73+
BAD_REQUEST = (400, 'Bad Request',
74+
'Bad request syntax or unsupported method')
75+
UNAUTHORIZED = (401, 'Unauthorized',
76+
'No permission -- see authorization schemes')
77+
PAYMENT_REQUIRED = (402, 'Payment Required',
78+
'No payment -- see charging schemes')
79+
FORBIDDEN = (403, 'Forbidden',
80+
'Request forbidden -- authorization will not help')
81+
NOT_FOUND = (404, 'Not Found',
82+
'Nothing matches the given URI')
83+
METHOD_NOT_ALLOWED = (405, 'Method Not Allowed',
84+
'Specified method is invalid for this resource')
85+
NOT_ACCEPTABLE = (406, 'Not Acceptable',
86+
'URI not available in preferred format')
87+
PROXY_AUTHENTICATION_REQUIRED = (407,
88+
'Proxy Authentication Required',
89+
'You must authenticate with this proxy before proceeding')
90+
REQUEST_TIMEOUT = (408, 'Request Timeout',
91+
'Request timed out; try again later')
92+
CONFLICT = 409, 'Conflict', 'Request conflict'
93+
GONE = (410, 'Gone',
94+
'URI no longer exists and has been permanently removed')
95+
LENGTH_REQUIRED = (411, 'Length Required',
96+
'Client must specify Content-Length')
97+
PRECONDITION_FAILED = (412, 'Precondition Failed',
98+
'Precondition in headers is false')
99+
REQUEST_ENTITY_TOO_LARGE = (413, 'Request Entity Too Large',
100+
'Entity is too large')
101+
REQUEST_URI_TOO_LONG = (414, 'Request-URI Too Long',
102+
'URI is too long')
103+
UNSUPPORTED_MEDIA_TYPE = (415, 'Unsupported Media Type',
104+
'Entity body in unsupported format')
105+
REQUEST_RANGE_NOT_SATISFIABLE = (416,
106+
'Request Range Not Satisfiable',
107+
'Cannot satisfy request range')
108+
EXPECTATION_FAILED = (417, 'Expectation Failed',
109+
'Expect condition could not be satisfied')
110+
IM_A_TEAPOT = 418, 'I\'m a teapot'
111+
AUTHENTICATION_TIMEOUT = 419, 'Authentication Timeout'
112+
METHOD_FAILURE = 420, 'Method Failure' # Spring framework
113+
UNPROCESSABLE_ENTITY = 422, 'Unprocessable Entity'
114+
LOCKED = 423, 'Locked'
115+
FAILED_DEPENDENCY = 424, 'Failed Dependency'
116+
UPGRADE_REQUIRED = 426, 'Upgrade Required'
117+
PRECONDITION_REQUIRED = (428, 'Precondition Required',
118+
'The origin server requires the request to be conditional')
119+
TOO_MANY_REQUESTS = (429, 'Too Many Requests',
120+
'The user has sent too many requests in '
121+
'a given amount of time ("rate limiting")')
122+
REQUEST_HEADER_FIELD_TOO_LARGE = (431,
123+
'Request Header Field Too Large',
124+
'The server is unwilling to process the request because its header '
125+
'fields are too large')
126+
LOGIN_TIMEOUT = 440, 'Login Timeout' # microsoft
127+
NO_RESPONSE = 444, 'No Response' # nginx
128+
RETRY_WITH = 449, 'Retry With' # microsoft
129+
BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS = (450,
130+
'Blocked By Windows Parental Controls') # microsoft
131+
REQUEST_HEADER_TOO_LARGE = 494, 'Request Header Too Large' # nginx
132+
CERT_ERROR = 495, 'Cert Error' # nginx
133+
NO_CERT = 496, 'No Cert' # nginx
134+
HTTP_TO_HTTPS = 497, 'HTTP To HTTPS' # nginx
135+
CLIENT_CLOSED_REQUEST = 499, 'Client Closed Request' # nginx
136+
137+
# server errors
138+
INTERNAL_SERVER_ERROR = (500, 'Internal Server Error',
139+
'Server got itself in trouble')
140+
NOT_IMPLEMENTED = (501, 'Not Implemented',
141+
'Server does not support this operation')
142+
BAD_GATEWAY = (502, 'Bad Gateway',
143+
'Invalid responses from another server/proxy')
144+
SERVICE_UNAVAILABLE = (503, 'Service Unavailable',
145+
'The server cannot process the request due to a high load')
146+
GATEWAY_TIMEOUT = (504, 'Gateway Timeout',
147+
'The gateway server did not receive a timely response')
148+
HTTP_VERSION_NOT_SUPPORTED = (505, 'HTTP Version Not Supported',
149+
'Cannot fulfill request')
150+
VARIANT_ALSO_NEGOTIATES = 506, 'Variant Also Negotiates'
151+
INSUFFICIENT_STORAGE = 507, 'Insufficient Storage'
152+
LOOP_DETECTED = 508, 'Loop Detected'
153+
BANDWIDTH_LIMIT_EXCEEDED = 509, 'Bandwidth Limit Exceeded'
154+
NOT_EXTENDED = 510, 'Not Extended'
155+
NETWORK_AUTHENTICATION_REQUIRED = (511,
156+
'Network Authentication Required',
157+
'The client needs to authenticate to gain network access')
158+
ORIGIN_ERROR = 520, 'Origin Error' # cloudflare
159+
WEB_SERVER_IS_DOWN = 521, 'Web Server Is Down' # cloudflare
160+
CONNECTON_TIMED_OUT = 522, 'Connection Timed Out' # cloudflare
161+
PROXY_DECLINED_REQUEST = 523, 'Proxy Declined Request' # cloudflare
162+
A_TIMEOUT_OCCURRED = 524, 'A Timeout Occurred', '' # cloudflare
163+
NETWORK_READ_TIMEOUT_ERROR = 598, 'Network Read Timeout Error'
164+
NETWORK_CONNECT_TIMEOUT_ERROR = 599, 'Network Connect Timeout Error'

0 commit comments

Comments
 (0)