You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.0 KiB

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