我正在编写代码来使用pycurl
下载文件。所以我想知道是否有可能暂停我的下载,然后从暂停的地方恢复下载?
pycurl 是否支持这些功能,或者是否有其他库支持暂停和恢复?
最佳答案
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 停止此下载并启动下载文件/文件夹的过程,它将从停止的位置开始。
关于python - 使用 pycurl 暂停和恢复下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13775892/