PycURL 续传功能

1 投票
1 回答
1279 浏览
提问于 2025-04-15 23:52

我好像无法让RESUME_FROM这个选项正常工作。下面是我一直在测试的一些示例代码:

import os
import pycurl
import sys

def progress(total, existing, upload_t, upload_d):
    try:
        frac = float(existing)/float(total)
    except:
        frac = 0
    sys.stdout.write("\r%s %3i%%" % ("file", frac*100)  )

url = "http://launchpad.net/keryx/stable/0.92/+download/keryx_0.92.4.tar.gz"
filename = url.split("/")[-1].strip()

def test(debug_type, debug_msg):
    print "debug(%d): %s" % (debug_type, debug_msg)

c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)

# Setup writing
if os.path.exists(filename):
    f = open(filename, "ab")
    c.setopt(pycurl.RESUME_FROM, os.path.getsize(filename))
else:
    f = open(filename, "wb")
c.setopt(pycurl.WRITEDATA, f)

#c.setopt(pycurl.VERBOSE, 1)
c.setopt(pycurl.DEBUGFUNCTION, test)
c.setopt(pycurl.NOPROGRESS, 0)
c.setopt(pycurl.PROGRESSFUNCTION, progress)
c.perform()

1 个回答

3

其实它是正常恢复的,不过看起来像是又从头开始了,因为在进度函数里没有把文件的大小(通过os.path.getsize(filename)获取)加到已有的进度上。这只是一个小错误!:)

撰写回答