I am used to kick off command by subprocess.Popen in my python scripts. Most of them like:
try:
while True:
line = proc.stdout.readline()
if(proc.Poll() is not None):break;
parse(line)
except(KeyboardInterrupt) as E:
kill(proc)
Now I have a case where a command(a simulator tool) receives "CTRL-C" into the debug mode(command line interface) to accept debug instructions, quit when exist instructions and resume to run. it seems that Popen doesn't support to pass the standard input to argument stdin
like stdin=input
. I know I can use os.system(cmd) which using the standard input/output as its input/output, but that is not my solution.
so someone can show me another solution? thanks in advance.
TRY: As Ahmed AEK said, I have a try:
proc = Popen(shlex.split(cmd), stdin=sys.stdin, stdout=sys.stdout, stderr=STDOUT)
try:
proc.wait() #how do I get output from stdout
except KeyboardInterrupt:
proc.send_signal(signal.SIGINT)
proc.wait()
Now, "CTRL-C"(SIGINT) can force the command into the debug mode. its function likes os.system(cmd)
. But, how to capture the output from stdout(sys.stdout) to search some keywords that is reason why I don't use the os.system(...)
, but subprocess.Popen(...)
(using proc.stdout.readline()
when stdout=PIPE
).