Environment
Os:windows x64
python:3.6.5 x64
requests:2.18.4
Code
# encoding=utf-8# author: walker# date: 2018-06-11# summary: use requests download large file import timeimport requests# Download a large file Def downonefile (srcUrl, LocalFile): print ('%s\n --->>>\n %s ' % (SRCURL, localfile)) starttime = time.time () with requests.get (srcurl, stream=true) as r: contentlength = int (r.headers[' content-length ')) line = ' CONTENT-LENGTH: %DB/ %.2FKB/ %.2FMB ' line = line % (contentlength, contentlength/1024, contentlength/1024/1024) print (line) downsize = 0 with open (localfile, ' WB ') as f: for chunk in r.iter_content ( 8192): if chunk: f.write (Chunk) downsize += len (Chunk) line = '%d kb/s - %.2f mb, Total %.2f  MB ' line = line % (downsize/1024/(Time.time ()-starttime), downsize/1024/1024, contentlength/1024/1024) &NBSp; print (line, End= ' \ R ') if downSize >= contentLength: break timecost = time.time () - startTime line = ' Total time: %.2f s, average speed: %.2f kb/s ' line = line % (timecost, downsize/1024/timecost) print (line) if __name__ == ' __main__ ': srcurl = r ' http://cachefly.cachefly.net/100mb.test ' localfile = r ' d:\ Python3project\test\out\out.test ' &Nbsp; downonefile (Srcurl, localfile)
【】
"Related reading"
Walker * * *
Python3 download large files with requests 2.x