我正在编写使用pycurl
下载文件的代码。所以我想知道有可能暂停我的下载,然后从暂停的任何地方恢复它吗?
pycurl
是否支持这些功能,还是有其他支持暂停和恢复的库?
答案 0 :(得分:3)
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停止下载并开始下载文件/文件夹,则从停止的位置开始。
答案 1 :(得分:0)
如果您停止线程并关闭连接,则可以使用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()