与Python中的另一个命令行程序进行交互 [英] Interacting with another command line program in Python
问题描述
我需要编写一个Python脚本,该脚本可以运行另一个命令行程序并与其stdin和stdout流进行交互.本质上,Python脚本将从目标命令行程序读取,通过写入其stdin进行智能响应,然后再次从该程序读取结果.(它将重复执行此操作.)
我已经浏览了子流程模块,但似乎无法让它完成我想要的读/写/读/写操作.还有其他我应该尝试的东西吗?
要执行这种详细的交互(当在您的控制范围之外时,另一个程序可能会缓冲其输出,除非它认为自己正在与终端进行对话)需要类似以下内容的东西 pexpect -反过来,它需要Python标准库模块 pty
(在允许的操作系统上,例如Linux和Mac OS x)实现伪终端".
在Windows上生活更加艰苦,但也许此zip文件可以提供帮助-它应该是Windows的 pexpect
端口(对不起,我没有Windows机器可以对其进行检查).有问题的项目称为 wexpect
,位于此处.>
I need to write a Python script that can run another command line program and interact with it's stdin and stdout streams. Essentially, the Python script will read from the target command line program, intelligently respond by writing to its stdin, and then read the results from the program again. (It would do this repeatedly.)
I've looked through the subprocess module, and I can't seem to get it to do this read/write/read/write thing that I'm looking for. Is there something else I should be trying?
To perform such detailed interaction (when, outside of your control, the other program may be buffering its output unless it thinks it's talking to a terminal) needs something like pexpect -- which in turns requires pty
, a Python standard library module that (on operating systems that allow it, such as Linux and Mac OS x) implements "pseudo-terminals".
Life is harder on Windows, but maybe this zipfile can help -- it's supposed to be a port of pexpect
to Windows (sorry, I have no Windows machine to check it on). The project in question, called wexpect
, lives here.
这篇关于与Python中的另一个命令行程序进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!