From 880c6a7011893f324c096347685d6e501ea06b93 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 30 May 2016 21:24:41 +0300 Subject: [PATCH] urllib.urequest: Add support for HTTPS. --- urllib.urequest/urllib/urequest.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/urllib.urequest/urllib/urequest.py b/urllib.urequest/urllib/urequest.py index 03e63839..cb0b2e23 100644 --- a/urllib.urequest/urllib/urequest.py +++ b/urllib.urequest/urllib/urequest.py @@ -8,10 +8,14 @@ def urlopen(url, data=None, method="GET"): except ValueError: proto, dummy, host = url.split("/", 2) path = "" - if proto != "http:": + if proto == "http:": + port = 80 + elif proto == "https:": + import ussl + port = 443 + else: raise ValueError("Unsupported protocol: " + proto) - port = 80 if ":" in host: host, port = host.split(":", 1) port = int(port) @@ -20,13 +24,15 @@ def urlopen(url, data=None, method="GET"): addr = ai[0][4] s = usocket.socket() s.connect(addr) + if proto == "https:": + s = ussl.wrap_socket(s) if data: req = b"%s /%s HTTP/1.0\r\nHost: %s\r\nContent-Length: %d\r\n\r\n" % (method, path, host, len(data)) else: req = b"%s /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (method, path, host) - s.send(req) + s.write(req) if data: - s.send(data) + s.write(data) l = s.readline() protover, status, msg = l.split(None, 2)