You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.8 KiB

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