subprocess - Interact with executables over server in Python? -
i want run executable on tcp server , take input socket connections interactively , send output client until executable terminated. trying piping through popen class of subprocess not helping interaction executable ( take input 1 time want input taken time until program exits ).
suppose send "1" input server server must send stdout corresponding "1" input client , ask next input , till executable exits in continuation .
just provide socket standard input, output, , error of subprocess. e.g.:
import socket import subprocess listener = socket.socket(socket.af_inet, socket.sock_stream) listener.setsockopt(socket.sol_socket, socket.so_reuseaddr, true) listener.bind(('0.0.0.0', 0)) listener.listen(5) print(listener.getsockname()) try: while true: client, addr = listener.accept() subprocess.popen(['cat'], stdin=client, stdout=client, stderr=client) client.close() except keyboardinterrupt: pass finally: listener.close()
this requires posix-compliant operating system.
Comments
Post a Comment