gpt4 book ai didi

python - 使用 Python 控制终端应用程序

转载 作者:太空狗 更新时间:2023-10-30 02:34:55 27 4
gpt4 key购买 nike

我有一个通过控制台运行的服务器程序。 (特别是 Bukkit MineCraft 服务器)我希望能够控制这个程序并读取输出。没有 GUI,所以应该不会太难,对吧?

无论如何,我从来没有用 python 控制过控制台,所以我完全被卡住了。有什么建议吗?

附言我使用的是 Debian Linux,所以应该可以简化一些事情。

我得到了一个很好的答案,但我还需要一件事。我想有一些方法可以将程序的完整输出打印到 python 控制台(逐行很好),我需要一些方法将控制台中的命令转发给程序。

最佳答案

像这样的任务的规范答案是使用 Pexpect .

Pexpect is a Python module for spawning child applications and controlling them automatically. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers. It can be used for automated software testing. Pexpect is in the spirit of Don Libes' Expect, but Pexpect is pure Python. Other Expect-like modules for Python require TCL and Expect or require C extensions to be compiled. Pexpect does not use C, Expect, or TCL extensions. It should work on any platform that supports the standard Python pty module. The Pexpect interface focuses on ease of use so that simple tasks are easy.

关于python - 使用 Python 控制终端应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5710280/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com