Скорость загрузки при загрузке файла? - PullRequest
0 голосов
/ 29 сентября 2019

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()

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

вывод:

Итого: 10913768 # Хорошо

Загрузок: 385024 # Хорошо

Слева: 10528744 # Хорошо

Процент: 3,527874149423004 # Хорошо

Оценка: 2,3952649685390823e-07 # Неверно?я знаю, что это около 1,5

вопрос в том, как получить скорость загрузки, пока файл еще загружается.

1 Ответ

0 голосов
/ 29 сентября 2019

Вы используете текущую временную метку для расчета кбит / с.Вместо этого он должен работать следующим образом:

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()

Я не мог проверить это глубоко, но значения казались нормальными.Это файл размером 10 МБ, и до того, как предел скорости интернета был достигнут, он закончен.Я хотел протестировать другой файл, но он почему-то не работал, хотя я изменил URL и имя файла.

...