Python3 download large files with requests 2.x

Source: Internet
Author: User
Tags windows x64

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"

    • Requests Official document: Http://docs.python-requests.org


Walker * * *


Python3 download large files with requests 2.x

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.