简体   繁体   English

使用Python与正在运行的控制台应用程序进行交互

[英]Interact with running console application with Python

On my system, I have an already-running console application that receives commands and emits output accordingly. 在我的系统上,我有一个已经在运行的控制台应用程序,该应用程序接收命令并相应地发出输出。 I would like to connect to this application from a python script, and interact with it. 我想从python脚本连接到此应用程序,并与其进行交互。 Is it possible? 可能吗?

I know I cannot use subprocess or similar because they just launch another process; 我知道我不能使用子流程或类似的流程,因为它们只是启动另一个流程。 I need to talk to an already existing one. 我需要和一个已经存在的人交谈。 How can I do it, in detail? 我该怎么做呢? Do I need to set up pipes, or sockets? 我是否需要设置管道或插座? What's the best way to do it? 最好的方法是什么?

If I understand your question correctly, you might want a tool like reptyr that can re-assign the pty of the process in which you're interested. 如果我正确理解了您的问题,则可能需要像reptyr这样的工具,可以重新分配您感兴趣的过程的部分。

You might also investigate the answer to this question that is similar. 您可能还会调查类似问题的答案。

Once you have the pty, then you can either use pexpect to handle the interaction. 一旦拥有了pty,则可以使用pexpect处理交互。

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

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