簡體   English   中英

通過python2.7 subprocess.PIPE連接時如何獲取tcsh箭頭鍵

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

我正在用 python2.7 編寫一個簡單的遠程 shell 程序。 它像這樣運行 shell:

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

這讓我可以運行命令,但箭頭鍵不起作用:

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.
% 

我用bash嘗試了同樣的事情,並且箭頭鍵正在工作。 我應該如何修改上面的 python2.7 代碼以獲得tcsh中的工作箭頭鍵?

使用 TTY 而不是 stdin、stdout 和 stderr 的管道。 像這樣:

import pty
import subprocess

master, slave = pty.openpty()

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

然后稍后在您讀取和寫入管道的代碼中,將其更改為讀取和寫入master

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM