0
我正在使用Python 2.7.9版本,当我尝试从Popen进程读取一行时,它一直卡住,直到进程结束。在结束之前我如何从标准输入读取数据?Popen.communicate stdin.write卡住
如果输入是'8200'(正确的密码),那么它会打印输出。 但是,如果密码从'8200'更改,所以没有输出,为什么?
子源代码:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char password[10];
int num;
do
{
printf("Enter the password:");
scanf("%s", &password);
num = atoi(password);
if (num == 8200)
printf("Yes!\n");
else
printf("Nope!\n");
} while (num != 8200);
return 0;
}
Python源:
from subprocess import Popen, PIPE
proc = Popen("Project2", shell=True, stdin=PIPE,stdout=PIPE,stderr=PIPE)
#stdout_data = proc.communicate(input='8200\r\n')[0]
proc.stdin.write('123\r\n')
print proc.stdout.readline()
仍然不起作用 – null
请尝试添加fflush(stdout);之后printf –
是的,我们正在朝..但现在的输出是: '输入密码:' 我希望它打印:“是!”或“不!”! (我添加了fflush(stdout);仅在第一个printf后) – null