首页 / 更多教程 / paramiko远程持续获取内容
paramiko远程持续获取内容
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了paramiko远程持续获取内容,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1351字,纯文字阅读大概需要2分钟。
内容图文
在使用paramiko时,我们在用exec_command
(command) 时更多的是一起读回数据,但实际官方还有更详细的说明。
当前遇到的需求就是,当执行某个命令时,有进度条的输出,我们需要持续获取进度条输出,而不是执行完成后再输出进度条。
再看看官网的说明:
exec_command(command) Execute a command on the server. If the server allows it, the channel will then be directly connected to the stdin, stdout, and stderr of the command being executed. When the command finishes executing, the channel will be closed and can’t be reused. You must open a new channel if you wish to execute another command. Parameters: command (str) – a shell command to execute. Raises: SSHException – if the request was rejected or the channel was closed
这是执行。
我们再看看返回的说明:
recv(nbytes) Receive data from the channel. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by nbytes. If a string of length zero is returned, the channel stream has closed. Parameters: nbytes (int) – maximum number of bytes to read. Returns: received data, as a str/bytes. Raises: socket.timeout – if no data is ready before the timeout set by settimeout.
解释一下,也就是当默认时,recv会一次获取所有的内容。
那么也就是我们可以通过这个方式获取进度条的实时输出
简单的代码如下:
_, stdout, stderr = self.ssh.exec_command(cmd, get_pty=True) while True: v = stdout.channel.recv(1024) ifnot v: breakprint(str(v)) time.sleep(3)
每隔3秒会去读取一次标准输出的内容
原文:https://www.cnblogs.com/landhu/p/15222967.html
内容总结
以上是互联网集市为您收集整理的paramiko远程持续获取内容全部内容,希望文章能够帮你解决paramiko远程持续获取内容所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。