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