python-2.7python-3.xftputil

How to pass chunk length from upload method on ftputil


In order to use the progress indicator avaliable on ftputil lib, how could i pass the chunk length?

actual code: 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()

Thanks in advance.


Solution

  • To solve this problem a new function test_callback was created for print the progress:

    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()