-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.py
More file actions
executable file
·293 lines (242 loc) · 9.4 KB
/
requests.py
File metadata and controls
executable file
·293 lines (242 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# The MIT License (MIT)
#
# Copyright (c) 2019 ladyada for Adafruit Industries
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
`adafruit_requests`
================================================================================
A requests-like library for web interfacing
* Author(s): ladyada, Paul Sokolovsky
Implementation Notes
--------------------
Adapted from https://github.com/micropython/micropython-lib/tree/master/urequests
micropython-lib consists of multiple modules from different sources and
authors. Each module comes under its own licensing terms. Short name of
a license can be found in a file within a module directory (usually
metadata.txt or setup.py). Complete text of each license used is provided
at https://github.com/micropython/micropython-lib/blob/master/LICENSE
author='Paul Sokolovsky'
license='MIT'
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
import gc
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Requests.git"
_the_interface = None # pylint: disable=invalid-name
_the_sock = None # pylint: disable=invalid-name
def set_socket(sock, iface=None):
"""Helper to set the global socket and optionally set the global network interface.
:param sock: socket object.
:param iface: internet interface object
"""
global _the_sock # pylint: disable=invalid-name, global-statement
_the_sock = sock
if iface:
global _the_interface # pylint: disable=invalid-name, global-statement
_the_interface = iface
_the_sock.set_interface(iface)
class Response:
"""The response from a request, contains all the headers/content"""
encoding = None
def __init__(self, sock):
self.socket = sock
self.encoding = "utf-8"
self._cached = None
self.status_code = None
self.reason = None
self._read_so_far = 0
self.headers = {}
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def close(self):
"""Close, delete and collect the response data"""
if self.socket:
self.socket.close()
del self.socket
del self._cached
gc.collect()
@property
def content(self):
"""The HTTP content direct from the socket, as bytes"""
# print(self.headers)
try:
content_length = int(self.headers["content-length"])
except KeyError:
content_length = 0
# print("Content length:", content_length)
if self._cached is None:
try:
self._cached = self.socket.recv(content_length)
finally:
self.socket.close()
self.socket = None
# print("Buffer length:", len(self._cached))
return self._cached
@property
def text(self):
"""The HTTP content, encoded into a string according to the HTTP
header encoding"""
return str(self.content, self.encoding)
def json(self):
"""The HTTP content, parsed into a json dictionary"""
try:
import json as json_module
except ImportError:
import ujson as json_module
return json_module.loads(self.content)
def iter_content(self, chunk_size=1, decode_unicode=False):
"""An iterator that will stream data by only reading 'chunk_size'
bytes and yielding them, when we can't buffer the whole datastream"""
if decode_unicode:
raise NotImplementedError("Unicode not supported")
while True:
chunk = self.socket.recv(chunk_size)
if chunk:
yield chunk
else:
return
# pylint: disable=too-many-branches, too-many-statements, unused-argument, too-many-arguments, too-many-locals
def request(method, url, data=None, json=None, headers=None, stream=False, timeout=1):
"""Perform an HTTP request to the given url which we will parse to determine
whether to use SSL ('https://') or not. We can also send some provided 'data'
or a json dictionary which we will stringify. 'headers' is optional HTTP headers
sent along. 'stream' will determine if we buffer everything, or whether to only
read only when requested
"""
global _the_interface # pylint: disable=global-statement, invalid-name
global _the_sock # pylint: disable=global-statement, invalid-name
if not headers:
headers = {}
try:
proto, dummy, host, path = url.split("/", 3)
# replace spaces in path
path = path.replace(" ", "%20")
except ValueError:
proto, dummy, host = url.split("/", 2)
path = ""
if proto == "http:":
port = 80
elif proto == "https:":
port = 443
else:
raise ValueError("Unsupported protocol: " + proto)
if ":" in host:
host, port = host.split(":", 1)
port = int(port)
addr_info = _the_sock.getaddrinfo(host, port, 0, _the_sock.SOCK_STREAM)[0]
sock = _the_sock.socket(addr_info[0], addr_info[1], addr_info[2])
resp = Response(sock) # our response
sock.settimeout(timeout) # socket read timeout
try:
if proto == "https:":
conntype = _the_interface.TLS_MODE
sock.connect(
(host, port), conntype
) # for SSL we need to know the host name
else:
conntype = _the_interface.TCP_MODE
sock.connect(addr_info[-1], conntype)
sock.send(b"%s /%s HTTP/1.0\r\n" % (bytes(method, "utf-8"), bytes(path, "utf-8")))
if "Host" not in headers:
sock.send(b"Host: %s\r\n" % bytes(host, "utf-8"))
if "User-Agent" not in headers:
sock.send(b"User-Agent: Adafruit CircuitPython\r\n")
# Iterate over keys to avoid tuple alloc
for k in headers:
sock.send(k.encode())
sock.send(b": ")
sock.send(headers[k].encode())
sock.send(b"\r\n")
if json is not None:
assert data is None
try:
import json as json_module
except ImportError:
import ujson as json_module
data = json_module.dumps(json)
sock.send(b"Content-Type: application/json\r\n")
if data:
sock.send(b"Content-Length: %d\r\n" % len(data))
sock.send(b"\r\n")
if data:
if isinstance(data, bytearray):
sock.send(bytes(data))
else:
sock.send(bytes(data, "utf-8"))
line = sock.readline()
# print(line)
line = line.split(None, 2)
status = int(line[1])
reason = ""
if len(line) > 2:
reason = line[2].rstrip()
resp.headers = parse_headers(sock)
if resp.headers.get("transfer-encoding"):
if "chunked" in resp.headers.get("transfer-encoding"):
raise ValueError("Unsupported " + resp.headers.get("transfer-encoding"))
elif resp.headers.get("location") and not 200 <= status <= 299:
raise NotImplementedError("Redirects not yet supported")
except:
sock.close()
raise
resp.status_code = status
resp.reason = reason
return resp
def parse_headers(sock):
"""
Parses the header portion of an HTTP request/response from the socket.
Expects first line of HTTP request/response to have been read already
return: header dictionary
rtype: Dict
"""
headers = {}
while True:
line = sock.readline()
if not line or line == b"\r\n":
break
#print("**line: ", line)
title, content = line.split(b': ', 1)
if title and content:
title = str(title.lower(), 'utf-8')
content = str(content, 'utf-8')
headers[title] = content
return headers
def head(url, **kw):
"""Send HTTP HEAD request"""
return request("HEAD", url, **kw)
def get(url, **kw):
"""Send HTTP GET request"""
return request("GET", url, **kw)
def post(url, **kw):
"""Send HTTP POST request"""
return request("POST", url, **kw)
def put(url, **kw):
"""Send HTTP PUT request"""
return request("PUT", url, **kw)
def patch(url, **kw):
"""Send HTTP PATCH request"""
return request("PATCH", url, **kw)
def delete(url, **kw):
"""Send HTTP DELETE request"""
return request("DELETE", url, **kw)