如何从ftputil上传方法传递块长度

时间:2014-03-12 13:33:09

标签: python-2.7 python-3.x ftputil

为了在ftputil lib上使用进度指示器,我怎么能传递块长度?

实际代码:     import ftputil

class FileTransferUtils(object):

    def __init__(self, address, user, password, remote_path, port):
        self.host = address.split(':')[0]
        self.start = ftputil.FTPHost(self.host, user, password, port=port, session_factory=OpenFTPSession)
        self.start.chdir(remote_path)
        self.remote_path = remote_path


    def upload(self, filename):
        try:
            # how to pass the chunk size?
            self.start.upload(filename, filename, callback=None)
            return True
        except Exception:
            print('Upload failed')
            raise
        finally:
            self.start.close()

提前致谢。

1 个答案:

答案 0 :(得分:1)

为了解决这个问题,我们创建了一个新函数test_callback来打印进度:

class FileTransferUtils(object):

    def __init__(self, address, user, password, remote_path, port):
        self.host = address.split(':')[0]
        self.start = ftputil.FTPHost(self.host, user, password, port=port, session_factory=OpenFTPSession)
        self.start.chdir(remote_path)
        self.remote_path = remote_path
        self.total_size = 0
        self.received = 0

    def test_callback(self, chunk):
        self.received += len(chunk)
        print("     {} Progress : {}Kb / {}Kb".format(
            'Uploading', float(self.received)/1000, self.total_size), end='\r'
        )    

    def upload(self, filename):
        try:
            def test_callback(i):
                print(" .", end='')

            self.start.upload(filename, filename, callback=self.test_callback)
        except Exception:
            print('Upload failed')
            raise
        finally:
            self.total_size = 0
            self.received = 0               
            self.start.close()