|
|
|
import os, socket, threading, sys |
|
|
|
host = "0.0.0.0" |
|
port = int(os.getenv("PORT", "5678")) |
|
|
|
print("listening on", host, port) |
|
print("hostname:", socket.gethostname()) |
|
try: |
|
print("local ips:", socket.gethostbyname_ex(socket.gethostname())[2]) |
|
except Exception as e: |
|
print("ip lookup error:", e) |
|
sys.stdout.flush() |
|
|
|
def handle_client(conn, addr): |
|
print("client connected:", addr) |
|
try: |
|
while True: |
|
data = conn.recv(1024) |
|
if not data: |
|
break |
|
print("received:", data.decode(errors="ignore").strip()) |
|
conn.sendall(data) |
|
except Exception as err: |
|
print("error:", err) |
|
finally: |
|
conn.close() |
|
print("client disconnected:", addr) |
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv: |
|
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|
srv.bind((host, port)) |
|
srv.listen() |
|
while True: |
|
conn, addr = srv.accept() |
|
threading.Thread(target=handle_client, args=(conn, addr), daemon=True).start() |