Skip to content

Commit d036673

Browse files
author
Paul Sokolovsky
committed
urllib.urequest: Avoid allocating memory when issuing request.
Memory will still be allocated when parsing response, but this already saves 164 bytes of memory traffic (x64).
1 parent 52ac454 commit d036673

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

urllib.urequest/urllib/urequest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ def urlopen(url, data=None, method="GET"):
2727
if proto == "https:":
2828
s = ussl.wrap_socket(s)
2929

30-
req = b"%s /%s HTTP/1.0\r\nHost: %s\r\n" % (method, path, host)
31-
s.write(req)
30+
s.write(method)
31+
s.write(b" /")
32+
s.write(path)
33+
s.write(b" HTTP/1.0\r\nHost: ")
34+
s.write(host)
35+
s.write(b"\r\n")
3236

3337
if data:
34-
req = b"Content-Length: %d\r\n" % len(data)
38+
s.write(b"Content-Length: ")
39+
s.write(str(len(data)))
40+
s.write(b"\r\n")
3541
s.write(b"\r\n")
3642
if data:
3743
s.write(data)

0 commit comments

Comments
 (0)