kopia lustrzana https://github.com/micropython/micropython-lib
upip: url_open: Reworking error handling to guaranteedly close socket.
rodzic
024d6bc2b1
commit
91d9c168b8
54
upip/upip.py
54
upip/upip.py
|
@ -117,32 +117,33 @@ def url_open(url):
|
||||||
addr = ai[0][4]
|
addr = ai[0][4]
|
||||||
|
|
||||||
s = usocket.socket(ai[0][0])
|
s = usocket.socket(ai[0][0])
|
||||||
#print("Connect address:", addr)
|
try:
|
||||||
s.connect(addr)
|
#print("Connect address:", addr)
|
||||||
|
s.connect(addr)
|
||||||
|
|
||||||
if proto == "https:":
|
if proto == "https:":
|
||||||
s = ussl.wrap_socket(s)
|
s = ussl.wrap_socket(s)
|
||||||
if warn_ussl:
|
if warn_ussl:
|
||||||
print("Warning: %s SSL certificate is not validated" % host)
|
print("Warning: %s SSL certificate is not validated" % host)
|
||||||
warn_ussl = False
|
warn_ussl = False
|
||||||
|
|
||||||
# MicroPython rawsocket module supports file interface directly
|
# MicroPython rawsocket module supports file interface directly
|
||||||
s.write("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (urlpath, host))
|
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":
|
|
||||||
s.close()
|
|
||||||
exc = ValueError(status)
|
|
||||||
if status == b"404" or status == b"301":
|
|
||||||
fatal("Package not found", exc)
|
|
||||||
fatal("Unexpected error querying for package", exc)
|
|
||||||
while 1:
|
|
||||||
l = s.readline()
|
l = s.readline()
|
||||||
if not l:
|
protover, status, msg = l.split(None, 2)
|
||||||
s.close()
|
if status != b"200":
|
||||||
fatal("Unexpected EOF in HTTP headers", ValueError())
|
if status == b"404" or status == b"301":
|
||||||
if l == b'\r\n':
|
raise NotFoundError("Package not found")
|
||||||
break
|
raise ValueError(status)
|
||||||
|
while 1:
|
||||||
|
l = s.readline()
|
||||||
|
if not l:
|
||||||
|
raise ValueError("Unexpected EOF in HTTP headers")
|
||||||
|
if l == b'\r\n':
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
s.close()
|
||||||
|
raise e
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
@ -215,9 +216,10 @@ def install(to_install, install_path=None):
|
||||||
if deps:
|
if deps:
|
||||||
deps = deps.decode("utf-8").split("\n")
|
deps = deps.decode("utf-8").split("\n")
|
||||||
to_install.extend(deps)
|
to_install.extend(deps)
|
||||||
except NotFoundError:
|
except Exception as e:
|
||||||
print("Error: cannot find '%s' package (or server error), packages may be partially installed" \
|
print("Error installing '{}': {}, packages may be partially installed".format(
|
||||||
% pkg_spec, file=sys.stderr)
|
pkg_spec, e),
|
||||||
|
file=sys.stderr)
|
||||||
|
|
||||||
def get_install_path():
|
def get_install_path():
|
||||||
global install_path
|
global install_path
|
||||||
|
|
Ładowanie…
Reference in New Issue