Skip to content

Commit de50429

Browse files
committed
urllib.urequest: Add absolutely minimal urlopen() implementation.
Optimized for low-memory bare-metal systems.
1 parent bba8fb1 commit de50429

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

urllib.urequest/urllib/urequest.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import usocket
2+
3+
def urlopen(url, data=None):
4+
if data:
5+
raise NotImplementedError("POST is not yet supported")
6+
try:
7+
proto, dummy, host, path = url.split("/", 3)
8+
except ValueError:
9+
proto, dummy, host = url.split("/", 2)
10+
path = ""
11+
if proto != "http:":
12+
raise ValueError("Unsupported protocol: " + proto)
13+
14+
ai = usocket.getaddrinfo(host, 80)
15+
addr = ai[0][4]
16+
s = usocket.socket()
17+
s.connect(addr)
18+
s.send(b"GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, host))
19+
20+
l = s.readline()
21+
protover, status, msg = l.split(None, 2)
22+
status = int(status)
23+
#print(protover, status, msg)
24+
while True:
25+
l = s.readline()
26+
if not l or l == b"\r\n":
27+
break
28+
#print(line)
29+
if l.startswith(b"Transfer-Encoding:"):
30+
if b"chunked" in line:
31+
raise ValueError("Unsupported " + l)
32+
elif l.startswith(b"Location:"):
33+
raise NotImplementedError("Redirects not yet supported")
34+
35+
return s

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy