我如何跟踪使用ftp.retrbinary下载的百分比



我目前正在使用此代码来打印使用FTP.Retrbinary从FTP服务器下载的百分比,但是下载以0.27%的完成完成,并且仅表示SizeWrenten以27828224位完成。我要去哪里?

from ftplib import FTP
ftp = FTP('host')
ftp.login('usr','pass')
totalSize = ftp.size('100file.zip')
print(totalSize, "bytes")
def download_file(block):
    global sizeWritten
    file.write(block)
    sizeWritten += 1024
    print(sizeWritten, "= size written", totalSize, "= total size")
    percentComplete = sizeWritten / totalSize
    print (percentComplete, "percent complete")
try:
    file = open('100file.zip', "wb")
    ftp.retrbinary("RETR " + '100file.zip' ,download_file)
    print("Download Successful!")
except:
    print("Error")
file.close()
ftp.close()

你忽略了块的大小,假装每个块的大小是1k:

sizeWritten += 1024

将其更改为:

sizeWritten += len(block)

您的客户端可以使用blocksize参数将最大块大小发送到服务器。但是您没有通过一个,所以您会得到8192的默认值。

那么,如果您完全不在8倍,为什么不准确地获得12.5%?好吧,首先,最后一个区块几乎总是小于最大值,因此您应该期望超过12.5%。其次,您只给服务器一个 max 块大小;它可以自由地决定它永远不想发送超过4K,在这种情况下,您将获得超过25%。

最新更新