Python - 运行一个带有提示 I/O 和“代理”的简单命令行程序它,在 Windows 上

发布于 2024-12-20 15:00:27 字数 965 浏览 0 评论 0原文

我有一个简单的命令行二进制程序 hello ,它输出到 STDOUT:

What is your name?

并等待用户输入。收到他们的输入后,它输出:

Hello, [name]!

并终止。

我想使用 Python 对该程序的最终输出运行计算(“Hello,[name]!”),但是在最终输出之前,我希望 Python 脚本本质上“是”二进制文件程序。换句话说,我希望 Python 将所有提示转发到 STDOUT,然后接受用户的输入并将其提供给程序。但是我想隐藏最终输出,以便我可以处理它并向用户显示我自己的结果。我不想在脚本中复制 hello 的行为,因为这个简单的程序是我实际使用的更复杂程序的替代品。

我希望在 subprocess 中有某种机制,我可以做一些类似的事情:

while process.is_running():
    next_char = process.stdout.read(1)
    if next_char == input_prompt_thing: # somehow check if the program is waiting for input
        user_input = raw_input(buffer)
        process.stdin.write(user_input)
    else:
        buffer += next_char

我一直在玩 subprocess 并且基本上已经实现了我可以使用 process.stdout.read(1) 在程序开始阻塞之前从程序中读取数据,但我不知道如何在进程阻塞我的 Python 脚本之前打破这个循环。我对控制台 I/O 不太熟悉,这对我来说不是一个专业领域,所以我开始感到非常迷失。我很感激任何帮助!

I have a simple command-line binary program hello which outputs to STDOUT:

What is your name?

and waits for the user to input it. After receiving their input it outputs:

Hello, [name]!

and terminates.

I want to use Python to run computations on the final output of this program ("Hello, [name]!"), however before the final output I want the Python script to essentially "be" the binary program. In other words I'd like Python to forward all of the prompts to STDOUT and then accept the user's input and give it to the program. However I want to hide the final output so I can process it and show my own results to the user. I do not want to replicate the hello's behavior in the script, as this simple program is a stand-in for a more complex program that I am actually working with.

I was hoping there would be some sort of mechanic in subprocess where I would be able to do something akin to:

while process.is_running():
    next_char = process.stdout.read(1)
    if next_char == input_prompt_thing: # somehow check if the program is waiting for input
        user_input = raw_input(buffer)
        process.stdin.write(user_input)
    else:
        buffer += next_char

I have been playing with subprocess and essentially got as far as realizing I could use process.stdout.read(1) to read from the program before it began blocking, but I can't figure out how to break this loop before the process blocks my Python script. I am not too familiar with console I/O and it is not an area of much expertise for me, so I am starting to feel pretty lost. I appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的黑色迷你裙 2024-12-27 15:00:27

您可以尝试 winpexpect (未经测试):

import re
from winpexpect import winspawn

p = winspawn('hello')
prompt = "What is your name?"
p.expect(re.escape(prompt))
name = raw_input(prompt)
p.sendline(name)
p.expect("Hello, .*!")
output = p.after
# ... 

You could try winpexpect (not tested):

import re
from winpexpect import winspawn

p = winspawn('hello')
prompt = "What is your name?"
p.expect(re.escape(prompt))
name = raw_input(prompt)
p.sendline(name)
p.expect("Hello, .*!")
output = p.after
# ... 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文