I am in need of python sftp client to download files from a sftp server. I started to use Paramiko. Small files in KB works well but however when I try to download 600 MB of file, it hangs indefinitely after downloading 20 MB of file. Unable to figure out what the issue is. Increasing the window size did not solve either. Any help would be much appreciated!
host = config.getsafe(section, "host")
username = config.getsafe(section, "username")
port = config.getsafe(section, "port")
remote_dir = config.getsafe(section, "remote_dir")
download_dir = config.getsafe(section, "download_dir")
archive_dir = config.getsafe(section, "archive_dir") if config.has_option(section, "archive_dir") else \
None
password = config.getsafe(section, "password") if config.has_option(section, "password") else None
file_pattern = config.getsafe(section, "file_pattern") if config.has_option(section, "file_pattern") \
else "*"
passphrase = config.getsafe(section, "passphrase") if config.has_option(section, "passphrase") else None
gnupg_home = config.getsafe(section, "gnupg_home") if config.has_option(section, "gnupg_home") else None
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, port=int(port), username=username, password=password)
sftp = ssh.open_sftp()
sftp.sshclient = ssh
sftp.get("/SFTP/PL_DEV/test.dat", "C:/import/download/test.dat")
I did two things to solve a similar problem:
increase window size – you say you tried that too; for me, this helped to get from a few ten MBs to half a GB but no further
effectively disable rekeying – this might have security implications, but helped me to get files over a GB from a weird windows sftp server
with paramiko.Transport((_SFTP['host'], 22)) as transport:
# SFTP FIXES
transport.default_window_size=paramiko.common.MAX_WINDOW_SIZE
transport.packetizer.REKEY_BYTES = pow(2, 40) # 1TB max, this is a security degradation!
transport.packetizer.REKEY_PACKETS = pow(2, 40) # 1TB max, this is a security degradation!
# / SFTP FIXES
transport.connect(username=_SFTP['user'], password=_SFTP['password'])
with paramiko.SFTPClient.from_transport(transport) as sftp:
listdir = sftp.listdir()
# ...
sftp.get(remotepath=filename, localpath=localpath)
Increasing default_max_packet_size and default_window_size as follows worked for me:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
client.connect(hostname, username=username, password=password, port=port)
tr = client.get_transport()
tr.default_max_packet_size = 100000000
tr.default_window_size = 100000000
sftp = client.open_sftp()
sftp.get(remote_file, local_filepath)
client.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With