FTP下载进度条

时间:2019-08-22 08:46:53

标签: python python-3.x ftp progress-bar

我想从FTP服务器下载文件时看到进度栏。 我的代码看起来像这样,但是我遇到了很多错误,而且我不知道为什么会这样: 注意:这只是一个“测试”代码,应该只能打印完整百分比的下载文件。

def download_file():
    ftp = FTP('exampledomain.com')
    ftp.login(user='user', passwd='pass')

    ftp.cwd('/some_dir/')

    filename = 'file.txt'
    ftp.sendcmd("TYPE i")
    totalsize = ftp.size(filename)
    totalsize = round(totalsize / 1024 / 1024, 1)
    ftp.sendcmd("TYPE A")

    dltracker = FtpDownloadTracker(int(totalsize))
    with open(filename, 'wb') as localfile:
        ftp.retrbinary('RETR ' + filename, localfile.write, 1024, dltracker.handle)

    ftp.quit()
    localfile.close()

class FtpDownloadTracker():
    sizeWritten = 0
    totalSize = 0
    lastPercent = 0

    def __init__(self, totalsize):
        self.totalSize = totalsize

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

        if self.lastPercent != percentComplete:
            self.lastPercent = percentComplete
            print(str(percentComplete) + " percent complete")

我得到这些错误:

Traceback (most recent call last):
  File "D:/PythonProjects/PythonFTP/ftpdl.py", line 148, in run
    ftp.retrbinary('RETR ' + filename, localfile.write, 1024, dltracker.handle)
  File "C:\Python\lib\ftplib.py", line 442, in retrbinary
    with self.transfercmd(cmd, rest) as conn:
  File "C:\Python\lib\ftplib.py", line 399, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "C:\Python\lib\ftplib.py", line 364, in ntransfercmd
    self.sendcmd("REST %s" % rest)
  File "C:\Python\lib\ftplib.py", line 273, in sendcmd
    return self.getresp()
  File "C:\Python\lib\ftplib.py", line 246, in getresp
    raise error_perm(resp)
ftplib.error_perm: 554-REST needs a numeric parameter
554 Restart offset reset to 0

我在做什么错?我可以注意到有一个叫做“ raise error_perm(resp)”的东西,它是否具有服务器权限或其他权限?

0 个答案:

没有答案