0

I'm tinkering with the VT100 emulation in the various linux terminal (e.g. xterm, gnome-terminal,etc) and need help with getting a python script to receive the non-printable escape sequences from the emulator. I think I got a handle on the VT100 emulation part; this is a question about python.

I want to run the following VT100 "Identify terminal type" query within python:

print "\x1b[c"
resp = raw_input()
print ":".join(x.encode('hex') for x in resp)   # debugging purpose only

I works almost nicely with the terminal's VT100 emulation EXCEPT that resp = raw_input() requires me to manually complete the response by pressing the ENTER key.

How do I get python to take the input from the VT100 emulation without manual intervention on my part?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
codechimp
  • 1,509
  • 1
  • 14
  • 21
  • a funny way would be to run another thread which'd press enter after certain time delay. EDIT: it seems to be the answer [look here](https://stackoverflow.com/questions/53167495/taking-in-multiple-inputs-for-a-fixed-time-in-python/53180738#53180738) – Atreyagaurav Jan 01 '20 at 13:31
  • The module https://pypi.org/project/keyboard/ should be helpful. – Philippe Jan 01 '20 at 13:42

2 Answers2

1

Would this work?

import subprocess

proc = subprocess.Popen(["echo \x1b[c"], stdout=subprocess.PIPE, shell=True)
resp, err = proc.communicate()

print (":".join(hex(x) for x in resp)) # debugging purpose only

Its in python3, but I believe you can do roughly the same in 2

Edit:
this is nicer:

import subprocess

resp = subprocess.check_output("echo \x1b[c", shell=True)

print (":".join(hex(x) for x in resp)) # debugging purpose only
Ron Serruya
  • 3,988
  • 1
  • 16
  • 26
  • Sorry neither works properly. A response is generated and captured without pressing `ENTER` (thanks for that, will study it more) but the response is not correct. It seems to just regurgitates the query command (i.e. '\x1b[c\n"), not the VT100 response of my test case (i.e "\x1b[?62;c"). Using `echo -e` or `printf` instead doesn't fix it either. – codechimp Jan 01 '20 at 22:49
  • How about `python -c "print('\x1b[c')"` ? – Ron Serruya Jan 01 '20 at 22:55
1

On a Unix (-like) system, the problem is not so much that Python does not take the input, but that the TTY subsystem does not even provide it until you hit return. This enables things like backspace deleting characters and ctrl-d being converted into an end-of-file marker.

You can change this by using setcbreak or setraw from the tty module.

See also the Wikipedia entry on terminal modes, and Linus Åkesson's The TTY Demystified.

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15
  • See also https://unix.stackexchange.com/questions/21752/what-s-the-difference-between-a-raw-and-a-cooked-device-driver and https://www.tutorialspoint.com/terminal-control-functions-in-python. I guess raw mode is problematic for vitality import Ctrl-C also. – codechimp Jan 08 '20 at 18:37