Popen.stdin.write文件不适用于Python3

2024-03-29 14:15:18 发布

您现在位置:Python中文网/ 问答频道 /正文

你知道吗Popen.stdin.write文件不适用于Python3

我正在启动到远程设备的telnet连接,并开始发送命令。 下面是一个简单的示例,它与远程设备建立telnet连接,并开始发送设备specfifc命令[即“vr”,“KP on”]


import subprocess

telnet_client = subprocess.Popen(
    ["telnet", "192.168.1.120", "4002"],
    stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
    universal_newlines=True)

#Get Version
telnet_client.stdin.write('vr\n')
telnet_client.stdin.flush()

#Power on the device
telnet_client.stdin.write('kp on\n')
telnet_client.stdin.flush()

#Factory default
telnet_client.stdin.write('fd\n')
telnet_client.stdin.flush

每个stdin.write()不抛出任何异常或错误,只返回写入的字节数。但事实上,信息并没有被发送出去。你知道吗

Ubuntu机器1:

我观察到在机器中安装Python3之后,上面的实现停止使用Python2.7

Ubuntu机器2:

在安装Python3之后的另一台Ubuntu机器上,上面的实现可以与Python2.7一起使用,但不能与Python3一起使用。你知道吗

所以普通的事情,在任何方向Popen.stdin.write都不适用于Python3。我相信有些包裹正在破坏系统。你知道吗

请告诉我这件事


Tags: 命令client机器远程onubuntustdinpython3
2条回答

我也面临同样的问题。process.stdin.write()适用于Python2.7,但不适用于Python3。虽然我无法调试问题所在,但我能够通过process.communicate()实现功能。Documentation。这是与进程通信的推荐方式。另外,作为输入传递给此方法的字符串需要以'\n'结尾。你知道吗

一切就绪。你知道吗

实际上服务器在调用标准冲洗你知道吗

telnet_client.stdin.write('vr\n')
telnet_client.stdin.flush()

相关问题 更多 >