In this Stack Overflow question I asked about creating a Python program of mine which was meant to allow me to access the terminal from a Python shell. It did not work... at least when running the program through startx
. But, interestingly enough, when I ran it through ssh
(my Pi was running in console mode) the program ran like a charm. The code is below:
import subprocess
import socket
import sys
import platform
import os
print "WELCOME TO xTROL. \n"
pyVER = sys.version
OS = platform.system()
print pyVER, "\n"
print "Running on %s" % (OS)
while True:
INPUT = raw_input(">>>")
cmd_FORMAT = INPUT.split()
try:
output = subprocess.Popen(cmd_FORMAT, stdout=subprocess.PIPE).communicate()[0]
print output
except Exception:
print "Something went wrong."
Why is this?
EDIT
By did not work, I mean that the program did not work interactively. Writing Python
in the startx
version would return a blank line, in console mode I entered the interactive prompt.
I ran the startx
Python program by double clicking on the item, opening idle, and then executing the program.
1 Answer 1
A terminal emulator needs to deal with three streams: stdin
for input, stdout
for normal display and stderr
for error output. Your program only captures stdout
and relies on the terminal emulator you're running it from to handle the others for you.
What the right thing for you to do is dependant on what you're trying to achieve. If you want to write your own version of something like SSH then you should create a pseudo terminal and then connect the remote program to that with you sending commands to it. I.e. you write to the ptty which then turns up on stdin
and you read from it to capture the program's stdout
and stderr
.
This page will tell you more than you ever wanted to know about this. If you can understand what it's telling you I think you can work out what strategy to pursue. http://www.linusakesson.net/programming/tty/index.php
Python
opened an interactive shell over ssh, but did nothing on startx).xinitrc
?