【发布时间】:2012-11-26 09:17:37
【问题描述】:
我正在编写代码以使用pycurl 下载文件。所以我想知道是否有可能暂停我的下载,然后从暂停的地方恢复它?
pycurl 是否支持这些功能,或者是否有任何其他支持暂停和恢复的库?
【问题讨论】:
我正在编写代码以使用pycurl 下载文件。所以我想知道是否有可能暂停我的下载,然后从暂停的地方恢复它?
pycurl 是否支持这些功能,或者是否有任何其他支持暂停和恢复的库?
【问题讨论】:
如果您停止线程并关闭连接,您可以使用 HTTP Content-Range 从中断的地方继续您的请求。只需找出您已经有多少字节,然后使用 RESUME_FROM 从那里开始:
import pycurl
starting_point = 999 # calculate this
url="http://test-url"
curl = pycurl.Curl()
curl.setopt(curl.URL, url)
curl.setopt(curl.RESUME_FROM, starting_point)
curl.perform()
curl.close()
【讨论】:
import os
import pycurl
import sys
def progress(total, existing, upload_t, upload_d):
existing = existing + os.path.getsize(filename)
try:
frac = float(existing)/float(total)
except:
frac = 0
sys.stdout.write("\r%s %3i%%" % ("File downloaded - ", frac*100))
url = raw_input('Enter URL to download folder/file: ')
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)
try:
c.perform()
except:
pass
可以下载文件,例如 - *.tar, .dmg, ,exe, jpg 等。
使用 pycurl 进行简历下载。
测试此代码为:
这里是登录终端,
anupam@anupampc:~/Documents$ python resume_download.py
Enter URL to download folder/file: http://nightly.openerp.com/6.0/6.0/openerp-allinone-setup-6.0-20110625-r3451.exe
File downloaded - 199%
如果您通过按 Ctrl + C 停止此下载并开始下载文件/文件夹的过程,它将从停止的位置开始。
【讨论】: