I'm looking for a way to create a Python program that wraps another Python program. This is what I mean:
while (otherProgram is waiting for raw_input):
send some text to the other program
It is essential that I can do this in two separate programs.
Here is another example:
text = raw_input()
print("You entered " + text)
text2 = raw_input()
print("Your second input was " + text2)
text3 = raw_input()
print("Your third input was " + text3)
# use program1.py somehow
send_to_program1("prefix " + raw_input() + " suffix")
sample input into program2:
sample output out of program2:
You entered prefix asdf suffix
Your second input was prefix ghjkl suffix
Your third input was prefix 1234 suffix
Things I have considered using:
execor anything like that would work, since I don't know how much code to execute.
What I hope to end up with
I eventually want to end up with a Python program which, each time it is run, inserts another successive input into the wrapped program at the point it left off, and pipes the output into stdout. If it is easier to do this, that would be great.
Here is an example of running the child process using subprocess.
import subprocess program1 = subprocess.Popen("program1.py", stdin=subprocess.PIPE, stdout=subprocess.PIPE) while program1.poll() == None: cmd_to_send = "prefix " + raw_input() + " suffix" program1.stdin.write(cmd_to_send + "\n")
Your child process will wait for input because
raw_input is a blocking call until it receives a line of input.
take a look at subprocess module or https://pypi.python.org/pypi/pexpect-u/