u2cli: connection reuse for lower latency

This commit is contained in:
ed 2021-09-28 00:14:45 +02:00
parent 98e7d8f728
commit b8ac9b7994

View file

@ -3,7 +3,7 @@ from __future__ import print_function, unicode_literals
""" """
up2k.py: upload to copyparty up2k.py: upload to copyparty
2021-09-27, v0.1, ed <irc.rizon.net>, MIT-Licensed 2021-09-27, v0.2, ed <irc.rizon.net>, MIT-Licensed
https://github.com/9001/copyparty/blob/hovudstraum/bin/up2k.py https://github.com/9001/copyparty/blob/hovudstraum/bin/up2k.py
- dependencies: requests - dependencies: requests
@ -47,6 +47,9 @@ VT100 = not WINDOWS or WINDOWS >= [10, 0, 14393]
# introduced in anniversary update # introduced in anniversary update
req_ses = requests.Session()
class File(object): class File(object):
"""an up2k upload task; represents a single file""" """an up2k upload task; represents a single file"""
@ -216,8 +219,8 @@ def get_hashlist(file, pcb):
file.kchunks = {k: [v1, v2] for k, v1, v2 in ret} file.kchunks = {k: [v1, v2] for k, v1, v2 in ret}
def handshake(url, file, pw, cert): def handshake(req_ses, url, file, pw):
# type: (str, File, any, any) -> List[str] # type: (requests.Session, str, File, any) -> List[str]
"""performs a handshake with the server; reply is a list of chunks to upload""" """performs a handshake with the server; reply is a list of chunks to upload"""
req = { req = {
@ -235,7 +238,7 @@ def handshake(url, file, pw, cert):
elif b"/" in file.rel: elif b"/" in file.rel:
url += file.rel.rsplit(b"/", 1)[0].decode("utf-8", "replace") url += file.rel.rsplit(b"/", 1)[0].decode("utf-8", "replace")
r = requests.post(url, headers=headers, json=req, verify=cert) r = req_ses.post(url, headers=headers, json=req)
try: try:
r = r.json() r = r.json()
except: except:
@ -254,8 +257,8 @@ def handshake(url, file, pw, cert):
return r["hash"] return r["hash"]
def upload(file, cid, pw, cert): def upload(req_ses, file, cid, pw):
# type: (File, str, any, any) -> None # type: (requests.Session, File, str, any) -> None
"""upload one specific chunk, `cid` (a chunk-hash)""" """upload one specific chunk, `cid` (a chunk-hash)"""
headers = { headers = {
@ -268,9 +271,11 @@ def upload(file, cid, pw, cert):
f = FileSlice(file, cid) f = FileSlice(file, cid)
try: try:
r = requests.post(file.url, headers=headers, data=f, verify=cert) r = req_ses.post(file.url, headers=headers, data=f)
if not r: if not r:
raise Exception(repr(r)) raise Exception(repr(r))
_ = r.content
finally: finally:
f.f.close() f.f.close()
@ -304,11 +309,10 @@ class Ctl(object):
print("found {} files, {}\n".format(nfiles, humansize(nbytes))) print("found {} files, {}\n".format(nfiles, humansize(nbytes)))
cert = None
if ar.td: if ar.td:
cert = False req_ses.verify = False
if ar.te: if ar.te:
cert = ar.te req_ses.verify = ar.te
self.filegen = walkdirs(ar.files) self.filegen = walkdirs(ar.files)
for nf, (top, rel, inf) in enumerate(self.filegen): for nf, (top, rel, inf) in enumerate(self.filegen):
@ -320,7 +324,7 @@ class Ctl(object):
while True: while True:
print(" hs...") print(" hs...")
up = handshake(ar.url, file, ar.a, cert) up = handshake(req_ses, ar.url, file, ar.a)
file.ucids = up file.ucids = up
if not up: if not up:
break break
@ -329,7 +333,7 @@ class Ctl(object):
ncs = len(up) ncs = len(up)
for nc, cid in enumerate(up): for nc, cid in enumerate(up):
print(" {} up {}".format(ncs - nc, cid)) print(" {} up {}".format(ncs - nc, cid))
upload(file, cid, ar.a, cert) upload(req_ses, file, cid, ar.a)
print(" ok!") print(" ok!")