Skip to content
Myr

status_codes.py

This file

Path

httpcore/status_codes.py

123 lines · 4.1 KiB · Python

Last changed by

Rollin'Tom Christiebefore adoption8a29a0a

History of this file

Take it

Rollin'Tom Christiebefore adoption8a29a0a

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