【发布时间】:2026-02-14 05:15:02
【问题描述】:
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 # 好
Rate : 2.3952649685390823e-07 # 错了吗?我知道它大约是 1.5
问题是如何在文件仍在下载时获得下载速率。
【问题讨论】:
-
我使用了内置的 curl 调用,它已经足够详细了。看看这是否适合你。 curl -o {} {}'.format(path, url)