在使用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)