我需要控制一个Windows程序,该程序通过调用
以下是代码:
<conio.h>
中的_kbhit
和_getch
从控制台直接读取输入。这里可以找到这样一个程序的示例:https://dev59.com/uHDXa4cB1Zd3GeqP8BK_#15603102
在Linux上,我可以使用pty.openpty()
创建新的伪终端并模拟按键。请参见此示例:https://code.google.com/p/lilykde/source/browse/trunk/lilykde/py/runpty.py
在Windows上,我尝试写入CONIN$
/CONOUT$
,但我所看到的只是我的数据出现在控制台上,而子进程忽略它。以下是代码:
#!/usr/bin/env python
import subprocess
import time
TEST_EXECUTABLE = 'C:\\dev\\test.exe'
TEST_INPUT = 'C:\\dev\\input.txt'
def main():
with open(TEST_INPUT, mode='r') as test_input, open('CONOUT$', mode='wb') as conout:
test_exec = subprocess.Popen([TEST_EXECUTABLE],
bufsize=0,
stdin=None,
stdout=None,
stderr=None)
for cmd in test_input:
cmd = cmd.strip('\r\n')
conout.write(cmd)
conout.flush()
time.sleep(1)
ret = test_exec.wait()
print '%s (%d): %d' % (TEST_EXECUTABLE, test_exec.pid, ret)
pass
if __name__ == "__main__":
main()
这可能吗?我如何模拟与子进程的用户交互?
谢谢。 Alex