Created
March 29, 2016 11:54
-
-
Save danieljfarrell/a6d06ea07b97c0a5ea73 to your computer and use it in GitHub Desktop.
Reading/writing stdout/stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, psutil, subprocess | |
# Usage: | |
# This script MUST be run from the root of the | |
# teraview-test-robot directory for example using, | |
# python run_test_robot_cmd.py | |
def make_test_robot_process(): | |
"""Creates the test robot process.""" | |
REL_TEST_ROBOT_EXE = "bin\\Debug\\test-robot.exe" | |
REL_CONFIG = "config\\test2" | |
setup_args = [REL_TEST_ROBOT_EXE, REL_CONFIG] | |
#print setup_args | |
p = subprocess.Popen(setup_args, | |
stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
# this works but closes the pipe! | |
#print p.communicate('show') | |
return p | |
p = make_test_robot_process() | |
EOL = "\r\n" | |
#print "{}Test Robot is ready for commands e.g. type 'help' or 'show' to start.{}".format(EOL, EOL) | |
# Start command loop | |
while True: | |
# Wait for user input | |
s = raw_input("> ") | |
# Append end-of-line | |
cmd_str = "{}{}".format(s.strip(), EOL) | |
# Do we need to flush before sending? | |
p.stdin.flush() | |
p.stdout.flush() | |
# Write command to stdin | |
p.stdin.write(cmd_str) | |
import time; time.sleep(0.3) | |
print "Send command '{}' now reading lines... this will probably hang...".format(cmd_str.strip()) | |
# Print debug values because this is not working... | |
print p | |
print p.stdout | |
# This line hangs! Grrr | |
response = p.stdout.readline() | |
# Do something with the response | |
if response != '': | |
print "{}".format(response) | |
else: | |
print "Breaking out" | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment