Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何输入输入、读取输出,然后根据接收到的输出将输出输入到python中的exe文件?_Python_Subprocess - Fatal编程技术网

如何输入输入、读取输出,然后根据接收到的输出将输出输入到python中的exe文件?

如何输入输入、读取输出,然后根据接收到的输出将输出输入到python中的exe文件?,python,subprocess,Python,Subprocess,我一直在尝试创建一个python脚本来打开一个exe文件, 输入一个输入,然后读取一个输出,然后根据收到的输出输入另一个输入 我一直在尝试使用Python的子进程库,但问题是communicate()方法只能使用一次。因此,除非在communicate()方法中同时输入两个输入,否则不可能输入两个输入,在这种情况下,该方法不起作用。因为第二个输入基于第一个输入后生成的输出,所以不能同时输入两个输入 此外,我还搜索了python的第三方库,但没有找到任何适用于windows的好库 有人能告诉我一种

我一直在尝试创建一个python脚本来打开一个
exe
文件, 输入一个输入,然后读取一个输出,然后根据收到的输出输入另一个输入

我一直在尝试使用Python的子进程库,但问题是
communicate()
方法只能使用一次。因此,除非在
communicate()
方法中同时输入两个输入,否则不可能输入两个输入,在这种情况下,该方法不起作用。因为第二个输入基于第一个输入后生成的输出,所以不能同时输入两个输入

此外,我还搜索了python的第三方库,但没有找到任何适用于windows的好库


有人能告诉我一种使用子进程库执行此操作的方法,或者给我推荐一个好的windows库吗?

考虑一个简单的应用程序,它有一些基本的执行控制流,只是与STDOUT相反地回显其STDIN-这可以是任何可执行文件,但为了简单起见,我们将坚持使用Python-比如,
app.py

#!/usr/bin/env python

import sys

sys.stdout.write("::BEGIN::\n")  # tell our listener that we're listening...
sys.stdout.flush()  # flush the STDOUT buffer
while True:  # a simple event loop
    line = sys.stdin.readline().rstrip()  # read a line from STDIN
    if line:  # ignore empty lines
        if line == "::END::":  # just a convenient way to shut down the app
            sys.stdout.write("::END::\n")  # tell our listener that we're done
            sys.stdout.flush()  # flush the STDOUT buffer
            break  # we're finished here
        sys.stdout.write(line[::-1])  # write the reversed line to STDOUT
        sys.stdout.write("\n")  # add a new line to the STDOUT
        sys.stdout.flush()  # flush the STDOUT buffer
然后,如果要打开此应用程序并通过Python脚本与之通信,则只需控制子进程STDOUT和STDIN,您可以无限期地执行此操作,例如:

import subprocess

# start our subprocess, forward its STDOUT and STDIN to the internal buffers
proc = subprocess.Popen(["python", "app.py"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)

# lets define our data to be sent one by one to our app.py, including ::END:: to exit...
items = ["test", "data", "to", "run", "sequentially", "::END::"]

# a convenience function to get the next item and write it to the passed buffer
def send_next_item(buf):
    item = items.pop(0)  # pop the first element from `items`
    print("REQ: {}".format(item))
    buf.write(item)  # write it to the passed buffer
    buf.write("\n")  # write a new line to the passed buffer
    buf.flush()  # flush the passed buffer

while True:  # wait for a prompt by our app
    line = proc.stdout.readline().rstrip()
    if line == "::BEGIN::":
        print("BEGIN!")
        send_next_item(proc.stdin)  # send the first item to the processes' STDIN
    elif line == "::END::":
        print("END!")
        break  # nothing more to do
    elif line:  # ignore empty lines
        print("RES: {}".format(line))
        send_next_item(proc.stdin)  # send the next item to the processes' STDIN
运行此命令时,您会得到如下输出:

BEGIN! REQ: test RES: tset REQ: data RES: atad REQ: to RES: ot REQ: run RES: nur REQ: sequentially RES: yllaitneuqes REQ: ::END:: END! 开始! 请求:测试 RES:tset 请求:数据 RES:atad 请求:至 RES:ot 请求:运行 RES:nur 请求:按顺序 RES:Yllatineuqes 请求:::结束:: 结束! 当然,您可以做进一步的处理来决定如何正确响应被调用应用程序的输入请求,这只是一个基本示例。

Popen.communicate()
只是一种方便-您可以直接与
.stdin
.stdout
属性交互(调用
.write()
.readline()
,以处理更复杂的情况。