通过 SSH 与 raspberry pi 通信

Communication to the raspberry pi through SSH

我想在 Android 设备上使用 SSH 与我的 raspberry pi 3 通话。如何让我的 python 脚本监听通过 SSH 发送的命令?

我过去可以在我的 Arduino 上这样做,我想在我的 PI 上做同样的事情,我可以这样做吗?

好吧,在无数个小时查看 Python 代码和大量试错之后。我终于找到了适合我的东西。这是我在 raspberry pi 上用来读取终端的代码:

import termios, fcntl, sys, os
fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
    while 1:
        try:
            c = sys.stdin.read(1)
            print "Got character", repr(c)
        except IOError: pass
finally:
    termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
    fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

所以我使用 python3 myscript.py 从终端启动我的 python 代码,我输入的任何内容都被困住了。

很有魅力。希望我可以帮助其他人。

忘记将 link 添加到我找到的答案中:What's the simplest way of detecting keyboard input in python from the terminal?