fstream/fstream-util-pipe_to_tcp.py
2021-04-21 12:57:19 +02:00

68 lines
1.4 KiB
Python

#!/usr/bin/env python3
import sys
oldexcepthook = sys.excepthook
def newexcepthook(type,value,traceback):
oldexcepthook(type,value,traceback)
#input("Press ENTER to quit.")
sys.excepthook = newexcepthook
import os
p = os.path.join
pUp = os.path.dirname
s = False
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
s = os.path.realpath(sys.executable)
else:
s = os.path.realpath(__file__)
sp = pUp(s)
# script start
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)
connection = False
connectionLock = threading.Lock()
class pipeThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global connection
while True:
data = sys.stdin.buffer.read(1000)
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:
if type(connection) != bool:
connection.close()
except Exception as e:
print(e)
connection = conn
connection.settimeout(15)