I have this python script (with ncurses
):
#! /usr/bin/python3
import sys,os
import curses
def draw_menu(stdscr):
k = 0
while (k != ord('q')):
stdscr.clear()
height, width = stdscr.getmaxyx()
stdscr.addstr(0, 0, "Last key is {}".format(k))
stdscr.refresh()
k = stdscr.getch()
def main():
curses.wrapper(draw_menu)
if __name__ == "__main__":
main()
And these are my last tries (with bad results) to catch stdout and send keypress:
This is with Popen
.
from subprocess import Popen, PIPE
#p = Popen('./test5.py', stdin=PIPE, stdout=PIPE, shell=True)
#p = Popen('./test5.py', shell=True)
p = Popen('./test2.py')
print(p.pid)
sleep(100)
p.stdin.write('a')
# p.stdin.close()
# p.stdout.close()
# p.wait()
And this is other with pexpect
:
import sys
import pexpect
child = pexpect.spawn('./test5.py', logfile=open("/tmp/file", "wb"))
child.logfile = open("/tmp/file", "wb")
child.expect(pexpect.EOF)
child.send('a')
child.send('q')
child.interact()
I tried with xdotools
but I could not catch the stdout.
Is there any form to cheat/hoax a executable for it "belive" that it is running normally?
I found the solution is "non blocking read stdout". There are several solution in https://chase-seibert.github.io/blog/2012/11/16/python-subprocess-asynchronous-read-stdout.html and https://gist.github.com/sebclaeys/1232088.
And my solution with the code of question: