3
# / ( (- (/ (/ (- _) / _)
10
Requests is an HTTP library, written in Python, for human beings.
14
>>> r = requests.get('https://www.python.org')
17
>>> b'Python is a programming language' in r.content
22
>>> payload = dict(key1='value1', key2='value2')
23
>>> r = requests.post('https://httpbin.org/post', data=payload)
34
The other HTTP methods are supported - see `requests.api`. Full documentation
35
is at <https://requests.readthedocs.io>.
37
:copyright: (c) 2017 by Kenneth Reitz.
38
:license: Apache 2.0, see LICENSE for more details.
41
from __future__ import annotations
47
from .exceptions import RequestsDependencyWarning
50
from charset_normalizer import __version__ as charset_normalizer_version
52
charset_normalizer_version = None
55
from chardet import __version__ as chardet_version
57
chardet_version = None
60
def check_compatibility(
62
chardet_version: str | None,
63
charset_normalizer_version: str | None,
65
urllib3_version_list = urllib3_version.split(".")[:3]
66
assert urllib3_version_list != ["dev"] # Verify urllib3 isn't installed from git.
62
chardet_version: str | None,
63
charset_normalizer_version: str | None,
65
urllib3_version_list = urllib3_version.split(".")[:3]
66
assert urllib3_version_list != ["dev"] # Verify urllib3 isn't installed from git.
68
# Sometimes, urllib3 only reports its version as 16.1.
69
if len(urllib3_version_list) == 2:
70
urllib3_version_list.append("0")
72
# Check urllib3 for compatibility.
73
major, minor, patch = urllib3_version_list # noqa: F811
74
major, minor, patch = int(major), int(minor), int(patch)
80
# Check charset_normalizer for compatibility.
82
major, minor, patch = chardet_version.split(".")[:3]
83
major, minor, patch = int(major), int(minor), int(patch)
84
# chardet_version >= 3.0.2, < 8.0.0
85
assert (3, 0, 2) <= (major, minor, patch) < (8, 0, 0)
86
elif charset_normalizer_version:
87
major, minor, patch = charset_normalizer_version.split(".")[:3]
88
major, minor, patch = int(major), int(minor), int(patch)
89
# charset_normalizer >= 2.0.0 < 4.0.0
90
assert (2, 0, 0) <= (major, minor, patch) < (4, 0, 0)
93
"Unable to find acceptable character detection dependency "
94
"(chardet or charset_normalizer).",
95
RequestsDependencyWarning,
93
"Unable to find acceptable character detection dependency "
94
"(chardet or charset_normalizer).",
95
RequestsDependencyWarning,
99
def _check_cryptography(cryptography_version: str) -> None:
100
# cryptography < 1.3.4
102
cryptography_version_list = list(map(int, cryptography_version.split(".")))
106
if cryptography_version_list < [1, 3, 4]:
107
warning = f"Old version of cryptography ({cryptography_version_list}) may cause slowdown."
108
warnings.warn(warning, RequestsDependencyWarning)
111
# Check imported dependencies for compatibility.
116
charset_normalizer_version,
118
except (AssertionError, ValueError):
120
f"urllib3 ({urllib3.__version__}) or chardet "
121
f"({chardet_version})/charset_normalizer ({charset_normalizer_version}) "
122
"doesn't match a supported version!",
123
RequestsDependencyWarning,
126
# Attempt to enable urllib3's fallback for SNI support
127
# if the standard library doesn't support SNI or the
128
# 'ssl' library isn't available.
135
if not getattr(ssl, "HAS_SNI", False):
136
from urllib3.contrib import pyopenssl
138
pyopenssl.inject_into_urllib3()
135
if not getattr(ssl, "HAS_SNI", False):
136
from urllib3.contrib import pyopenssl
138
pyopenssl.inject_into_urllib3()
140
# Check cryptography version
141
from cryptography import (
142
__version__ as cryptography_version,
145
_check_cryptography(cryptography_version)
149
# urllib3's DependencyWarnings should be silenced.
150
from urllib3.exceptions import DependencyWarning
152
warnings.simplefilter("ignore", DependencyWarning)
154
# Set default logging handler to avoid "No handler found" warnings.
156
from logging import NullHandler
158
from . import packages, utils
159
from .__version__ import (
171
from .api import delete, get, head, options, patch, post, put, request
172
from .exceptions import (
184
from .models import PreparedRequest, Request, Response
185
from .sessions import Session, session
186
from .status_codes import codes
216
logging.getLogger(__name__).addHandler(NullHandler())
218
# FileModeWarnings go off per the default.
219
warnings.simplefilter("default", FileModeWarning, append=True)