__init__.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, < 6.0.0
  62. assert (3, 0, 2) <= (major, minor, patch) < (6, 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 = "Old version of cryptography ({}) may cause slowdown.".format(
  82. cryptography_version
  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. "urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
  93. "version!".format(
  94. urllib3.__version__, chardet_version, charset_normalizer_version
  95. ),
  96. RequestsDependencyWarning,
  97. )
  98. # Attempt to enable urllib3's fallback for SNI support
  99. # if the standard library doesn't support SNI or the
  100. # 'ssl' library isn't available.
  101. try:
  102. try:
  103. import ssl
  104. except ImportError:
  105. ssl = None
  106. if not getattr(ssl, "HAS_SNI", False):
  107. from urllib3.contrib import pyopenssl
  108. pyopenssl.inject_into_urllib3()
  109. # Check cryptography version
  110. from cryptography import __version__ as cryptography_version
  111. _check_cryptography(cryptography_version)
  112. except ImportError:
  113. pass
  114. # urllib3's DependencyWarnings should be silenced.
  115. from urllib3.exceptions import DependencyWarning
  116. warnings.simplefilter("ignore", DependencyWarning)
  117. # Set default logging handler to avoid "No handler found" warnings.
  118. import logging
  119. from logging import NullHandler
  120. from . import packages, utils
  121. from .__version__ import (
  122. __author__,
  123. __author_email__,
  124. __build__,
  125. __cake__,
  126. __copyright__,
  127. __description__,
  128. __license__,
  129. __title__,
  130. __url__,
  131. __version__,
  132. )
  133. from .api import delete, get, head, options, patch, post, put, request
  134. from .exceptions import (
  135. ConnectionError,
  136. ConnectTimeout,
  137. FileModeWarning,
  138. HTTPError,
  139. JSONDecodeError,
  140. ReadTimeout,
  141. RequestException,
  142. Timeout,
  143. TooManyRedirects,
  144. URLRequired,
  145. )
  146. from .models import PreparedRequest, Request, Response
  147. from .sessions import Session, session
  148. from .status_codes import codes
  149. logging.getLogger(__name__).addHandler(NullHandler())
  150. # FileModeWarnings go off per the default.
  151. warnings.simplefilter("default", FileModeWarning, append=True)