Python使用urllib2模块实现断点续传下载的方法
本文实例讲述了Python使用urllib2模块实现断点续传下载的方法。分享给大家供大家参考。具体分析如下:
在使用HTTP协议进行下载的时候只需要在头上设置一下Range的范围就可以进行断点续传下载,当然,首先服务器需要支持断点续传。
利用Python的urllib2模块完成断点续传下载的例子:
#!/usr/bin/python
#-*-coding:UTF-8-*
'''
Createdon2013-04-15
CreatedbyRobinTang
AdemoforResumingTransfer
'''
importurllib2
req=urllib2.Request('http://www.python.org/')
req.add_header('Range','bytes=0-20')
#settherange,from0byteto19byte,20byteslen
res=urllib2.urlopen(req)
data=res.read()
printdata
print'---------'
print'len:%d'%len(data)
希望本文所述对大家的Python程序设计有所帮助。