简体   繁体   English

检测tty输出的结束

[英]detecting end of tty output

Hi I'm writing a psudo-terminal that can live in a tty and spawn a second tty which is filters input and output from 嗨,我正在写一个伪终端,它可以存在于tty中并生成第二个tty,该tty是过滤器的输入和输出

I'm writing it in python for now, spawning the second tty and reading and writing is easy 我现在用python编写它,产生第二个tty,读和写都很容易

but when I read, the read does not end, it waits for more input. 但是当我阅读时,阅读并没有结束,它等待更多的输入。

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

----------------------------- outputs ------------------------ -----------------------------输出-------------------- ----

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

it looks like it's reaching the end of hte buffer and instead of returning None or '' it hangs waiting for more input. 看起来好像到达了hte缓冲区的末尾,而不是返回None或”它挂起,等待更多输入。

what should I be looking for to see if the output has completed? 我应该寻找什么以查看输出是否完成? the end of the buffer? 缓冲区的尽头? a non-printable character? 不可打印的字符?

---------------- edit ------------- ----------------编辑-------------

this happends also when I run xpcshell instead of ls, I'm assuming these interactive programs have some way of knowing to display the prompt again, strangly the prompt, in this case "js>" never apears 当我运行xpcshell而不是ls时,也会发生这种情况,我假设这些交互式程序具有某种知道再次显示提示的方式,这很奇怪,在这种情况下,“ js>”永远不会消失

Well, your output actually hasn't completed. 好吧,您的输出实际上还没有完成。 Because you spawned /bin/sh , the shell is still running after "ls" completes. 因为您生成了/bin/sh ,所以“ ls”完成后,shell仍在运行。 There is no EOF indicator, because it's still running. 没有EOF指示器,因为它仍在运行。

Why not simply run /bin/ls ? 为什么不简单地运行/bin/ls

You could do something like 你可以做类似的事情

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

out, err_output = pfd.communicate()

This also highlights subprocess.communicate , which is a safer way to get output (For outputs which fit in memory, anyway) from a single program run. 这也突出显示了subprocess.communicate ,这是从单个程序运行中获取输出(无论如何适合内存的输出)的更安全方法。 This will return only when the program has finished running. 仅在程序完成运行后才返回。

Alternately, you -could- read linewise from the shell, but you'd be looking for a special shell sequence like the sh~# line which could easily show up in program output. 或者,您可以从shell逐行读取,但是您会在寻找特殊的shell序列,例如sh〜 sh~#行,可以很容易地在程序输出中显示出来。 Thus, running a shell is probably a bad idea all around. 因此,运行shell可能是个坏主意。


Edit Here is what I was referring to, but it's still not really the best solution, as it has a LOT of caveats: 编辑这是我所指的,但它仍然不是真正的最佳解决方案,因为它有很多警告:

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

Note that this will break out if any other command outputs 'sh#' at the beginning of the line, and also if for some reason the output is different from expected, you will enter the same blocking situation as before. 请注意,如果任何其他命令在行的开头输出“ sh#”,则会中断此操作,并且如果由于某些原因输出的结果与预期的不同,您将输入与以前相同的阻塞情况。 This is why it's a very sub-optimal situation for a shell. 这就是为什么对于shell来说,它是次优的情况。

For applications like a shell, the output will not end until the shell ends. 对于外壳之类的应用程序,直到外壳结束,输出才会结束。 Either use select.select() to check if it has more output waiting for you, or end the process. 使用select.select()检查它是否有更多输出在等待您,或者结束该过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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