如何通过process.Popen()调用getche()事件 - 不监控stdin
我在Windows上启动一个二进制程序,使用的是:
process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = ch.input)
ch.input comes from:
ch = InputStreamChunker('\n')
ch.daemon = True
ch.start()
这个方法很酷,因为它可以非阻塞地从标准输出读取数据。它是这个StackOverflow问题的最佳答案:
如何非阻塞地读取subprocess.Popen.stdout中的所有可用数据?这是我脚本的主要部分,试图启动和监控这个进程:
import subprocess
import time
from e import *
import msvcrt
ch = InputStreamChunker('\n')
ch.daemon = True
ch.start()
cmd = "C:\TEST.EXE"
process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = ch.input)
i = process.stdin
answers = []
waitforresults(ch, answers, expect = 5)
an = 1
for i in answers:
print "ANSWER # " + str(an) + ":" + i.read()
an= an+1
answers = []
time.sleep(5)
msvcrt.ungetch('s')
ch.flush()
waitforresults(ch, answers, expect = 1)
for a in answers:
print a.getvalue()
process.terminate()
ch.stop()
del process, ch
print "DONE"
无论如何,我可以用这种方法很好地从进程中读取数据。当我尝试用以下方式向进程写入数据时:
i = process.stdin i.write("s\n") 或者 i.write("s")
输出会在控制台窗口中打印出来,但二进制程序并没有捕获到。我查看了这个二进制程序的代码,发现它使用了一种非标准的方法来捕获用户输入。
这个进程是一个交互式命令行工具,它会等待用户输入S、R、P或Q(状态、恢复、暂停、退出),并且它在运行时捕获输入时不需要用户按回车。这是通过getche()实现的。我在IDA Pro中查看了这个二进制程序以确认:
v7 = getche();
if ( (unsigned int)dword_41F060 > 1 )
sub_4030A0(&unk_41C111, v5);
WaitForSingleObject((HANDLE)dword_41F040, 0xFFFFFFFFu);
if ( v7 == 113 )
{
if ( dword_41F060 != 1 )
{
if ( dword_41F060 )
dword_41F060 = 6;
}
}
else
{
if ( v7 <= 113 )
{
if ( v7 == 112 )
{
if ( dword_41F060 == 2 )
QueryPerformanceCounter((LARGE_INTEGER *)&qword_41F128);
dword_41F060 = 3;
sub_4030A0("Paused", v5);
}
}
else
{
if ( v7 == 114 )
{
if ( dword_41F060 == 3 )
{
QueryPerformanceFrequency((LARGE_INTEGER *)&v9);
QueryPerformanceCounter((LARGE_INTEGER *)&v8);
v3 = (double)(v8 - qword_41F128);
LODWORD(v4) = sub_413FF0(v9, v10, 1000, 0);
dbl_41F138 = v3 / (double)v4 + dbl_41F138;
}
dword_41F060 = 2;
sub_4030A0("Resumed", v5);
}
else
{
if ( v7 == 115 )
sub_40AFC0();
}
}
}
有没有人知道如何触发这个事件?这个链接看起来很有希望: http://docs.python.org/library/msvcrt.html,我尝试使用:
msvcrt.ungetch("s") “将字符char‘推回’到控制台缓冲区;它将是下一个被getch()或getche()读取的字符。”
这确实将字母"s"推送到了控制台,但没有触发我在getche()上的断点。手动按下字母S确实有效,并且会导致IDA Pro触发断点。
求助? :)
编辑:
我创建了两个小的Windows控制台应用程序,演示什么有效,什么无效,并确保我的Python环境正常。
第一个我无法通过写入stdin来识别输入,第二个可以。
#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char response;
printf("Enter \"s\":\n");
response = _getch();
printf("You entered %c\n", response);
return 0;
}
可以写入这个:
#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char response [2];
printf("Enter \"s\":\n");
gets(response);
printf("You entered %s", response);
return 0;
}
编辑 2 我还尝试了: import subprocess import time from e import * import msvcrt
ch = InputStreamChunker('\n')
ch.daemon = True
ch.start()
cmd = ["test-getch.exe"]
process = subprocess.Popen(cmd, stderr = subprocess.PIPE, stdin = subprocess.PIPE, stdout = ch.input)
i = process.stdin
answers = []
msvcrt.putch('s')
ch.flush
waitforresults(ch, answers, expect = 1)
for answer in answers:
print answer.getvalue()
i.close()
time.sleep(3)
#process.terminate()
ch.stop()
del process, ch
print "DONE"
1 个回答
以下是来自MSDN的摘录:
这些程序可以在你的控制台上进行读写操作。控制台的输入输出程序和流输入输出或低级输入输出库的程序不兼容。在Windows操作系统中,这些函数的输出总是直接发送到控制台,无法进行重定向。
所以我看到有两个可能的选择,但都不太靠谱:
- 创建一个伪Windows控制台,然后从这个控制台运行你的程序。这样你就可以在这个控制台上看到输出了。(类似于UNIX中的pty)
- 想办法从现有的Windows控制台获取数据。Console2 http://sourceforge.net/projects/console/ 就是做类似的事情。
我不知道如何做这些事情,或者它们是否真的可行。
总之,我觉得没有什么实际的解决办法,基本上就是运气不好。