summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorh3ll0r <h3ll0r@gmail.com>2021-04-26 16:02:38 +0200
committerTim Stolarski <t.stolarski@stolarski-it.de>2021-04-26 16:48:44 +0200
commitd4f5d725f75683879aadd08b3cd335db629eea2b (patch)
tree1871efe38dda3090c673181b5654e89affe546f5
parent33daa8e541cb91cdee01a085c368e1b388472e6a (diff)
downloadparamiko-d4f5d725f75683879aadd08b3cd335db629eea2b.tar.gz
Make prefetching optional in get() and getfo()
In some cases prefetching can lead to problems when downloading larger files. The connection to the SFTP server is disconnected due to the query behavior. Proprietary implementations in particular tend to behave that way. Switching off prefetching is a slow but resilient solution to this problem.
-rw-r--r--paramiko/sftp_client.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py
index 93190d85..88e6eee0 100644
--- a/paramiko/sftp_client.py
+++ b/paramiko/sftp_client.py
@@ -758,7 +758,7 @@ class SFTPClient(BaseSFTP, ClosingContextManager):
with open(localpath, "rb") as fl:
return self.putfo(fl, remotepath, file_size, callback, confirm)
- def getfo(self, remotepath, fl, callback=None):
+ def getfo(self, remotepath, fl, callback=None, prefetch=True):
"""
Copy a remote file (``remotepath``) from the SFTP server and write to
an open file or file-like object, ``fl``. Any exception raised by
@@ -771,18 +771,21 @@ class SFTPClient(BaseSFTP, ClosingContextManager):
:param callable callback:
optional callback function (form: ``func(int, int)``) that accepts
the bytes transferred so far and the total bytes to be transferred
+ :param bool prefetch:
+ option to deactivate prefetching
:return: the `number <int>` of bytes written to the opened file object
.. versionadded:: 1.10
"""
file_size = self.stat(remotepath).st_size
with self.open(remotepath, "rb") as fr:
- fr.prefetch(file_size)
+ if prefetch:
+ fr.prefetch(file_size)
return self._transfer_with_callback(
reader=fr, writer=fl, file_size=file_size, callback=callback
)
- def get(self, remotepath, localpath, callback=None):
+ def get(self, remotepath, localpath, callback=None, prefetch=True):
"""
Copy a remote file (``remotepath``) from the SFTP server to the local
host as ``localpath``. Any exception raised by operations will be
@@ -793,13 +796,15 @@ class SFTPClient(BaseSFTP, ClosingContextManager):
:param callable callback:
optional callback function (form: ``func(int, int)``) that accepts
the bytes transferred so far and the total bytes to be transferred
+ :param bool prefetch:
+ option to deactivate prefetching
.. versionadded:: 1.4
.. versionchanged:: 1.7.4
Added the ``callback`` param
"""
with open(localpath, "wb") as fl:
- size = self.getfo(remotepath, fl, callback)
+ size = self.getfo(remotepath, fl, callback, prefetch)
s = os.stat(localpath)
if s.st_size != size:
raise IOError(