I want to talk to my raspberry pi 3 using SSH on an Android device. How can I make my python script listen on the commands sent through SSH ?
I use to be able to do that on my Arduino and would like to do the same on my PI can I do that ?
I want to talk to my raspberry pi 3 using SSH on an Android device. How can I make my python script listen on the commands sent through SSH ?
I use to be able to do that on my Arduino and would like to do the same on my PI can I do that ?
Ok, so after countless hours looking through Python code and a bunch of trial and error. I finally found something that works for me. Here's the code I'm using on my raspberry pi to read the terminal :
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)
So I start my python code from the Terminal using python3 myscript.py and what ever I input get's trapped.
Works like a charm. Hope I can help someone else with this.
Forgot to add the link to the answer I found : What's the simplest way of detecting keyboard input in python from the terminal?