Search code examples
pythondownloadrate

Downloading Rate while downloading a file?



import time # to use it later with downloading Rate,

import urllib.request # to download a file
url = "http://xxxx.xxx.com/xxx/Setup.exe"

file_name = url[-9:] # file name will be setup.exe just for Ex .


class Download(): # i'm useing a Class but its ok if you have an answer in another way .

def __init__(self):
    urllib.request.urlretrieve(url, file_name, self.progress)

def progress(self,block,blocksize,total):
    print("Total : \t\t",total)
    downloaded = block * blocksize
    print("Downlaoded : \t\t",downloaded)
    left = total - downloaded
    print("Left : \t\t",left)
    percent =  downloaded * 100 / total
    print("Percent : \t\t",percent)
    print("Rate : \t\t",self.rate_return(downloaded)) # i dont get it . 



def rate_return(self,current_size):
    while True:
        # here is my problem ! i know its size / time to get Rate of downloading file .
        # but its totaly wrong :(
        return (current_size/1024)/time.time()
        # size / 1024 to convert it to KB . / time in seconds .

Download()

.................

the output :

Total : 10913768 # Good

Downlaoded : 385024 # Good

Left : 10528744 # Good

Percent : 3.527874149423004 # Good

Rate : 2.3952649685390823e-07 # Wrong ? i know its about 1.5

the question is how to get downloading rate while the file is still downloading .


Solution

  • You are using the current timestamp to calculate the kbps. Instead it should work like this:

    import time # to use it later with downloading Rate
    import urllib.request # to download a file
    
    # Downloading Sublime as an Example .
    url = "https://download.sublimetext.com/Sublime%20Text%20Build%203207%20x64%20Setup.exe"
    file_name = url[-9:] # file name will be setup.exe just for Ex .
    class Download(): # i'm useing a Class but its ok if you have an answer in another way .
        def __init__(self):
            self.start = time.time()
            urllib.request.urlretrieve(url, file_name, self.progress)
    
        def progress(self,block,blocksize,total):
            print("Total : \t\t",total)
            downloaded = block * blocksize
            print("Downlaoded : \t\t",downloaded)
            left = total - downloaded
            print("Left : \t\t",left)
            percent =  downloaded * 100 / total
            print("Percent : \t\t",percent)
            print("Rate : \t\t",self.rate_return(downloaded)) # i dont get it . 
    
        def rate_return(self,current_size):
            #while True:  # worthless
            # here is my problem ! i know its size / time to get Rate of downloading file .
            # but its totaly wrong :(
            return (current_size / 1024) / (time.time() - self.start)
            # size / 1024 to convert it to KB . / time in seconds .
    
    Download()
    

    I couldn't test it deeply but the values seemed ok. It is a 10MB file and before the limit of my Internet speed was reached it is finished. I wanted to test with another file but somehow it didn't work although i changed url and filename.