1.加入超时重试
This commit is contained in:
parent
da63baeb8d
commit
6e0056aed3
|
|
@ -14,19 +14,28 @@ def download_file(
|
||||||
server_name = server["name"]
|
server_name = server["name"]
|
||||||
chunk_read = 0
|
chunk_read = 0
|
||||||
s_time = datetime.datetime.now()
|
s_time = datetime.datetime.now()
|
||||||
with requests.get(url, stream=True) as r:
|
try:
|
||||||
for chunk in r.iter_content(chunk_size):
|
with requests.get(url, stream=True, timeout=longest_time) as r:
|
||||||
if chunk:
|
for chunk in r.iter_content(chunk_size):
|
||||||
chunk_read += len(chunk)
|
if chunk:
|
||||||
e_time = datetime.datetime.now()
|
chunk_read += len(chunk)
|
||||||
duration = (e_time - s_time).total_seconds()
|
e_time = datetime.datetime.now()
|
||||||
if chunk_read >= file_size:
|
duration = (e_time - s_time).total_seconds()
|
||||||
break
|
if chunk_read >= file_size:
|
||||||
if duration >= longest_time:
|
break
|
||||||
print('Longer than {longest_time} seconds. Stopped.'.format(longest_time=longest_time))
|
if duration >= longest_time:
|
||||||
break
|
print(
|
||||||
speed = chunk_read / duration / 1024 / 1024 * 8 # mega bit
|
"{server} longer than {longest_time} seconds. Stopped.".format(
|
||||||
model.add_record(server_name, speed)
|
longest_time=longest_time, server=server_name
|
||||||
# print(now_timestamp{} MB download".format(chunk_read / 1024 / 1024))
|
)
|
||||||
# print("{} seconds".format(duration / 1))
|
)
|
||||||
return speed
|
break
|
||||||
|
speed = chunk_read / duration / 1024 / 1024 * 8 # mega bit
|
||||||
|
model.add_record(server_name, speed)
|
||||||
|
print("{server}:{speed} Mbit/s".format(server=server_name, speed=speed))
|
||||||
|
except requests.exceptions.Timeout as e:
|
||||||
|
print(
|
||||||
|
"timeout before wait {timeout} seconds. Error:{err}".format(
|
||||||
|
err=str(e), timeout=longest_time
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
|
||||||
8
main.py
8
main.py
|
|
@ -5,7 +5,9 @@ import downloader
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
servers = [{"url": "http://67.209.190.15/1000M.bin", "name": "bwg"}]
|
servers = [
|
||||||
speed = downloader.download_file(servers)
|
{"url": "http://67.209.190.15/1000M.bin", "name": "bwg"},
|
||||||
print(speed)
|
{"url": "http://192.168.1.130:10010/1000MB.bin", "name": "local"},
|
||||||
|
]
|
||||||
|
downloader.download_file(servers)
|
||||||
print("Finished.")
|
print("Finished.")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue