diff --git a/fstream-util-pipe_to_tcp.py b/fstream-util-pipe_to_tcp.py index 0fb6e14..f10ca22 100644 --- a/fstream-util-pipe_to_tcp.py +++ b/fstream-util-pipe_to_tcp.py @@ -22,8 +22,11 @@ import socket import threading import queue +def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) + serverAddr = ("127.0.0.1",61921) serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +eprint("Opening socket...") serverSocket.bind(serverAddr) serverSocket.listen(1) @@ -35,23 +38,31 @@ class pipeThread(threading.Thread): threading.Thread.__init__(self) def run(self): + global connection while True: data = sys.stdin.buffer.read(1000) - try: - with connectionLock: - connection.send(data) - except Exception as e: - print(e) + + with connectionLock: + try: + if type(connection) != bool: + connection.send(data) + except Exception as e: + connection = False + print(e) pThread = pipeThread() pThread.start() while True: + eprint("Awaiting connection...") conn, address = serverSocket.accept() + eprint("Connection established, sending data.") with connectionLock: try: - connection.close() + if type(connection) != bool: + connection.close() except Exception as e: print(e) + connection = conn connection.settimeout(15) \ No newline at end of file