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