您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

122 行
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # __
  3. # /__) _ _ _ _ _/ _
  4. # / ( (- (/ (/ (- _) / _)
  5. # /
  6. """
  7. Requests HTTP Library
  8. ~~~~~~~~~~~~~~~~~~~~~
  9. Requests is an HTTP library, written in Python, for human beings. Basic GET
  10. usage:
  11. >>> import requests
  12. >>> r = requests.get('https://www.python.org')
  13. >>> r.status_code
  14. 200
  15. >>> 'Python is a programming language' in r.content
  16. True
  17. ... or POST:
  18. >>> payload = dict(key1='value1', key2='value2')
  19. >>> r = requests.post('http://httpbin.org/post', data=payload)
  20. >>> print(r.text)
  21. {
  22. ...
  23. "form": {
  24. "key2": "value2",
  25. "key1": "value1"
  26. },
  27. ...
  28. }
  29. The other HTTP methods are supported - see `requests.api`. Full documentation
  30. is at <http://python-requests.org>.
  31. :copyright: (c) 2017 by Kenneth Reitz.
  32. :license: Apache 2.0, see LICENSE for more details.
  33. """
  34. import urllib3
  35. import chardet
  36. import warnings
  37. from .exceptions import RequestsDependencyWarning
  38. def check_compatibility(urllib3_version, chardet_version):
  39. urllib3_version = urllib3_version.split('.')
  40. assert urllib3_version != ['dev'] # Verify urllib3 isn't installed from git.
  41. # Sometimes, urllib3 only reports its version as 16.1.
  42. if len(urllib3_version) == 2:
  43. urllib3_version.append('0')
  44. # Check urllib3 for compatibility.
  45. major, minor, patch = urllib3_version # noqa: F811
  46. major, minor, patch = int(major), int(minor), int(patch)
  47. # urllib3 >= 1.21.1, <= 1.22
  48. assert major == 1
  49. assert minor >= 21
  50. assert minor <= 22
  51. # Check chardet for compatibility.
  52. major, minor, patch = chardet_version.split('.')[:3]
  53. major, minor, patch = int(major), int(minor), int(patch)
  54. # chardet >= 3.0.2, < 3.1.0
  55. assert major == 3
  56. assert minor < 1
  57. assert patch >= 2
  58. # Check imported dependencies for compatibility.
  59. try:
  60. check_compatibility(urllib3.__version__, chardet.__version__)
  61. except (AssertionError, ValueError):
  62. warnings.warn("urllib3 ({0}) or chardet ({1}) doesn't match a supported "
  63. "version!".format(urllib3.__version__, chardet.__version__),
  64. RequestsDependencyWarning)
  65. # Attempt to enable urllib3's SNI support, if possible
  66. try:
  67. from urllib3.contrib import pyopenssl
  68. pyopenssl.inject_into_urllib3()
  69. except ImportError:
  70. pass
  71. # urllib3's DependencyWarnings should be silenced.
  72. from urllib3.exceptions import DependencyWarning
  73. warnings.simplefilter('ignore', DependencyWarning)
  74. from .__version__ import __title__, __description__, __url__, __version__
  75. from .__version__ import __build__, __author__, __author_email__, __license__
  76. from .__version__ import __copyright__, __cake__
  77. from . import utils
  78. from . import packages
  79. from .models import Request, Response, PreparedRequest
  80. from .api import request, get, head, post, patch, put, delete, options
  81. from .sessions import session, Session
  82. from .status_codes import codes
  83. from .exceptions import (
  84. RequestException, Timeout, URLRequired,
  85. TooManyRedirects, HTTPError, ConnectionError,
  86. FileModeWarning, ConnectTimeout, ReadTimeout
  87. )
  88. # Set default logging handler to avoid "No handler found" warnings.
  89. import logging
  90. try: # Python 2.7+
  91. from logging import NullHandler
  92. except ImportError:
  93. class NullHandler(logging.Handler):
  94. def emit(self, record):
  95. pass
  96. logging.getLogger(__name__).addHandler(NullHandler())
  97. # FileModeWarnings go off per the default.
  98. warnings.simplefilter('default', FileModeWarning, append=True)