首页 > 解决方案 > Paramiko:为什么阅读这么慢?

问题描述

为什么这个脚本

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('remote.machine')
stdin, stdout, stderr = ssh.exec_command('cat 200mb.file')
stdin.close()
while not stdout.channel.exit_status_ready():
    while stdout.channel.recv_ready():
    #if stdout.channel.recv_ready():
        buf = stdout.channel.recv(4096)

需要几分钟

# ssh remote.machine 'cat 200mb.file' > 200mb.file

需要不到两秒?(是的,我知道由于 paramikos SSH 行为,我最终会丢失一些输出。)

任何建议或建议将不胜感激。

标签: python-3.xparamiko

解决方案


如果有人对答案感兴趣......这样我就可以几乎以本机速度读取数据(但可以进行更多优化):

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('remote.machine')
channel = ssh.get_transport().open_channel('session')
channel.exec_command('cat 200mb.file')

while True:
    (rlist, wlist, xlist) = select.select([channel], [], [], 1)
    if rlist:
        got_data = False
        if channel.recv_ready():
            data = channel.recv(4096)
            if data:
                got_data = True
                #sys.stdout.buffer.write(data)
        if channel.recv_stderr_ready():
            data = channel.recv_stderr(4096)
            if data:
                got_data = True
                #sys.stderr.buffer.write(data)
        if not got_data:
            break

# yes I saw situations where 'channel.exit_status_ready' is NOT ready here!
# (e.g. 'cat non-existent-file': in this case the stderr is small and
# sometimes being read so fast that 'channel.exit_status_ready' returns
# 'False' at first call!
while not channel.exit_status_ready():
    time.sleep(0.1)

#sys.stdout.write("exit code: '%s'\n" % channel.recv_exit_status())

推荐阅读