【问题标题】:Python URLRetrieve Limit Rate and Resume Partial DownloadPython URLRetrieve 限制速率并恢复部分下载
【发布时间】:2010-12-26 09:08:41
【问题描述】:

我正在使用来自this thread 的代码来限制我的下载速率。

如何将部分下载恢复与速率限制代码合并?我发现的示例使用urlopen 而不是urlretrieve,而RateLimit 类依赖于urlretrieve

我想要一个控制部分下载的外部函数,而不必更改RateLimit 类:

from throttle import TokenBucket, RateLimit

def retrieve_limit_rate(url, filename, rate_limit):
    """Fetch the contents of urls"""
    bucket = TokenBucket(10*rate_limit, rate_limit)

    print "rate limit = %.1f kB/s" % (rate_limit,)

    print 'Downloading %s...' % filename
    rate_limiter = RateLimit(bucket, filename)
    #
    # What do I put here to allow resuming files?
    #
    return urllib.urlretrieve(url, filename, rate_limiter)

【问题讨论】:

标签: python limit rate resume-download


【解决方案1】:

也许可以改用 PyCurl:

def curl_progress(total, existing, upload_t, upload_d):
    try:
        frac = float(existing)/float(total)
    except:
        frac = 0
    print "Downloaded %d/%d (%0.2f%%)" % (existing, total, frac)

def curl_limit_rate(url, filename, rate_limit):
    """Rate limit in bytes"""
    import pycurl
    c = pycurl.Curl()
    c.setopt(c.URL, url)
    c.setopt(c.MAX_RECV_SPEED_LARGE, rate_limit)
    if os.path.exists(filename):
        file_id = open(filename, "ab")
        c.setopt(c.RESUME_FROM, os.path.getsize(filename))
    else:
        file_id = open(filename, "wb")

    c.setopt(c.WRITEDATA, file_id)
    c.setopt(c.NOPROGRESS, 0)
    c.setopt(c.PROGRESSFUNCTION, curl_progress)
    c.perform()

【讨论】:

    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 2023-04-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    相关资源
    最近更新 更多