简体   繁体   中英

How to get tcsh arrow keys when connected through python2.7 subprocess.PIPE

I am writing a simple remote shell program in python2.7. It runs the shell like this:

p = subprocess.Popen(['/usr/bin/tcsh', '-i'],
                     stdin  = subprocess.PIPE,
                     stdout = subprocess.PIPE,
                     stderr = subprocess.PIPE)

This lets me run commands but the arrow keys aren't working:

Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.
% date
Sun Dec 25 10:54:47 PST 2022
% 
^[[A: Command not found.
% 

I tried the same thing with bash and the arrow keys are working. How should I modify the python2.7 code above to get working arrow keys in tcsh ?

Use a TTY instead of pipes for stdin, stdout, and stderr. Something like this:

import pty
import subprocess

master, slave = pty.openpty()

p = subprocess.Popen(['/usr/bin/tcsh', '-i'],
                     stdin  = slave,
                     stdout = slave,
                     stderr = slave)

And then later in your code where you're reading and writing to the pipes, change it to read and write to master instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM