paramiko的Python sftp错误

时间:2016-11-14 16:54:13

标签: python sftp paramiko

为什么我在调用get函数时会出现与paramiko连接丢失的错误?

  <nvd3 options="options" data="data" class="with-3d-shadow with-transitions"></nvd3>

我有一个70 MB的文件,在我收到错误后功能下载20mb。 当size文件小于20mb

时,这个函数很好用

这是paramiko日志文件:

class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):
    def missing_host_key(self, client, hostname, key):
        return

client = paramiko.SSHClient()
client.set_missing_host_key_policy(AllowAnythingPolicy())
client.connect('', username='',password='')
sftp.get('','')

和Python错误:

DEB [20161115-10:25:47.792] thr=1   paramiko.transport: starting thread (client mode): 0x472a3d0
DEB [20161115-10:25:47.793] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.0.2
DEB [20161115-10:25:47.793] thr=1   paramiko.transport: Remote version/idstring: SSH-2.0-SshServer
INF [20161115-10:25:47.794] thr=1   paramiko.transport: Connected (version 2.0, client SshServer)
DEB [20161115-10:25:47.795] thr=1   paramiko.transport: kex algos:['ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa'] client encrypt:['aes256-ctr', 'aes256-cbc'] server encrypt:['aes256-ctr', 'aes256-cbc'] client mac:['hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com'] server mac:['hmac-sha1', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com'] client compress:['none'] server compress:['none'] client lang:['en-US'] server lang:['en-US'] kex follows?False
DEB [20161115-10:25:47.795] thr=1   paramiko.transport: Kex agreed: diffie-hellman-group1-sha1
DEB [20161115-10:25:47.796] thr=1   paramiko.transport: Cipher agreed: aes256-ctr
DEB [20161115-10:25:47.796] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20161115-10:25:47.796] thr=1   paramiko.transport: Compression agreed: none
DEB [20161115-10:25:48.054] thr=1   paramiko.transport: kex engine KexGroup1 specified hash_algo <built-in function openssl_sha1>
DEB [20161115-10:25:48.054] thr=1   paramiko.transport: Switch to new keys ...
DEB [20161115-10:25:48.057] thr=1   paramiko.transport: userauth is OK
INF [20161115-10:25:48.137] thr=1   paramiko.transport: Authentication (password) successful!
DEB [20161115-10:25:57.677] thr=2   paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20161115-10:25:57.680] thr=1   paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20161115-10:25:57.681] thr=1   paramiko.transport: Secsh channel 0 opened.
DEB [20161115-10:25:57.682] thr=1   paramiko.transport: [chan 0] Sesch channel 0 request ok
INF [20161115-10:25:57.684] thr=2   paramiko.transport.sftp: [chan 0] Opened sftp connection (server version 3)
DEB [20161115-10:25:57.685] thr=2   paramiko.transport.sftp: [chan 0] stat(b'/GEO/OUT')
DEB [20161115-10:25:57.688] thr=2   paramiko.transport.sftp: [chan 0] normalize(b'/GEO/OUT')
DEB [20161115-10:25:57.690] thr=2   paramiko.transport.sftp: [chan 0] listdir(b'/GEO/OUT/.')
DEB [20161115-10:26:02.008] thr=2   paramiko.transport.sftp: [chan 0] stat(b'/GEO/OUT/test.csv')
DEB [20161115-10:26:02.012] thr=2   paramiko.transport.sftp: [chan 0] open(b'/GEO/OUT/test.csv', 'rb')
DEB [20161115-10:26:02.016] thr=2   paramiko.transport.sftp: [chan 0] open(b'/GEO/OUT/test.csv', 'rb') -> b'2f47454f2f4f55542f746573742e637376'
DEB [20161115-10:28:10.626] thr=1   paramiko.transport: EOF in transport thread
DEB [20161115-10:28:10.626] thr=2   paramiko.transport.sftp: [chan 0] close(b'2f47454f2f4f55542f746573742e637376')

2 个答案:

答案 0 :(得分:2)

我的问题的解决方案是:

tr = client.get_transport()
tr.default_max_packet_size = 100000000
tr.default_window_size = 100000000

答案 1 :(得分:0)

我猜你得到资源错误?随意更新问题,并提供实际错误。

如果错误与资源有关,我会尝试使用open而不是get,我会使用open和read,如下所示:

class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):
    def missing_host_key(self, client, hostname, key):
        return

client = paramiko.SSHClient()
client.set_missing_host_key_policy(AllowAnythingPolicy())
client.connect('', username='',password='')
file = sftp.open('','')
file.read # Will get you the file.

这是由于SFTP无法真正传输文件的问题。

另一种可能性是设置传输文件的块大小,如下所示:

remotefile = sftp.open('','')
with remotefile.file(remote_file_path, mode='w') as sftpfile:
    sftpfile.MAX_REQUEST_SIZE = 1024
    sftpfile.write(data)