Acquaintance python: file download progress

(To be updated follow-up ...)

Create a "callback" method using urlretrieve request: Download progress

Detailed code is as follows:

# ! / The User / bin env Python 
# author: the Simple-Sir 
# Time: 2019/8/8 21:00 
# download progress 
from urllib Import Request
 Import Time 

DEF downPercent (blocknum, blockSize, totalSize):
     '' ' 
    callback function, download progress 
    : param blockNum: downloaded data blocks 
    : param blockSize: the data block size 
    : param totalSize: to download the total file size 
    '' ' 
    downed = blocknum * blockSize / 1024   # downloaded size kB 
    totalSize = totalSize / 1024   # total size kB 
    the p-round = (downed / totalSize * 100,2)   # downloaded percentage
    = totalSize-downed downWait   # to be downloaded size kB 
    IF P> 100 : 
        P = 100
     IF downWait < 0: 
        downWait = 0 
    nowtime = The time.strftime ( ' % Y-M-% D%% H:% M:% S ' , time.localtime (the time.time ()))
     Print ( ' {} total file size: {} ' .format (nowtime, totalSize))
     Print ( ' {} downloaded: {} (\ 033 [36 ; 1m { %} \ 033 [0m) ' .format (nowtime, downed, P))
     Print ( ' {} to be downloaded: {} (\ 033 [31 is; 1M%} {\ 033 [0m) '.format (nowtime, downWait, round (100-P, 2 ))) 

DEF downloads (): 
    URL = ' https://cdn.mysql.com//Downloads/MySQLInstaller/mysql-installer-community-5.7.27.0. the MSI '   # MySQL database download 
    startTime = time.time () 
    request.urlretrieve (url, ' MySQL-Installer-Community Community-5.7.27.0.msi ' , downPercent) 
    endTime = time.time ()
     Print ( ' file download is complete! total time: ' , The time.strftime ( ' % M:% S ' , time.localtime (endTime- the startTime))) 

IF  the __name__ =='__main__':
    download()
They download progress

Implementation process:

 

Guess you like

Origin www.cnblogs.com/simple-li/p/11324455.html