Created
September 13, 2012 13:09
-
-
Save lethee/3714179 to your computer and use it in GitHub Desktop.
Test server/client apps Using Python's subprocess
This file contains 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 time | |
import sys | |
for i in range(5): | |
time.sleep(1) | |
sys.stdout.write("%d\n" % i) | |
sys.stdout.flush() |
This file contains 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 subprocess | |
import time | |
import threading | |
import signal | |
import os | |
############# | |
server_cmd = "python server.py" | |
server_ready_pattern = "2" | |
client_cmd = "ls -al" | |
############# | |
def start_client(server_proc): | |
client = subprocess.call(client_cmd, shell=True) | |
print client | |
print "![test ] server" | |
os.kill(server_proc.pid, signal.SIGTERM) | |
def start_server(): | |
proc = subprocess.Popen(server_cmd,shell=True,stdout=subprocess.PIPE) | |
while True: | |
time.sleep(0.1) | |
line = proc.stdout.readline() | |
if not line: break | |
print "![server]", line.strip() | |
if (line.strip() == server_ready_pattern): | |
threading.Thread(target=start_client, args=(proc,)).start() | |
proc.wait() | |
print "![test ] server killed" | |
if __name__ == '__main__': | |
start_server() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
결과: