我正在编写一个代码来使用pycurl
. 所以我想知道是否有可能暂停我的下载,然后从暂停的地方恢复它?是否pycurl
支持这些功能或者是否有任何其他支持暂停和恢复的库?
问问题
2960 次
2 回答
4
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 停止此下载并开始下载文件/文件夹的过程,它将从停止的位置开始。
于 2013-06-11T10:23:56.250 回答
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()
于 2012-12-08T20:05:04.467 回答