进程间通信Python

2024-09-27 19:30:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python编写一个脚本,它应该与软件进行通信 "控制台应用程序.exe“(用C写的);最后一个一旦启动就等待一个固定的 从他的“stdin”发出lenght命令(5个字节),并在 他的“stdout”是我应该在Python脚本上读到的输出。在

#
//This is the "ConsoleApplication.c" file
#include <stdio.h>
#include <function.h>

char* command[5];
int main()
{
 while(1)
 {

 scanf("%s\n", &command);
 output = function(command);
 print("%s\n", output);

 }
}
# ^{pr2}$ #

有什么建议吗?我怎样才能修好它?在

我试着用

stdout = p.communicate(input='test\n')[0] 

但我在运行时收到以下错误: “TypeError:'str'不支持缓冲区接口” 我也试过这个

from subprocess import Popen, PIPE, STDOUT


p = Popen(['ConsoleApplication.exe'], stdout=PIPE, stdin=PIPE, stderr=PIPE)

out, err = p.communicate(input='00056\n'.encode())
print(out)
out, err = p.communicate(input='00043\n'.encode())
print(out)

但我得到一个错误: “值错误:开始通信后无法发送输入”


Tags: 脚本inputoutputinclude错误stdinstdoutfunction

热门问题