diff options
-rw-r--r-- | paramiko/sftp_client.py | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index b953526c..6a24b943 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -591,6 +591,18 @@ class SFTPClient(BaseSFTP, ClosingContextManager): """ return self._cwd and u(self._cwd) + def _transfer_with_callback(self, reader, writer, file_size, callback): + size = 0 + while True: + data = reader.read(32768) + writer.write(data) + size += len(data) + if len(data) == 0: + break + if callback is not None: + callback(size, file_size) + return size + def putfo(self, fl, remotepath, file_size=0, callback=None, confirm=True): """ Copy the contents of an open file object (``fl``) to the SFTP server as @@ -620,15 +632,9 @@ class SFTPClient(BaseSFTP, ClosingContextManager): """ with self.file(remotepath, 'wb') as fr: fr.set_pipelined(True) - size = 0 - while True: - data = fl.read(32768) - fr.write(data) - size += len(data) - if callback is not None: - callback(size, file_size) - if len(data) == 0: - break + size = self._transfer_with_callback( + reader=fl, writer=fr, file_size=file_size, callback=callback + ) if confirm: s = self.stat(remotepath) if s.st_size != size: @@ -688,16 +694,9 @@ class SFTPClient(BaseSFTP, ClosingContextManager): with self.open(remotepath, 'rb') as fr: file_size = self.stat(remotepath).st_size fr.prefetch() - size = 0 - while True: - data = fr.read(32768) - fl.write(data) - size += len(data) - if len(data) == 0: - break - if callback is not None: - callback(size, file_size) - return size + return self._transfer_with_callback( + reader=fr, writer=fl, file_size=file_size, callback=callback + ) def get(self, remotepath, localpath, callback=None): """ |