如何处理需要交互式响应的可执行文件? [英] How to handle an executable requiring interactive responses?

查看:42
本文介绍了如何处理需要交互式响应的可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 tsfoil2.exe 的可执行文件,我想从我的 python 环境中操作这个 .exe.我在 Windows 7 上运行 Python 2.7.3,Spyder 2.1.11.

I have a executable, called tsfoil2.exe, and I want to operate this .exe from my python environment. I'm running Python 2.7.3, with Spyder 2.1.11 on Windows 7.

为了运行 .exe,它需要一些输入、默认硬盘驱动器 ('I:\')、输出文件的名称 ('test') 和输入文件的名称 ('SC20610.inp').

In order to operate the .exe, it requires some input, the default hard drive ('I:\'), a name for the outputfile ('test'), and a name for the input file ('SC20610.inp').

我的一位同事建议我使用 os.system,并为其提供一个包含所有参数的临时输入文件.

One of my colleagues advised me to use os.system, and supply this with a temporary input file, that contains all the arguments.

f = open('temp', 'w') 
f.write('I:\ \n') 
f.write('test \n')
f.write('SC20610.inp\n')  
f.close() 

然后我通过以下方式向这个文件提供 .exe 的参数:

I then supply this file with arguments to the .exe in the following way:

os.system("tsfoil2.exe < temp")

这一切都有效,但程序需要ENTER"才能关闭.出于某种原因,.exe 反复要求按 ENTER 键退出".甚至,当我在 Spyder 控制台中按下 Enter 键时,程序也不会终止.

This all works, but the program requires a 'ENTER' to close. For some reason, the .exe is repeatedly asking to 'Press the ENTER key to exit'. Even, when I press the enter key in my Spyder-console, the program does not terminate.

有没有办法将ENTER"键作为 .exe 的交互式输入?我曾尝试使用 SendKeys 类,但由于程序不会终止,因此无法到达包含 SendKeys 命令的代码行.我也尝试将它包含在参数文件中,但这也不起作用.

Is there a way to give the 'ENTER' key as an interactive input to .exe? I've tried to use the SendKeys class, but as the program does not terminate, it does not reach the line of code that contains the SendKeys command. I've also tried to include it in the arguments-file, but this does not work either.

此外,我还发现切换到 subprocesses 命令可能是有益的,因为它可能会给我更多的执行命令,但我无法使用输入文件运行可执行文件.

Furthermore I've also found out that it might be beneficial to switch to subprocesses command, as it might give me more command over the execution, but I haven't been able to run the executable with the input files.

是否可以使用 os.system 提供必要的 'ENTER',或者我应该切换到子进程,如果可以,我如何构建类似于 os.system("tsfoil2.exe < temp") 我现在正在使用.

我已经试过了:

import subprocess as sub
f = open('temp', 'w') 
f.write('I:\ \n') 
f.write('test \n')
f.write('SC20610.inp\n')  
f.close() 
proc=sub.Popen(["tsfoil2.exe","temp"], shell=True)

还有这个

import subprocess as sub
p=sub.Popen('tsfoil2.exe')
p.communicate(input='I:' )

但是,程序不响应给定的参数.

But, the program does not respond to the arguments given.

MWE:

import os
f = open('temp', 'w') 
f.write('I:\ \n') 
f.write('test \n')
f.write('SC20610.inp\n')  
f.close() 

os.system("tsfoil2.exe < temp")

这两个程序都可以通过 http://www 找到.dept.aoe.vt.edu/~mason/Mason_f/tsfoil2.exe,输入文件可以通过http://www.dept.aoe.vt.edu/~mason/Mason_f/SC20610.inp.

Both the program can be found via http://www.dept.aoe.vt.edu/~mason/Mason_f/tsfoil2.exe, the input file can be found via http://www.dept.aoe.vt.edu/~mason/Mason_f/SC20610.inp.

我希望一切都清楚,你可以帮助我.

I hope everything is clear, and you can help me out.

推荐答案

'Press the ENTER key to exit' 表示程序需要换行符.

'Press the ENTER key to exit' means that the programs expects a newline.

我在 temp 文件的末尾没有看到空行.此外,您可能指的是 'I:\\\n' -- 如果您想要 \,则需要在 Python 字符串文字中使用 '\\' 在输出中.

I see no blank line at the end of the temp file. Also, you might have meant 'I:\\\n' -- you need to use '\\' in a Python string literal if you want \ in the output.

问题是 tsfoil2.exe 认为换行符是什么,例如 b'\r\n' 或只是 b'\n'以及它希望从哪里接收它:从标准输入(getchar())或直接从控制台(getch()).

The question is what tsfoil2.exe considers a newline e.g., b'\r\n' or just b'\n' and where it expects to receive it: from stdin (getchar()) or directly from console (getch()).

假设程序期望 b'\r\n' 来自 Windows 上的标准输入:

Assuming that the program expects b'\r\n' from stdin on Windows:

import os
from subprocess import CalledProcessError, Popen, PIPE

cmd = "tsfoil2.exe"
input_data = os.linesep.join(['I:\\', 'test', 'SC20610.inp', os.linesep])
p = Popen(cmd, stdin=PIPE, bufsize=0)
p.communicate(input_data.encode('ascii'))
if p.returncode != 0:
   raise CalledProcessError(p.returncode, cmd)

工作原理

os.linesep == "\r\n" 在 Windows 上."\n".join(["a", "b"]) == "a\nb".

How it works

os.linesep == "\r\n" on Windows. "\n".join(["a", "b"]) == "a\nb".

每个进程可能有三个标准流:stdin、stdout、stderr.在 Python 中,它们表示为 sys.stdinsys.stdoutsys.stderr.您可以从 stdin 读取输入并写入 stdout,stderr 例如,input 函数默认从 stdin 读取,print 写入 stdout.stderr 可用于写入错误消息.

Each process may have three standard streams: stdin, stdout, stderr. In Python, they are represented as sys.stdin, sys.stdout, sys.stderr. You can read input from stdin and write to stdout, stderr e.g., input function reads from stdin and print writes to stdout by default. stderr may be used to write error messages.

stdin=PIPE 告诉 Popen 在调用它的父进程和新子进程(tsfoil2.exe")之间创建一个管道并重定向子进程'标准输入.p.communicate(data) 将数据写入 p.stdin,关闭它并等待子进程完成.p.returncode 包含子进程的退出状态.通常非零状态表示失败.

stdin=PIPE tells Popen to create a pipe between the parent process where it is called and the new child process ("tsfoil2.exe") and redirect the subprocess' stdin. p.communicate(data) writes data to p.stdin, closes it and waits for the child process to finish. p.returncode contains the exit status of the child process. Usually non-zero status means failure.

它模拟 shell 管道而不实际生成 shell:

It emulates the shell pipeline without actually spawning the shell:

$ echo input data | tsfoil2.exe

<小时>

如果需要直接从控制台输入,您可以尝试 SendKeys 模块 或其纯 Python 实现 SendKeys-ctypes:

from SendKeys import SendKeys

SendKeys(r"""
    {LWIN}
    {PAUSE .25}
    r
    C:\Path\To\tsfoil2.exe{ENTER}
    {PAUSE 1}
    I:\{ENTER}
    {PAUSE 1}
    test{ENTER}
    {PAUSE 1}
    SC20610.inp{ENTER}
    {PAUSE 1}
    {ENTER}
""")

我还没有测试过.

这篇关于如何处理需要交互式响应的可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆