I need to control a Windows program, which reads input directly from console by calling _kbhit
and _getch
from <conio.h>
. An example of such program can be found here: https://stackoverflow.com/a/15603102/365492
On Linux I can use pty.openpty()
to create new pseudo-terminal and to emulate key presses. See this example: https://code.google.com/p/lilykde/source/browse/trunk/lilykde/py/runpty.py
On Windows I tried to write to CONIN$
/CONOUT$
but all I can see is that my data is appearing on the console, while child process ignores it.
Here is the code:
#!/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()
Is it possible at all? How can I emulate user interaction with the child process?
Thanks. Alex