下载较大文件时分段下载会加速下载过程,几乎所有下载软件都有类似的特性。在python中如何实现分段下载文件呢?
>>> import urllib2 >>> req = urllib2.Request('http://www.python.org/') #下载19000到20000字节的片段 >>> req.headers['Range'] = 'bytes=%s-%s' % (19000, 20000) >>> f = urllib2.urlopen(req) >>> pagerange = f.headers.get('Content-Range') >>> print pagerange bytes 19000-19189/19190 >>> print repr(f.read()) '</div>/n/tCopyright © 1990-2012, <a href=/'/psf//'>Python Software Foundation</a><br/>/n/t<a href="/about/legal">Legal Statements</a>/n </div>/n/n/n </div>/n </div>/n</body>/n</html>/n/n/n/n/n/n/n' >>>
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/8246.html