status_codes.py 4.1 KB

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