本文介绍了如何实现一个标准输入,标准输出包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行stdin和stdout一个交互式程序。
我需要创建包装到它的标准输入,将发送X,检查其打印Y和再
包装的标准输入和stdout重定向到程序的标准输入和stdout就像方案将直接执行。

如何实现这一点? X和Y可以是硬codeD。击? Python的?

编辑:我无法运行该程序的两倍。它必须是一个实例。
这里是伪code:

 高清包(CMD,在,expected_out):
  P = EXEC(CMD)
  p.writeToStdin(在)
  OUT = p.readBytes(expected_out.size())
  如果(满分= expected_out!)返回失败;
  #如果上述4条线路将缺席或(在==进出==)
  #那么这个包装是完全一样的cmd直接执行
  connectpipe(p.stdout,标准输出)
  connectpipe(标准输入,p.stdin)
  p.continueExecution()


解决方案

是用于制作其他自动化程序的运行 - 本质上,你写的东西一样,以纯文本格式,

启动此程序。当它打印出单词用户名,发送我的用户名。当它发出的密码,发送我的密码。

这真是太棒了驾驶其他程序。

I have an interactive program that runs stdin and stdout.I need to create wrapper that will send X to it's stdin, check that it prints Y and thenredirects wrapper's stdin and stdout to program's stdin and stdout just like program would be executed directly.

How to implement this ? X and Y can be hardcoded. Bash? Python?

Edit: I can't run the program twice. It has to be one instance.Here is the pseudocode:

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()
解决方案

Expect is made for automating the running of other programs - essentially you write something like, in plain text,

Start this program. When it prints out the word "username", send it my username. When it sends "password", send it my password.

It's really great for driving other programs.

这篇关于如何实现一个标准输入,标准输出包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:42