I'm trying to build a simple SFTP client using pysftp / paramiko.
How can I resume a transfer if it gets interrupted, e.g. if the connection fails? Is there a way to restart the transfer, find that the file already exists then begin transferring more data from the point where it got broken off?
I can't find any clear information on this in the documentation to either module. Is there nothing similar to the reget
command in OpenSSH?
Neither Paramiko nor pysftp support transfer resume. But if you take a look at the SFTPClient.get
implementation, it should be easy to implement the resume. The following should do:
if os.path.isfile(localpath):
localsize = os.stat(localpath)
else
localsize = 0
remotesize = sftp.stat(remotepath).st_size
if localsize < remotesize:
with open(localpath, "ab") as fl,
sftp.open(remotepath, "rb") as fr:
if localsize > 0:
fr.seek(localsize)
fr.prefetch(remotesize)
sftp._transfer_with_callback(
reader=fr, writer=fl, file_size=remotesize, callback=None)