upip: Add support for native (no wget) SSL operations using ussl module.

pull/46/head
Paul Sokolovsky 2015-09-03 17:26:58 +03:00
rodzic daaffd7edb
commit 8c749d292a
1 zmienionych plików z 46 dodań i 8 usunięć

Wyświetl plik

@ -86,6 +86,43 @@ def expandhome(s):
s = s.replace("~/", h + "/") s = s.replace("~/", h + "/")
return s return s
try:
import ussl
import usocket
def download(url, local_name):
proto, _, host, urlpath = url.split('/', 3)
ai = usocket.getaddrinfo(host, 443)
#print("Address infos:", ai)
addr = ai[0][4]
s = usocket.socket()
#print("Connect address:", addr)
s.connect(addr)
if proto == "https:":
s = ussl.wrap_socket(s)
# MicroPython rawsocket module supports file interface directly
s.write("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (urlpath, host))
l = s.readline()
protover, status, msg = l.split(None, 2)
if status != b"200":
raise OSError()
while 1:
l = s.readline()
if not l:
raise OSError()
if l == b'\r\n':
break
with open(local_name, "wb") as f:
while 1:
l = s.read(1024)
if not l:
break
f.write(l)
except ImportError:
def download(url, local_name): def download(url, local_name):
if debug: if debug:
print("wget -q %s -O %s" % (url, local_name)) print("wget -q %s -O %s" % (url, local_name))
@ -95,6 +132,7 @@ def download(url, local_name):
if rc == 8 * 256: if rc == 8 * 256:
raise NotFoundError raise NotFoundError
def get_pkg_metadata(name): def get_pkg_metadata(name):
download("https://pypi.python.org/pypi/%s/json" % name, ".pkg.json") download("https://pypi.python.org/pypi/%s/json" % name, ".pkg.json")
with open(".pkg.json") as f: with open(".pkg.json") as f: