Python 使用urllib2模块实现断点续传下载

来源:互联网 发布:大数据可视化技术招聘 编辑:程序博客网 时间:2024/05/22 07:54

在使用HTTP协议进行下载的时候只需要在头上设置一下Range的范围就可以进行断点续传下载,当然,首先服务器需要支持断点续传。

利用Python的urllib2模块完成断点续传下载的例子:

#!/usr/bin/python# -*- coding: UTF-8 -*'''Created on 2013-04-15Created by RobinTangA demo for Resuming Transfer'''import urllib2req = urllib2.Request('http://www.python.org/')req.add_header('Range', 'bytes=0-20') # set the range, from 0byte to 19byte, 20bytes lenres = urllib2.urlopen(req)data = res.read()print dataprint '---------'print 'len:%d'%len(data)


原创粉丝点击