python - 检测 tty 输出结束

标签 python tty control-characters

嗨,我正在编写一个伪终端,它可以驻留在一个 tty 中并生成第二个 tty,它过滤来自的输入和输出

我现在用 python 编写它,生成第二个 tty 并且读写很容易

但是当我读取时,读取并没有结束,它等待更多的输入。

import subprocess

pfd = subprocess.Popen(['/bin/sh'], shell=True, 
    stdout=subprocess.PIPE, stdin=subprocess.PIPE)

cmd = "ls" 

pfd.stdin.write(cmd + '\n')

out = ''
while 1: 
    c = pfd.stdout.read(1)
    if not c: # if end of output (this never happends)
        break 
    if c == '\n': # print line when found
        print repr(out)
        out = ''
    else:
        out += c

---------------------------- 输出 ----------------- -------

intty $ python intty.py 
'intty.py'
'testA_blank'
'testB_blank'
(hangs here does not return)

看起来它已经到达了缓冲区的末尾,并且没有返回 None 或 '',而是挂起等待更多输入。

我应该寻找什么来查看输出是否已完成?缓冲区的末尾?不可打印的字符?

---------------- 编辑-------------

当我运行 xpcshell 而不是 ls 时也会发生这种情况,我假设这些交互式程序有某种方式知道再次显示提示, 奇怪的是提示,在这种情况下“js>”永远不会出现

最佳答案

嗯,你的输出实际上还没有完成。因为您生成了 /bin/sh,所以 shell 在“ls”完成后仍在运行。没有 EOF 指示器,因为它仍在运行。

为什么不简单地运行/bin/ls

你可以做类似的事情

pfd = subprocess.Popen(['ls'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

out, err_output = pfd.communicate()

这也突出了subprocess.communicate,这是从单个程序运行中获取输出(无论如何,对于适合内存的输出)的更安全的方法。仅当程序完成运行时才会返回。

或者,您可以从 shell 中逐行读取,但您需要寻找特殊的 shell 序列,例如可以轻松显示在程序输出中的 sh~# 行。因此,运行 shell 可能不是一个好主意。

<小时/>

编辑这是我所指的,但它仍然不是真正的最佳解决方案,因为它有很多警告:

while 1: 
    c = pfd.stdout.read(1)
    if not c:
        break
    elif c == '\n': # print line when found
        print repr(out)
        out = ''
    else:
        out += c
        if out.strip() == 'sh#':
            break

请注意,如果任何其他命令在行开头输出“sh#”,并且如果由于某种原因输出与预期不同,您将进入与以前相同的阻塞情况。这就是为什么对于 shell 来说这是一个非常次优的情况。

关于python - 检测 tty 输出结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2278150/

相关文章:

c - 我将如何着手编写 Linux TTY 嗅探器?

python - 我无法 sys.stdout.seek

printing - 标准ML打印控制字符

linux - 在 Linux 中以编程方式断开并重新连接 ttyUSB0

javascript - ASCII 控制字符 html 输入文本

python - Python 模块 INSTABOT 登录问题

python - 无法使用 Python 的 mock.patch 模拟 urllib2.urlopen

python - 如何在 OS X 10.10.3 中卸载和/或管理多个版本的 python

python - 从 Linux 系统中删除所有非必要的 Python 包