用pycurl暂停并恢复下载 [英] Pause and resume downloading with pycurl

查看:119
本文介绍了用pycurl暂停并恢复下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pycurl 编写代码来下载文件。所以我想知道这是否有可能暂停我的下载,然后从暂停的任何地方恢复它?
pycurl 支持这些功能,还是有任何其他支持暂停和恢复的库?

解决方案

  import os 
import pycurl
import sys


def progress ,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 %%%(文件下载 - ,frac * 100))

url = raw_input('输入URL下载文件夹/文件:')
filename = url.split(/)[ - 1] .strip()


def test debug_type,debug_msg):
printdebug(%d):%s%(debug_type,debug_msg)

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

#设置写入
如果os。 path.exists(filename):
f = open(filename,ab)
c.setopt(pycu r.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()
除了:
pass
pre>

可以下载文件,例如 - * .tar,.dmg,,exe,jpg等。



使用pycurl进行恢复下载。



将此代码测试为:




  • 在终端上运行此文件,如:



这是终端上的登录,

  anupam @ anupampc:〜/ Documents $ python resume_download.py 

输入URL下载文件夹/ file:http://nightly.openerp.com/6.0/6.0/openerp-allinone-setup-6.0-20110625-r3451.exe

文件下载 - 199%

如果通过按Ctrl + C停止此下载,并开始下载文件/文件夹,则从停止的位置开始。 / p>

I'm writing a code to download files using pycurl. So I want to know that is there a possibility to pause my download and then resume it from wherever it's been paused? Does pycurl support these features or is there any other library which support pause and resume?

解决方案

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

Possible to download file for example - *.tar, .dmg, ,exe, jpg etc .

Used pycurl for resume download.

Testing of this code as:

  • Run this file on terminal like:

Here is the log on terminal,

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%

If you stop this download by pressing Ctrl + C and starts process to download file/folder , it start from where it was stopped.

这篇关于用pycurl暂停并恢复下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆