如何读取ssh remote用python编写的stdin

2024-04-18 23:37:58 发布

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

我在做一个ssh远程实时控制的raspberry-pi四直升机。你知道吗

现在我遇到了一些关于如何通过ssh子进程的stdin发送数据的问题。编程语言是python3

我想要实现的是我有一个本地python程序,ssh将raspberry pi的quadcopter程序作为子进程执行,并通过将其写入子进程的stdin来发送用户命令。 我的四架直升机应该能够通过stdin接收用户命令,但不会阻塞整个程序循环。你知道吗

从我的本地控制器程序应该是这样的

...
ssh_proc = subprocess.Popen(
    ['python3','test.py'], # for now I'm using test py to debug
    stdin = subprocess.PIPE,
    stdout = subprocess.PIPE,
    stderr = subprocess.PIPE
)

...
# when I send command
ssh_proc.stdin.writeline(my command)

以及我的测试接收器程序:

import time
while (1):
    print('read from stdin', sys.stdin.readline(), flush=True)
    time.sleep(0.01)

应该有一些回声输出供我调试,但我什么都没有。你知道吗


Tags: 用户pytest命令程序进程stdinpi