-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathget_deflate.py
47 lines (36 loc) · 1.22 KB
/
get_deflate.py
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
"""Send GET HTTP request and request and handle 'deflate' compression of response.
Requires the 'deflate' module added in Micropython v1.21, the 'zlib' module from
micropython-lib, or the CPython standard library.
"""
try:
# Use MicroPython's 'deflate' module, if available,
# to avoid dependency on and RAM cost of 'zlib' module.
import deflate
except ImportError:
import zlib
def decompress(resp):
return zlib.decompress(resp.content)
else:
def decompress(resp, wbits=10):
with deflate.DeflateIO(resp._sf, deflate.ZLIB, wbits) as g:
return g.read()
import mrequests
host = "http://httpbin.org/"
# host = "http://localhost/"
url = host + "deflate"
r = mrequests.get(url, headers={b"TE": b"deflate"}, save_headers=True)
if r.status_code == 200:
print("Response headers:")
print("-----------------\n")
print(b"\n".join(r.headers).decode())
if r.encoding.split(",")[0] == "deflate":
text = decompress(r).decode("utf-8")
print("Deflated response text length: %i" % len(text))
else:
text = r.text
print("Response text:")
print("--------------\n")
print(text)
else:
print("Request failed. Status: {}".format(r.status_code))
r.close()