__init__.py 4.8 KB

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