I am trying to write and read from a CLI program using subprocess.Popen.
So every time you issue a command to the CLI it prints something, and that something is what I want to read. I will try to explain what I want do to with a concrete example so everybody can try.
Thus, instead of using my real program I will use ipython for which the problem holds:
p = subprocess.Popen(['ipython'],stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate(input="5+5")
If you use Popen.communicate(input=command) you get at stdout exactly all the text that ipython would print in the terminal if I would have executed it directly from the shell.
What I want to do:
Instead of using communicate. I want to be able to send individual commands and read the immediate output. For example I would like to do:
p.stdin.write("5+5")
out1 = p.stdout.read()
p.stdin.write("5*10")
out2 = p.stdout.read()
So out1 should be 5 and out2 should be 50.
Problem:
You can not read the stdout until you close stdin.
How do you do that ? I can't use read(n) or readline() since in my real application I do not know the length of the output.