本文實例講述了Python使用urllib2模塊實現斷點續傳下載的方法。分享給大家供大家參考。具體分析如下:
在使用HTTP協議進行下載的時候只需要在頭上設置一下Range的范圍就可以進行斷點續傳下載,當然,首先服務器需要支持斷點續傳。
利用Python的urllib2模塊完成斷點續傳下載的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#!/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程序設計有所幫助。