使用时间和大小计算下载速度

时间:2014-06-19 11:06:32

标签: python ftp file-transfer

如何使用文件大小和下载文件所需的时间(以秒为单位?)来获取正在下载的文件的平均速度(以KBps和kbps为单位)。

然后我还需要采用5次传输的平均速度(以KBps和kbps为单位)。

这就是我所拥有的,我不确定它是否是正确的开始?

start_time = time.time()
ftp.storbinary('STOR stor.zip', file, 1024, uploadTracker.handle)
elapsed_time = time.time() - start_time
totalSizeInKiloBytes = totalSize / 1024
print "\n KB/PS: ", totalSizeInKiloBytes / elapsed_time

由于

编辑: 这是输出;

Progress: 100% [\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\]  50.00 kB/s
 Elapsed Time: 8.30499982834

 Size in Bytes: 52428800

 KB/PS:  6164.96099438

KB / PS输出似乎错了?当hte进度条结束时的速度从未超过~100 kB / s时,怎么能是6164

编辑:

FtpUploadTracker类

class FtpUploadTracker:
    sizeWritten = 0
    totalSize = 0
    lastShownPercent = 1
    percentComplete = 0
    count = 0

    def __init__(self, totalSize):
        self.totalSize = totalSize
        self.count = 0

    def handle(self, block):
        self.sizeWritten += 1024
        self.percentComplete = round((self.sizeWritten / self.totalSize) * 100)
        if(self.percentComplete == 100):
            self.count = 1501

        if(self.count >= 1500): #count acts as a timer to only update percent complete text every ~1.5 seconds
            if (self.lastShownPercent != self.percentComplete):
                self.lastShownPercent = self.percentComplete
                pbar = ProgressBar(widgets=widgets, maxval=100)
                pbar.start()
                pbar.update(self.percentComplete)
                self.count = 0
        else:
            self.count += 1

0 个答案:

没有答案