status_codes.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. Example::
  6. >>> import requests
  7. >>> requests.codes['temporary_redirect']
  8. 307
  9. >>> requests.codes.teapot
  10. 418
  11. >>> requests.codes['\o/']
  12. 200
  13. Some codes have multiple names, and both upper- and lower-case versions of
  14. the names are allowed. For example, ``codes.ok``, ``codes.OK``, and
  15. ``codes.okay`` all correspond to the HTTP status code 200.
  16. """
  17. from .structures import LookupDict
  18. _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. ), # "resume" and "resume_incomplete" to be removed in 3.0
  49. # 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. }
  100. codes = LookupDict(name="status_codes")
  101. def _init():
  102. for code, titles in _codes.items():
  103. for title in titles:
  104. setattr(codes, title, code)
  105. if not title.startswith(("\\", "/")):
  106. setattr(codes, title.upper(), code)
  107. def doc(code):
  108. names = ", ".join(f"``{n}``" for n in _codes[code])
  109. return "* %d: %s" % (code, names)
  110. global __doc__
  111. __doc__ = (
  112. __doc__ + "\n" + "\n".join(doc(code) for code in sorted(_codes))
  113. if __doc__ is not None
  114. else None
  115. )
  116. _init()