pexpect等同于Expect的"send_user" [英] pexpect equivalent of Expect's "send_user"

查看:96
本文介绍了pexpect等同于Expect的"send_user"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的上一个问题的续篇期望,在交互输入过滤器中的print语句直到交互完成后才发送到stdout.

As a continuation from my previous question on building interactive option menus in pexpect, print statements within an interact input filter do not get sent to stdout until after the interact is complete.

文档似乎没有包含与预期的send_user等效的方法,是否有任何解决方法可将输出发送给用户,而不是从pexpect的interact方法中发送给生成的孩子?

The docs don't seem to contain an equivalent method to send_user from expect, is there any workaround to send output to the user, not to the spawned child from within pexpect's interact method?

bash-4.1$ cat testInputFilter.py
import pexpect

def input_filter(s):
    if s == b'\003':
        print('you pushed ctrl+c')
        return b'\r: r u going to kill me? press ctrl-d to exit!\r'
    elif s == b'\004':
        print('you pushed ctrl+d')
        return b'\r: ok, bye; exit\r'
    else:
        return s

proc = pexpect.spawn('bash --norc')
proc.interact(input_filter=input_filter)
proc.expect(pexpect.EOF)
bash-4.1$ ~/python/python36/bin/python3.6 testInputFilter.py | tee inputTest.txt
bash-4.1$
bash-4.1$ : r u going to kill me? press ctrl-d to exit!
bash-4.1$
bash-4.1$ : ok, bye; exit
exit
you pushed ctrl+c
you pushed ctrl+d
bash-4.1$

推荐答案

真的不知道为什么在与 spawn ed child,但是您可以显式 flush ,这样它可以实时显示:

Don't really know why print()'ed data is not automatically flushed when interacting with the spawned child but you can flush it explicitly so it would show up realtime:

print('something')
sys.stdout.flush()

或仅使用(仅适用于python3)

or just use (only for python3)

# .raw is not buffered
sys.stdout.buffer.raw.write('something')

这篇关于pexpect等同于Expect的"send_user"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆