Skip to content

Commit daec6f2

Browse files
authored
Add files via upload
1 parent ed77fb7 commit daec6f2

4 files changed

Lines changed: 236 additions & 140 deletions

File tree

pywwwget_chatgpt.py

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@
6161
import re
6262
import sys
6363
import platform
64-
import secrets
64+
try:
65+
import secrets
66+
except ImportError:
67+
secrets = False
6568
import socket
6669
import shutil
6770
import time
@@ -1478,19 +1481,27 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
14781481
try:
14791482
httpcodereason = resp.reason
14801483
except AttributeError:
1481-
httpcodereason = http_status_to_reason(geturls_text.getcode())
1484+
httpcodereason = http_status_to_reason(resp.getcode())
14821485
try:
14831486
httpversionout = resp.version
14841487
except AttributeError:
14851488
httpversionout = "1.1"
14861489
try:
14871490
httpmethodout = resp.get_method()
14881491
except AttributeError:
1489-
httpmethodout = resp._method
1492+
try:
1493+
httpmethodout = resp._method
1494+
except AttributeError:
1495+
httpmethodout = httpmethod
14901496
httpurlout = resp.geturl()
1491-
httpheaderout = resp.info()
1497+
httpheaderout = dict(resp.info())
1498+
#httpheaderout = dict(resp.headers)
14921499
try:
1493-
httpheadersentout = req.unredirected_hdrs | req.headers
1500+
try:
1501+
httpheadersentout = req.unredirected_hdrs | req.headers
1502+
except TypeError:
1503+
httpheadersentout = req.unredirected_hdrs
1504+
httpheadersentout.update(req.headers)
14941505
except AttributeError:
14951506
httpheadersentout = req.header_items()
14961507
fulldatasize = httpfile.tell()
@@ -3488,7 +3499,10 @@ def _udp_seq_send(fileobj, host, port, resume=False, path_text=None, **kwargs):
34883499

34893500
# Start with base timeout; will adapt
34903501
timeout = max(min_to, min(max_to, base_timeout))
3491-
sock.settimeout(timeout)
3502+
try:
3503+
sock.settimeout(timeout)
3504+
except Exception:
3505+
pass
34923506

34933507
chunk = int(kwargs.get("chunk", 1200))
34943508
max_window = int(kwargs.get("window", 32)) # cap
@@ -3506,7 +3520,11 @@ def _udp_seq_send(fileobj, host, port, resume=False, path_text=None, **kwargs):
35063520

35073521
tid = int(kwargs.get("tid", 0) or 0)
35083522
if tid == 0:
3509-
tid = secrets.randbits(64)
3523+
# py2/py3 compatible random 64b
3524+
try:
3525+
tid = secrets.randbits(64) # py3.6+
3526+
except Exception:
3527+
tid = struct.unpack("!Q", os.urandom(8))[0]
35103528

35113529
# stats
35123530
stats = {
@@ -3574,42 +3592,49 @@ def _udp_seq_send(fileobj, host, port, resume=False, path_text=None, **kwargs):
35743592
srtt = None
35753593
rttvar = None
35763594

3595+
# PY2/3 replacement for "nonlocal": store mutable state in dict
3596+
_st = {
3597+
"srtt": srtt,
3598+
"rttvar": rttvar,
3599+
"timeout": timeout,
3600+
"cwnd": cwnd,
3601+
"cwnd_float": cwnd_float,
3602+
}
3603+
35773604
def _update_rtt(sample):
3578-
nonlocal srtt, rttvar, timeout
35793605
if sample <= 0:
35803606
return
3581-
if srtt is None:
3582-
srtt = sample
3583-
rttvar = sample / 2.0
3607+
if _st["srtt"] is None:
3608+
_st["srtt"] = sample
3609+
_st["rttvar"] = sample / 2.0
35843610
else:
35853611
# RFC6298-ish
3586-
alpha = 1 / 8
3587-
beta = 1 / 4
3588-
rttvar = (1 - beta) * rttvar + beta * abs(srtt - sample)
3589-
srtt = (1 - alpha) * srtt + alpha * sample
3590-
timeout = srtt + 4.0 * rttvar
3591-
timeout = max(min_to, min(max_to, timeout))
3612+
alpha = 1.0 / 8.0
3613+
beta = 1.0 / 4.0
3614+
_st["rttvar"] = (1 - beta) * _st["rttvar"] + beta * abs(_st["srtt"] - sample)
3615+
_st["srtt"] = (1 - alpha) * _st["srtt"] + alpha * sample
3616+
3617+
_st["timeout"] = _st["srtt"] + 4.0 * _st["rttvar"]
3618+
_st["timeout"] = max(min_to, min(max_to, _st["timeout"]))
35923619
try:
3593-
sock.settimeout(timeout)
3620+
sock.settimeout(_st["timeout"])
35943621
except Exception:
35953622
pass
35963623

35973624
def _loss_event():
3598-
nonlocal cwnd, cwnd_float
35993625
stats["loss_events"] += 1
3600-
cwnd = max(1, cwnd // 2)
3601-
cwnd_float = float(cwnd)
3626+
_st["cwnd"] = max(1, int(_st["cwnd"]) // 2)
3627+
_st["cwnd_float"] = float(_st["cwnd"])
36023628

36033629
def _ai_increase(acked_count):
36043630
# additive increase: cwnd += acked/cwnd (smoothed)
3605-
nonlocal cwnd, cwnd_float
36063631
if acked_count <= 0:
36073632
return
3608-
cwnd_float += float(acked_count) / max(1.0, float(cwnd))
3609-
new_cwnd = int(cwnd_float)
3610-
if new_cwnd > cwnd:
3611-
cwnd = min(max_window, new_cwnd)
3612-
cwnd_float = float(cwnd)
3633+
_st["cwnd_float"] += float(acked_count) / max(1.0, float(_st["cwnd"]))
3634+
new_cwnd = int(_st["cwnd_float"])
3635+
if new_cwnd > _st["cwnd"]:
3636+
_st["cwnd"] = min(max_window, new_cwnd)
3637+
_st["cwnd_float"] = float(_st["cwnd"])
36133638

36143639
def _send_pkt(seq, wire_payload, flags):
36153640
sock.sendto(_u_pack(flags, seq, total, tid) + wire_payload, addr)
@@ -3629,7 +3654,7 @@ def _read_chunk():
36293654

36303655
# Prime window
36313656
eof = False
3632-
while not eof and len(in_flight) < cwnd:
3657+
while not eof and len(in_flight) < _st["cwnd"]:
36333658
data = _read_chunk()
36343659
if data is None:
36353660
eof = True
@@ -3666,7 +3691,6 @@ def _read_chunk():
36663691
# Cumulative ACK: drop all <= ack_upto
36673692
for s in [s for s in list(in_flight.keys()) if s <= ack_upto]:
36683693
wire, ts, _tries, _dlen = in_flight[s]
3669-
# RTT sample from original send timestamp (works best if not retransmitted)
36703694
sample = now - ts
36713695
_update_rtt(sample)
36723696
del in_flight[s]
@@ -3708,7 +3732,7 @@ def _read_chunk():
37083732
now = time.time()
37093733
for seq in list(in_flight.keys()):
37103734
wire, ts, tries, dlen = in_flight[seq]
3711-
if (now - ts) >= timeout:
3735+
if (now - ts) >= _st["timeout"]:
37123736
if tries >= retries:
37133737
failed = True
37143738
in_flight.clear()
@@ -3722,7 +3746,7 @@ def _read_chunk():
37223746
break
37233747

37243748
# Fill window based on cwnd
3725-
while not eof and len(in_flight) < cwnd:
3749+
while not eof and len(in_flight) < _st["cwnd"]:
37263750
data = _read_chunk()
37273751
if data is None:
37283752
eof = True
@@ -3738,10 +3762,10 @@ def _read_chunk():
37383762
dur = max(1e-9, time.time() - t_start)
37393763
stats["duration_s"] = dur
37403764
stats["throughput_Bps"] = float(stats["bytes_sent_payload"]) / dur
3741-
stats["timeout"] = timeout
3742-
stats["srtt"] = srtt
3743-
stats["rttvar"] = rttvar
3744-
stats["cwnd_end"] = cwnd
3765+
stats["timeout"] = _st["timeout"]
3766+
stats["srtt"] = _st["srtt"]
3767+
stats["rttvar"] = _st["rttvar"]
3768+
stats["cwnd_end"] = _st["cwnd"]
37453769

37463770
if failed:
37473771
try:

pywwwget_deepseek.py

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import re
2929
import sys
3030
import platform
31-
import secrets
31+
try:
32+
import secrets
33+
except ImportError:
34+
secrets = False
3235
import socket
3336
import shutil
3437
import time
@@ -1711,19 +1714,27 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
17111714
try:
17121715
httpcodereason = resp.reason
17131716
except AttributeError:
1714-
httpcodereason = http_status_to_reason(geturls_text.getcode())
1717+
httpcodereason = http_status_to_reason(resp.getcode())
17151718
try:
17161719
httpversionout = resp.version
17171720
except AttributeError:
17181721
httpversionout = "1.1"
17191722
try:
17201723
httpmethodout = resp.get_method()
17211724
except AttributeError:
1722-
httpmethodout = resp._method
1725+
try:
1726+
httpmethodout = resp._method
1727+
except AttributeError:
1728+
httpmethodout = httpmethod
17231729
httpurlout = resp.geturl()
1724-
httpheaderout = resp.info()
1730+
httpheaderout = dict(resp.info())
1731+
#httpheaderout = dict(resp.headers)
17251732
try:
1726-
httpheadersentout = req.unredirected_hdrs | req.headers
1733+
try:
1734+
httpheadersentout = req.unredirected_hdrs | req.headers
1735+
except TypeError:
1736+
httpheadersentout = req.unredirected_hdrs
1737+
httpheadersentout.update(req.headers)
17271738
except AttributeError:
17281739
httpheadersentout = req.header_items()
17291740
fulldatasize = httpfile.tell()
@@ -3146,7 +3157,10 @@ def _udp_seq_send(fileobj, host, port, resume=False, path_text=None, **kwargs):
31463157

31473158
# Start with base timeout; will adapt
31483159
timeout = max(min_to, min(max_to, base_timeout))
3149-
sock.settimeout(timeout)
3160+
try:
3161+
sock.settimeout(timeout)
3162+
except Exception:
3163+
pass
31503164

31513165
chunk = int(kwargs.get("chunk", 1200))
31523166
max_window = int(kwargs.get("window", 32)) # cap
@@ -3164,7 +3178,11 @@ def _udp_seq_send(fileobj, host, port, resume=False, path_text=None, **kwargs):
31643178

31653179
tid = int(kwargs.get("tid", 0) or 0)
31663180
if tid == 0:
3167-
tid = secrets.randbits(64)
3181+
# py2/py3 compatible random 64b
3182+
try:
3183+
tid = secrets.randbits(64) # py3.6+
3184+
except Exception:
3185+
tid = struct.unpack("!Q", os.urandom(8))[0]
31683186

31693187
# stats
31703188
stats = {
@@ -3232,42 +3250,49 @@ def _udp_seq_send(fileobj, host, port, resume=False, path_text=None, **kwargs):
32323250
srtt = None
32333251
rttvar = None
32343252

3253+
# PY2/3 replacement for "nonlocal": store mutable state in dict
3254+
_st = {
3255+
"srtt": srtt,
3256+
"rttvar": rttvar,
3257+
"timeout": timeout,
3258+
"cwnd": cwnd,
3259+
"cwnd_float": cwnd_float,
3260+
}
3261+
32353262
def _update_rtt(sample):
3236-
nonlocal srtt, rttvar, timeout
32373263
if sample <= 0:
32383264
return
3239-
if srtt is None:
3240-
srtt = sample
3241-
rttvar = sample / 2.0
3265+
if _st["srtt"] is None:
3266+
_st["srtt"] = sample
3267+
_st["rttvar"] = sample / 2.0
32423268
else:
32433269
# RFC6298-ish
3244-
alpha = 1 / 8
3245-
beta = 1 / 4
3246-
rttvar = (1 - beta) * rttvar + beta * abs(srtt - sample)
3247-
srtt = (1 - alpha) * srtt + alpha * sample
3248-
timeout = srtt + 4.0 * rttvar
3249-
timeout = max(min_to, min(max_to, timeout))
3270+
alpha = 1.0 / 8.0
3271+
beta = 1.0 / 4.0
3272+
_st["rttvar"] = (1 - beta) * _st["rttvar"] + beta * abs(_st["srtt"] - sample)
3273+
_st["srtt"] = (1 - alpha) * _st["srtt"] + alpha * sample
3274+
3275+
_st["timeout"] = _st["srtt"] + 4.0 * _st["rttvar"]
3276+
_st["timeout"] = max(min_to, min(max_to, _st["timeout"]))
32503277
try:
3251-
sock.settimeout(timeout)
3278+
sock.settimeout(_st["timeout"])
32523279
except Exception:
32533280
pass
32543281

32553282
def _loss_event():
3256-
nonlocal cwnd, cwnd_float
32573283
stats["loss_events"] += 1
3258-
cwnd = max(1, cwnd // 2)
3259-
cwnd_float = float(cwnd)
3284+
_st["cwnd"] = max(1, int(_st["cwnd"]) // 2)
3285+
_st["cwnd_float"] = float(_st["cwnd"])
32603286

32613287
def _ai_increase(acked_count):
32623288
# additive increase: cwnd += acked/cwnd (smoothed)
3263-
nonlocal cwnd, cwnd_float
32643289
if acked_count <= 0:
32653290
return
3266-
cwnd_float += float(acked_count) / max(1.0, float(cwnd))
3267-
new_cwnd = int(cwnd_float)
3268-
if new_cwnd > cwnd:
3269-
cwnd = min(max_window, new_cwnd)
3270-
cwnd_float = float(cwnd)
3291+
_st["cwnd_float"] += float(acked_count) / max(1.0, float(_st["cwnd"]))
3292+
new_cwnd = int(_st["cwnd_float"])
3293+
if new_cwnd > _st["cwnd"]:
3294+
_st["cwnd"] = min(max_window, new_cwnd)
3295+
_st["cwnd_float"] = float(_st["cwnd"])
32713296

32723297
def _send_pkt(seq, wire_payload, flags):
32733298
sock.sendto(_u_pack(flags, seq, total, tid) + wire_payload, addr)
@@ -3287,7 +3312,7 @@ def _read_chunk():
32873312

32883313
# Prime window
32893314
eof = False
3290-
while not eof and len(in_flight) < cwnd:
3315+
while not eof and len(in_flight) < _st["cwnd"]:
32913316
data = _read_chunk()
32923317
if data is None:
32933318
eof = True
@@ -3324,7 +3349,6 @@ def _read_chunk():
33243349
# Cumulative ACK: drop all <= ack_upto
33253350
for s in [s for s in list(in_flight.keys()) if s <= ack_upto]:
33263351
wire, ts, _tries, _dlen = in_flight[s]
3327-
# RTT sample from original send timestamp (works best if not retransmitted)
33283352
sample = now - ts
33293353
_update_rtt(sample)
33303354
del in_flight[s]
@@ -3366,7 +3390,7 @@ def _read_chunk():
33663390
now = time.time()
33673391
for seq in list(in_flight.keys()):
33683392
wire, ts, tries, dlen = in_flight[seq]
3369-
if (now - ts) >= timeout:
3393+
if (now - ts) >= _st["timeout"]:
33703394
if tries >= retries:
33713395
failed = True
33723396
in_flight.clear()
@@ -3380,7 +3404,7 @@ def _read_chunk():
33803404
break
33813405

33823406
# Fill window based on cwnd
3383-
while not eof and len(in_flight) < cwnd:
3407+
while not eof and len(in_flight) < _st["cwnd"]:
33843408
data = _read_chunk()
33853409
if data is None:
33863410
eof = True
@@ -3396,10 +3420,10 @@ def _read_chunk():
33963420
dur = max(1e-9, time.time() - t_start)
33973421
stats["duration_s"] = dur
33983422
stats["throughput_Bps"] = float(stats["bytes_sent_payload"]) / dur
3399-
stats["timeout"] = timeout
3400-
stats["srtt"] = srtt
3401-
stats["rttvar"] = rttvar
3402-
stats["cwnd_end"] = cwnd
3423+
stats["timeout"] = _st["timeout"]
3424+
stats["srtt"] = _st["srtt"]
3425+
stats["rttvar"] = _st["rttvar"]
3426+
stats["cwnd_end"] = _st["cwnd"]
34033427

34043428
if failed:
34053429
try:

0 commit comments

Comments
 (0)