__init__.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # __
  2. # /__) _ _ _ _ _/ _
  3. # / ( (- (/ (/ (- _) / _)
  4. # /
  5. """
  6. Requests HTTP Library
  7. ~~~~~~~~~~~~~~~~~~~~~
  8. Requests is an HTTP library, written in Python, for human beings.
  9. Basic GET usage:
  10. >>> import requests
  11. >>> r = requests.get('https://www.python.org')
  12. >>> r.status_code
  13. 200
  14. >>> b'Python is a programming language' in r.content
  15. True
  16. ... or POST:
  17. >>> payload = dict(key1='value1', key2='value2')
  18. >>> r = requests.post('https://httpbin.org/post', data=payload)
  19. >>> print(r.text)
  20. {
  21. ...
  22. "form": {
  23. "key1": "value1",
  24. "key2": "value2"
  25. },
  26. ...
  27. }
  28. The other HTTP methods are supported - see `requests.api`. Full documentation
  29. is at <https://requests.readthedocs.io>.
  30. :copyright: (c) 2017 by Kenneth Reitz.
  31. :license: Apache 2.0, see LICENSE for more details.
  32. """
  33. import warnings
  34. import urllib3
  35. from .exceptions import RequestsDependencyWarning
  36. try:
  37. from charset_normalizer import __version__ as charset_normalizer_version
  38. except ImportError:
  39. charset_normalizer_version = None
  40. try:
  41. from chardet import __version__ as chardet_version
  42. except ImportError:
  43. chardet_version = None
  44. def check_compatibility(urllib3_version, chardet_version, charset_normalizer_version):
  45. urllib3_version = urllib3_version.split(".")
  46. assert urllib3_version != ["dev"] # Verify urllib3 isn't installed from git.
  47. # Sometimes, urllib3 only reports its version as 16.1.
  48. if len(urllib3_version) == 2:
  49. urllib3_version.append("0")
  50. # Check urllib3 for compatibility.
  51. major, minor, patch = urllib3_version # noqa: F811
  52. major, minor, patch = int(major), int(minor), int(patch)
  53. # urllib3 >= 1.21.1
  54. assert major >= 1
  55. if major == 1:
  56. assert minor >= 21
  57. # Check charset_normalizer for compatibility.
  58. if chardet_version:
  59. major, minor, patch = chardet_version.split(".")[:3]
  60. major, minor, patch = int(major), int(minor), int(patch)
  61. # chardet_version >= 3.0.2, < 8.0.0
  62. assert (3, 0, 2) <= (major, minor, patch) < (8, 0, 0)
  63. elif charset_normalizer_version:
  64. major, minor, patch = charset_normalizer_version.split(".")[:3]
  65. major, minor, patch = int(major), int(minor), int(patch)
  66. # charset_normalizer >= 2.0.0 < 4.0.0
  67. assert (2, 0, 0) <= (major, minor, patch) < (4, 0, 0)
  68. else:
  69. warnings.warn(
  70. "Unable to find acceptable character detection dependency "
  71. "(chardet or charset_normalizer).",
  72. RequestsDependencyWarning,
  73. )
  74. def _check_cryptography(cryptography_version):
  75. # cryptography < 1.3.4
  76. try:
  77. cryptography_version = list(map(int, cryptography_version.split(".")))
  78. except ValueError:
  79. return
  80. if cryptography_version < [1, 3, 4]:
  81. warning = (
  82. f"Old version of cryptography ({cryptography_version}) may cause slowdown."
  83. )
  84. warnings.warn(warning, RequestsDependencyWarning)
  85. # Check imported dependencies for compatibility.
  86. try:
  87. check_compatibility(
  88. urllib3.__version__, chardet_version, charset_normalizer_version
  89. )
  90. except (AssertionError, ValueError):
  91. warnings.warn(
  92. f"urllib3 ({urllib3.__version__}) or chardet "
  93. f"({chardet_version})/charset_normalizer ({charset_normalizer_version}) "
  94. "doesn't match a supported version!",
  95. RequestsDependencyWarning,
  96. )
  97. # Attempt to enable urllib3's fallback for SNI support
  98. # if the standard library doesn't support SNI or the
  99. # 'ssl' library isn't available.
  100. try:
  101. try:
  102. import ssl
  103. except ImportError:
  104. ssl = None
  105. if not getattr(ssl, "HAS_SNI", False):
  106. from urllib3.contrib import pyopenssl
  107. pyopenssl.inject_into_urllib3()
  108. # Check cryptography version
  109. from cryptography import __version__ as cryptography_version
  110. _check_cryptography(cryptography_version)
  111. except ImportError:
  112. pass
  113. # urllib3's DependencyWarnings should be silenced.
  114. from urllib3.exceptions import DependencyWarning
  115. warnings.simplefilter("ignore", DependencyWarning)
  116. # Set default logging handler to avoid "No handler found" warnings.
  117. import logging
  118. from logging import NullHandler
  119. from . import packages, utils
  120. from .__version__ import (
  121. __author__,
  122. __author_email__,
  123. __build__,
  124. __cake__,
  125. __copyright__,
  126. __description__,
  127. __license__,
  128. __title__,
  129. __url__,
  130. __version__,
  131. )
  132. from .api import delete, get, head, options, patch, post, put, request
  133. from .exceptions import (
  134. ConnectionError,
  135. ConnectTimeout,
  136. FileModeWarning,
  137. HTTPError,
  138. JSONDecodeError,
  139. ReadTimeout,
  140. RequestException,
  141. Timeout,
  142. TooManyRedirects,
  143. URLRequired,
  144. )
  145. from .models import PreparedRequest, Request, Response
  146. from .sessions import Session, session
  147. from .status_codes import codes
  148. logging.getLogger(__name__).addHandler(NullHandler())
  149. # FileModeWarnings go off per the default.
  150. warnings.simplefilter("default", FileModeWarning, append=True)