如何实现stdin,stdout包装器?

Łuk*_*Lew 5 python bash stdin stdout wrapper

我有一个运行stdin和stdout的交互式程序.我需要创建将X发送到它的stdin的包装器,检查它是否打印Y然后将包装器的stdin和stdout重定向到程序的stdin和stdout,就像程序将直接执行一样.

怎么实现这个?X和Y可以硬编码.击?蟒蛇?

编辑:我无法运行程序两次.它必须是一个例子.这是伪代码:

def wrap(cmd, in, expected_out):
  p = exec(cmd)
  p.writeToStdin(in)
  out = p.readBytes (expected_out.size())
  if (out != expected_out) return fail;
  # if the above 4 lines would be absent or (in == "" and out == "")
  # then this wrapper would be exactly like direct execution of cmd
  connectpipe (p.stdout, stdout)
  connectpipe (stdin, p.stdin)
  p.continueExecution() 
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 3

Expect是为了自动运行其他程序而设计的 - 本质上你可以用纯文本编写类似的内容,

启动这个程序。当它打印出“用户名”一词时,将我的用户名发送给它。当它发送“密码”时,发送我的密码。

这对于驱动其他程序来说真的很棒。