jpayne@7
|
1 r"""
|
jpayne@7
|
2 The ``codes`` object defines a mapping from common names for HTTP statuses
|
jpayne@7
|
3 to their numerical codes, accessible either as attributes or as dictionary
|
jpayne@7
|
4 items.
|
jpayne@7
|
5
|
jpayne@7
|
6 Example::
|
jpayne@7
|
7
|
jpayne@7
|
8 >>> import requests
|
jpayne@7
|
9 >>> requests.codes['temporary_redirect']
|
jpayne@7
|
10 307
|
jpayne@7
|
11 >>> requests.codes.teapot
|
jpayne@7
|
12 418
|
jpayne@7
|
13 >>> requests.codes['\o/']
|
jpayne@7
|
14 200
|
jpayne@7
|
15
|
jpayne@7
|
16 Some codes have multiple names, and both upper- and lower-case versions of
|
jpayne@7
|
17 the names are allowed. For example, ``codes.ok``, ``codes.OK``, and
|
jpayne@7
|
18 ``codes.okay`` all correspond to the HTTP status code 200.
|
jpayne@7
|
19 """
|
jpayne@7
|
20
|
jpayne@7
|
21 from .structures import LookupDict
|
jpayne@7
|
22
|
jpayne@7
|
23 _codes = {
|
jpayne@7
|
24 # Informational.
|
jpayne@7
|
25 100: ("continue",),
|
jpayne@7
|
26 101: ("switching_protocols",),
|
jpayne@7
|
27 102: ("processing",),
|
jpayne@7
|
28 103: ("checkpoint",),
|
jpayne@7
|
29 122: ("uri_too_long", "request_uri_too_long"),
|
jpayne@7
|
30 200: ("ok", "okay", "all_ok", "all_okay", "all_good", "\\o/", "✓"),
|
jpayne@7
|
31 201: ("created",),
|
jpayne@7
|
32 202: ("accepted",),
|
jpayne@7
|
33 203: ("non_authoritative_info", "non_authoritative_information"),
|
jpayne@7
|
34 204: ("no_content",),
|
jpayne@7
|
35 205: ("reset_content", "reset"),
|
jpayne@7
|
36 206: ("partial_content", "partial"),
|
jpayne@7
|
37 207: ("multi_status", "multiple_status", "multi_stati", "multiple_stati"),
|
jpayne@7
|
38 208: ("already_reported",),
|
jpayne@7
|
39 226: ("im_used",),
|
jpayne@7
|
40 # Redirection.
|
jpayne@7
|
41 300: ("multiple_choices",),
|
jpayne@7
|
42 301: ("moved_permanently", "moved", "\\o-"),
|
jpayne@7
|
43 302: ("found",),
|
jpayne@7
|
44 303: ("see_other", "other"),
|
jpayne@7
|
45 304: ("not_modified",),
|
jpayne@7
|
46 305: ("use_proxy",),
|
jpayne@7
|
47 306: ("switch_proxy",),
|
jpayne@7
|
48 307: ("temporary_redirect", "temporary_moved", "temporary"),
|
jpayne@7
|
49 308: (
|
jpayne@7
|
50 "permanent_redirect",
|
jpayne@7
|
51 "resume_incomplete",
|
jpayne@7
|
52 "resume",
|
jpayne@7
|
53 ), # "resume" and "resume_incomplete" to be removed in 3.0
|
jpayne@7
|
54 # Client Error.
|
jpayne@7
|
55 400: ("bad_request", "bad"),
|
jpayne@7
|
56 401: ("unauthorized",),
|
jpayne@7
|
57 402: ("payment_required", "payment"),
|
jpayne@7
|
58 403: ("forbidden",),
|
jpayne@7
|
59 404: ("not_found", "-o-"),
|
jpayne@7
|
60 405: ("method_not_allowed", "not_allowed"),
|
jpayne@7
|
61 406: ("not_acceptable",),
|
jpayne@7
|
62 407: ("proxy_authentication_required", "proxy_auth", "proxy_authentication"),
|
jpayne@7
|
63 408: ("request_timeout", "timeout"),
|
jpayne@7
|
64 409: ("conflict",),
|
jpayne@7
|
65 410: ("gone",),
|
jpayne@7
|
66 411: ("length_required",),
|
jpayne@7
|
67 412: ("precondition_failed", "precondition"),
|
jpayne@7
|
68 413: ("request_entity_too_large",),
|
jpayne@7
|
69 414: ("request_uri_too_large",),
|
jpayne@7
|
70 415: ("unsupported_media_type", "unsupported_media", "media_type"),
|
jpayne@7
|
71 416: (
|
jpayne@7
|
72 "requested_range_not_satisfiable",
|
jpayne@7
|
73 "requested_range",
|
jpayne@7
|
74 "range_not_satisfiable",
|
jpayne@7
|
75 ),
|
jpayne@7
|
76 417: ("expectation_failed",),
|
jpayne@7
|
77 418: ("im_a_teapot", "teapot", "i_am_a_teapot"),
|
jpayne@7
|
78 421: ("misdirected_request",),
|
jpayne@7
|
79 422: ("unprocessable_entity", "unprocessable"),
|
jpayne@7
|
80 423: ("locked",),
|
jpayne@7
|
81 424: ("failed_dependency", "dependency"),
|
jpayne@7
|
82 425: ("unordered_collection", "unordered"),
|
jpayne@7
|
83 426: ("upgrade_required", "upgrade"),
|
jpayne@7
|
84 428: ("precondition_required", "precondition"),
|
jpayne@7
|
85 429: ("too_many_requests", "too_many"),
|
jpayne@7
|
86 431: ("header_fields_too_large", "fields_too_large"),
|
jpayne@7
|
87 444: ("no_response", "none"),
|
jpayne@7
|
88 449: ("retry_with", "retry"),
|
jpayne@7
|
89 450: ("blocked_by_windows_parental_controls", "parental_controls"),
|
jpayne@7
|
90 451: ("unavailable_for_legal_reasons", "legal_reasons"),
|
jpayne@7
|
91 499: ("client_closed_request",),
|
jpayne@7
|
92 # Server Error.
|
jpayne@7
|
93 500: ("internal_server_error", "server_error", "/o\\", "✗"),
|
jpayne@7
|
94 501: ("not_implemented",),
|
jpayne@7
|
95 502: ("bad_gateway",),
|
jpayne@7
|
96 503: ("service_unavailable", "unavailable"),
|
jpayne@7
|
97 504: ("gateway_timeout",),
|
jpayne@7
|
98 505: ("http_version_not_supported", "http_version"),
|
jpayne@7
|
99 506: ("variant_also_negotiates",),
|
jpayne@7
|
100 507: ("insufficient_storage",),
|
jpayne@7
|
101 509: ("bandwidth_limit_exceeded", "bandwidth"),
|
jpayne@7
|
102 510: ("not_extended",),
|
jpayne@7
|
103 511: ("network_authentication_required", "network_auth", "network_authentication"),
|
jpayne@7
|
104 }
|
jpayne@7
|
105
|
jpayne@7
|
106 codes = LookupDict(name="status_codes")
|
jpayne@7
|
107
|
jpayne@7
|
108
|
jpayne@7
|
109 def _init():
|
jpayne@7
|
110 for code, titles in _codes.items():
|
jpayne@7
|
111 for title in titles:
|
jpayne@7
|
112 setattr(codes, title, code)
|
jpayne@7
|
113 if not title.startswith(("\\", "/")):
|
jpayne@7
|
114 setattr(codes, title.upper(), code)
|
jpayne@7
|
115
|
jpayne@7
|
116 def doc(code):
|
jpayne@7
|
117 names = ", ".join(f"``{n}``" for n in _codes[code])
|
jpayne@7
|
118 return "* %d: %s" % (code, names)
|
jpayne@7
|
119
|
jpayne@7
|
120 global __doc__
|
jpayne@7
|
121 __doc__ = (
|
jpayne@7
|
122 __doc__ + "\n" + "\n".join(doc(code) for code in sorted(_codes))
|
jpayne@7
|
123 if __doc__ is not None
|
jpayne@7
|
124 else None
|
jpayne@7
|
125 )
|
jpayne@7
|
126
|
jpayne@7
|
127
|
jpayne@7
|
128 _init()
|