__init__.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 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.26
  48. assert major == 1
  49. assert minor >= 21
  50. assert minor <= 26
  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, < 5.0.0
  55. assert (3, 0, 2) <= (major, minor, patch) < (5, 0, 0)
  56. def _check_cryptography(cryptography_version):
  57. # cryptography < 1.3.4
  58. try:
  59. cryptography_version = list(map(int, cryptography_version.split('.')))
  60. except ValueError:
  61. return
  62. if cryptography_version < [1, 3, 4]:
  63. warning = 'Old version of cryptography ({}) may cause slowdown.'.format(cryptography_version)
  64. warnings.warn(warning, RequestsDependencyWarning)
  65. # Check imported dependencies for compatibility.
  66. try:
  67. check_compatibility(urllib3.__version__, chardet.__version__)
  68. except (AssertionError, ValueError):
  69. warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
  70. "version!".format(urllib3.__version__, chardet.__version__),
  71. RequestsDependencyWarning)
  72. # Attempt to enable urllib3's fallback for SNI support
  73. # if the standard library doesn't support SNI or the
  74. # 'ssl' library isn't available.
  75. try:
  76. try:
  77. import ssl
  78. except ImportError:
  79. ssl = None
  80. if not getattr(ssl, "HAS_SNI", False):
  81. from urllib3.contrib import pyopenssl
  82. pyopenssl.inject_into_urllib3()
  83. # Check cryptography version
  84. from cryptography import __version__ as cryptography_version
  85. _check_cryptography(cryptography_version)
  86. except ImportError:
  87. pass
  88. # urllib3's DependencyWarnings should be silenced.
  89. from urllib3.exceptions import DependencyWarning
  90. warnings.simplefilter('ignore', DependencyWarning)
  91. from .__version__ import __title__, __description__, __url__, __version__
  92. from .__version__ import __build__, __author__, __author_email__, __license__
  93. from .__version__ import __copyright__, __cake__
  94. from . import utils
  95. from . import packages
  96. from .models import Request, Response, PreparedRequest
  97. from .api import request, get, head, post, patch, put, delete, options
  98. from .sessions import session, Session
  99. from .status_codes import codes
  100. from .exceptions import (
  101. RequestException, Timeout, URLRequired,
  102. TooManyRedirects, HTTPError, ConnectionError,
  103. FileModeWarning, ConnectTimeout, ReadTimeout
  104. )
  105. # Set default logging handler to avoid "No handler found" warnings.
  106. import logging
  107. from logging import NullHandler
  108. logging.getLogger(__name__).addHandler(NullHandler())
  109. # FileModeWarnings go off per the default.
  110. warnings.simplefilter('default', FileModeWarning, append=True)