Home  >  Article  >  Backend Development  >  Python使用urllib2模块实现断点续传下载的方法

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

WBOY
WBOYOriginal
2016-06-10 15:10:27954browse

本文实例讲述了Python使用urllib2模块实现断点续传下载的方法。分享给大家供大家参考。具体分析如下:

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

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

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

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn