trying to use chatgpt to debug
This commit is contained in:
42
testserverwithclass.py
Normal file
42
testserverwithclass.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
|
||||||
|
Class myClass():
|
||||||
|
HEADER = 64
|
||||||
|
PORT = 5000
|
||||||
|
SERVER = socket.gethostbyname(socket.gethostname())
|
||||||
|
ADDR = (SERVER, PORT)
|
||||||
|
FORMAT = 'utf-8'
|
||||||
|
DISCONNECT_MESSAGE = "!DISCONNECT"
|
||||||
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
server.bind(ADDR)
|
||||||
|
|
||||||
|
def handle_client(conn, addr):
|
||||||
|
print(f"[NEW CONNECTION] {addr} connected.")
|
||||||
|
|
||||||
|
connected = True
|
||||||
|
while connected:
|
||||||
|
msg_length = conn.recv(HEADER).decode(FORMAT)
|
||||||
|
if msg_length:
|
||||||
|
msg_length = int(msg_length)
|
||||||
|
msg = conn.recv(msg_length).decode(FORMAT)
|
||||||
|
if msg == DISCONNECT_MESSAGE:
|
||||||
|
connected = False
|
||||||
|
|
||||||
|
print(f"[{addr}] {msg}")
|
||||||
|
conn.send("Msg received".encode(FORMAT))
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
def start():
|
||||||
|
server.listen()
|
||||||
|
print(f"[LISTENING] Server is listening on {SERVER}")
|
||||||
|
while True:
|
||||||
|
conn, addr = server.accept()
|
||||||
|
thread = threading.Thread(target=handle_client, args=(conn, addr))
|
||||||
|
thread.start()
|
||||||
|
print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")
|
||||||
|
|
||||||
|
|
||||||
|
print("[STARTING] server is starting...")
|
||||||
|
start()
|
||||||
Reference in New Issue
Block a user