jpayne@7: r""" jpayne@7: The ``codes`` object defines a mapping from common names for HTTP statuses jpayne@7: to their numerical codes, accessible either as attributes or as dictionary jpayne@7: items. jpayne@7: jpayne@7: Example:: jpayne@7: jpayne@7: >>> import requests jpayne@7: >>> requests.codes['temporary_redirect'] jpayne@7: 307 jpayne@7: >>> requests.codes.teapot jpayne@7: 418 jpayne@7: >>> requests.codes['\o/'] jpayne@7: 200 jpayne@7: jpayne@7: Some codes have multiple names, and both upper- and lower-case versions of jpayne@7: the names are allowed. For example, ``codes.ok``, ``codes.OK``, and jpayne@7: ``codes.okay`` all correspond to the HTTP status code 200. jpayne@7: """ jpayne@7: jpayne@7: from .structures import LookupDict jpayne@7: jpayne@7: _codes = { jpayne@7: # Informational. jpayne@7: 100: ("continue",), jpayne@7: 101: ("switching_protocols",), jpayne@7: 102: ("processing",), jpayne@7: 103: ("checkpoint",), jpayne@7: 122: ("uri_too_long", "request_uri_too_long"), jpayne@7: 200: ("ok", "okay", "all_ok", "all_okay", "all_good", "\\o/", "✓"), jpayne@7: 201: ("created",), jpayne@7: 202: ("accepted",), jpayne@7: 203: ("non_authoritative_info", "non_authoritative_information"), jpayne@7: 204: ("no_content",), jpayne@7: 205: ("reset_content", "reset"), jpayne@7: 206: ("partial_content", "partial"), jpayne@7: 207: ("multi_status", "multiple_status", "multi_stati", "multiple_stati"), jpayne@7: 208: ("already_reported",), jpayne@7: 226: ("im_used",), jpayne@7: # Redirection. jpayne@7: 300: ("multiple_choices",), jpayne@7: 301: ("moved_permanently", "moved", "\\o-"), jpayne@7: 302: ("found",), jpayne@7: 303: ("see_other", "other"), jpayne@7: 304: ("not_modified",), jpayne@7: 305: ("use_proxy",), jpayne@7: 306: ("switch_proxy",), jpayne@7: 307: ("temporary_redirect", "temporary_moved", "temporary"), jpayne@7: 308: ( jpayne@7: "permanent_redirect", jpayne@7: "resume_incomplete", jpayne@7: "resume", jpayne@7: ), # "resume" and "resume_incomplete" to be removed in 3.0 jpayne@7: # Client Error. jpayne@7: 400: ("bad_request", "bad"), jpayne@7: 401: ("unauthorized",), jpayne@7: 402: ("payment_required", "payment"), jpayne@7: 403: ("forbidden",), jpayne@7: 404: ("not_found", "-o-"), jpayne@7: 405: ("method_not_allowed", "not_allowed"), jpayne@7: 406: ("not_acceptable",), jpayne@7: 407: ("proxy_authentication_required", "proxy_auth", "proxy_authentication"), jpayne@7: 408: ("request_timeout", "timeout"), jpayne@7: 409: ("conflict",), jpayne@7: 410: ("gone",), jpayne@7: 411: ("length_required",), jpayne@7: 412: ("precondition_failed", "precondition"), jpayne@7: 413: ("request_entity_too_large",), jpayne@7: 414: ("request_uri_too_large",), jpayne@7: 415: ("unsupported_media_type", "unsupported_media", "media_type"), jpayne@7: 416: ( jpayne@7: "requested_range_not_satisfiable", jpayne@7: "requested_range", jpayne@7: "range_not_satisfiable", jpayne@7: ), jpayne@7: 417: ("expectation_failed",), jpayne@7: 418: ("im_a_teapot", "teapot", "i_am_a_teapot"), jpayne@7: 421: ("misdirected_request",), jpayne@7: 422: ("unprocessable_entity", "unprocessable"), jpayne@7: 423: ("locked",), jpayne@7: 424: ("failed_dependency", "dependency"), jpayne@7: 425: ("unordered_collection", "unordered"), jpayne@7: 426: ("upgrade_required", "upgrade"), jpayne@7: 428: ("precondition_required", "precondition"), jpayne@7: 429: ("too_many_requests", "too_many"), jpayne@7: 431: ("header_fields_too_large", "fields_too_large"), jpayne@7: 444: ("no_response", "none"), jpayne@7: 449: ("retry_with", "retry"), jpayne@7: 450: ("blocked_by_windows_parental_controls", "parental_controls"), jpayne@7: 451: ("unavailable_for_legal_reasons", "legal_reasons"), jpayne@7: 499: ("client_closed_request",), jpayne@7: # Server Error. jpayne@7: 500: ("internal_server_error", "server_error", "/o\\", "✗"), jpayne@7: 501: ("not_implemented",), jpayne@7: 502: ("bad_gateway",), jpayne@7: 503: ("service_unavailable", "unavailable"), jpayne@7: 504: ("gateway_timeout",), jpayne@7: 505: ("http_version_not_supported", "http_version"), jpayne@7: 506: ("variant_also_negotiates",), jpayne@7: 507: ("insufficient_storage",), jpayne@7: 509: ("bandwidth_limit_exceeded", "bandwidth"), jpayne@7: 510: ("not_extended",), jpayne@7: 511: ("network_authentication_required", "network_auth", "network_authentication"), jpayne@7: } jpayne@7: jpayne@7: codes = LookupDict(name="status_codes") jpayne@7: jpayne@7: jpayne@7: def _init(): jpayne@7: for code, titles in _codes.items(): jpayne@7: for title in titles: jpayne@7: setattr(codes, title, code) jpayne@7: if not title.startswith(("\\", "/")): jpayne@7: setattr(codes, title.upper(), code) jpayne@7: jpayne@7: def doc(code): jpayne@7: names = ", ".join(f"``{n}``" for n in _codes[code]) jpayne@7: return "* %d: %s" % (code, names) jpayne@7: jpayne@7: global __doc__ jpayne@7: __doc__ = ( jpayne@7: __doc__ + "\n" + "\n".join(doc(code) for code in sorted(_codes)) jpayne@7: if __doc__ is not None jpayne@7: else None jpayne@7: ) jpayne@7: jpayne@7: jpayne@7: _init()