【问题标题】:How to compute the progress of a download given the chunk number, its size and the total size of the download?给定块数、大小和下载的总大小,如何计算下载进度?
【发布时间】:2017-07-18 16:57:32
【问题描述】:

Python 的 urllib.request.urlretrieve() 接受一个函数,该函数将在从网络中获取每个块时调用。

该函数使用三个参数调用:块的渐进式标识符、其大小和下载的总大小。

鉴于这三个信息,我可以计算已经获取的字节数吗?这将用于计算下载进度。

我很想这样做chunk_number * chunk_size / download_size,但我不确定所有块的块大小都是恒定的。

【问题讨论】:

  • 不幸的是,没有理由假设块的大小都相同!如果您想提供准确的下载百分比,您可能必须保持一个运行总和(例如total_bytes += most_recent_chunk.size

标签: python http urllib chunked-encoding


【解决方案1】:

您可以保持目前为止看到的所有尺寸的总和。

试试这个:

import urllib.request


def my_downloader(url, filename = None):
    running_total = 0
    def my_reporthook(count, size, total):
        nonlocal running_total
        running_total += size
        print ("{}%".format(100*running_total//total))
    return urllib.request.urlretrieve(url, filename, my_reporthook)

print (my_downloader('https://www.gutenberg.org/files/55146/55146-h.zip'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    相关资源
    最近更新 更多