Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,21 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import os,
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
| 8 |
-
print("
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
data = conn.recv(1024)
|
| 20 |
-
if not data:
|
| 21 |
-
break
|
| 22 |
-
print("received:", data.decode(errors="ignore").strip())
|
| 23 |
-
conn.sendall(data)
|
| 24 |
-
except Exception as err:
|
| 25 |
-
print("error:", err)
|
| 26 |
-
finally:
|
| 27 |
-
conn.close()
|
| 28 |
-
print("client disconnected:", addr)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
srv.bind((host, port))
|
| 33 |
-
srv.listen()
|
| 34 |
-
while True:
|
| 35 |
-
conn, addr = srv.accept()
|
| 36 |
-
threading.Thread(target=handle_client, args=(conn, addr), daemon=True).start()
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
import os,socket,threading,sys
|
| 3 |
|
| 4 |
+
port=int(os.getenv("PORT","5678"))
|
| 5 |
+
print("echo tcp on",port,flush=True)
|
| 6 |
|
| 7 |
+
def handle(c,a):
|
| 8 |
+
print("conn",a,flush=True)
|
| 9 |
+
while (d:=c.recv(1024)):
|
| 10 |
+
print("recv",d.decode(errors="ignore").strip(),flush=True)
|
| 11 |
+
c.sendall(d)
|
| 12 |
+
c.close()
|
| 13 |
+
print("disc",a,flush=True)
|
| 14 |
|
| 15 |
+
s=socket.socket()
|
| 16 |
+
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
|
| 17 |
+
s.bind(("0.0.0.0",port))
|
| 18 |
+
s.listen()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
while True:
|
| 21 |
+
threading.Thread(target=handle,args=s.accept(),daemon=True).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|