Is it possible to combine incoming requests and TTY requests in Python CLI scripts? For example, do the following:
import sys
piped_text = None
if not sys.stdin.isatty():
piped_text = sys.stdin.read()
user_in = raw_input('Enter something: ')
if piped_text:
sys.stdout.write(piped_text)
sys.stdout.write(user_in + '\n')
It produces the following output:
~: python mrprompt.py
Enter something: something
something
~: echo foo | python mrprompt.py
Enter something: Traceback (most recent call last):
File "mrprompt.py", line 9, in <module>
user_in = raw_input('Enter something: ')
EOFError: EOF when reading a line
When the output I'm looking for is the following:
~: python mrprompt.py
Enter something: something
something
~: echo foo | python mrprompt.py
Enter something: something
foo
something
I suppose, in other words, is it possible for a subshell to know the tty of its parent shell? Is it possible in Python to interact with the tty parent shell? I use bash inside the GNU screen (so reading the environment variable "SSH_TTY" is not a viable option).
source
share