-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
44 lines (34 loc) · 1.19 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import socket
import threading
HEADER_SIZE = 64 # in bytes
FORMAT = "utf-8"
PORT = 3000
IP_ADDRESS = socket.gethostbyname(socket.gethostname())
SERVER_ADDRESS = (IP_ADDRESS, PORT)
DISCONNECT_MESSAGE = "!DISCONNECT"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(SERVER_ADDRESS)
def handle_request(new_connection, client_ip):
print(f"[SERVER] {client_ip} connected")
connected = True
while connected:
msg_length = new_connection.recv(HEADER_SIZE).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
actual_msg = new_connection.recv(msg_length).decode(FORMAT)
if actual_msg == DISCONNECT_MESSAGE:
break
print(f"[{client_ip}] {actual_msg}")
new_connection.close()
def start():
server.listen()
print(f"[SERVER] Server started at {SERVER_ADDRESS}")
while True:
new_connection, client_ip = server.accept()
thread = threading.Thread(
target=handle_request, args=(new_connection, client_ip))
thread.start()
print(
f"[SERVER] Total active thread(s): {threading.activeCount() - 1}")
print("[SERVER] Starting...")
start()