如何解决如何在使用 Python 下载块中的文件时获得 ETA?
我有这个代码可以下载块中的文件并显示进度条,我设法使用它获得了下载速度。但它是如何获得 ETA 的?
我的代码:
import time
import aiohttp
from my_tools import humanbytes
async def download_file(message,url,file_name):
with open(file_name,mode='wb') as f:
s_time = int(time.perf_counter())
async with aiohttp.ClientSession() as session:
async with session.get(url) as r:
total_length = r.headers.get('content-length') or r.headers.get("Content-Length")
dl = 0
if total_length is None:
f.write(r.content)
else:
total_length = int(total_length)
async for chunk in r.content.iter_chunked(max(int(total_length/1000),1024*1024)):
dl += len(chunk)
f.write(chunk)
done = int(50 * dl / total_length)
r_ = f"<b><u>Downloading This File</b></u> \n<b>File :</b> <code>{file_name}</code> \n<b>File Size :</b> <code>{humanbytes(total_length)}</code> \n<b>Downloaded :</b> <code>{humanbytes(dl)}</code> \n{'█' * done}{' ' * (50-done)} \n\n<b>ETA :</b> <code>{dl//(time.perf_counter() - s_time)}</code>"
print(r)
await download_file(message,'https://www.python.org/static/img/python-logo@2x.png','boyka.png')
请帮忙!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。