python-3.x - 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 行为,我最终会丢失一些输出。)
任何建议或建议将不胜感激。
解决方案
如果有人对答案感兴趣......这样我就可以几乎以本机速度读取数据(但可以进行更多优化):
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())
推荐阅读
- c# - RDLC 报告 C#。通过调节显示或隐藏控件
- python - 如何将openssl连接到Python?
- python - 将 \ucweirdnumberscharacters 翻译成英文?
- python - Python 字典中键的值未更新
- r - 当我使用 as.POSIXct 命令时,该列变为空
- git - ExtJS 6 将 workspace.json 添加到 gitignore
- mercurial - 如何判断存储库是否有来自 shell 脚本的传出更改并安全地检测到真正的错误?
- git - AWS CodeCommit - 致命:未找到存储库“https://git-codecommit.us-east-1..”
- php - CURL:主机名和端口后的路径
- sql - 使用 MS Access 或 SQL Server 的考勤日志