-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
68 lines (59 loc) · 2.47 KB
/
Copy pathserver.py
File metadata and controls
68 lines (59 loc) · 2.47 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import socket
import threading
HEADER_SIZE = 10
conn_id = 0
header = None
connections = []
def dispatch_message(message, id):
print("Dispatching message: '" + message + "' from ID '" + str(id) + "'")
message = message + "\nMessage: "
message = f'{len(message.encode("utf-8")):<10}' + message
message = message.encode("utf-8")
for connection in connections:
if connection[0] != id:
print("Sending to ID: " + str(connection[0]))
connection[1].send(bytes(message))
def handle_client(s, a, id):
global header
print(f"Connection from {a} has been established")
while True:
new_message = True
while True:
if new_message:
new_message = False
try:
header = str(s.recv(HEADER_SIZE).decode("utf-8"))
except ConnectionResetError:
for index, connection in enumerate(connections):
if connection[0] == id:
conn = connections.pop(index)
conn[1].close()
print(f"Connection from {a} has been closed")
return
if len(header) != 0:
message_length = int(header)
print(f'New message from {a}! Length: {message_length}')
message = s.recv(message_length).decode("utf-8")
if message.split()[-1] == "exit":
for index, connection in enumerate(connections):
if connection[0] == id:
conn = connections.pop(index)
conn[1].close()
print(
f"Connection from {a} has been closed by user")
return
print(message)
dispatch_message(message, id)
break
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 1234))
print("Server started at {}:{}".format(
socket.gethostbyname(socket.gethostname()), 1234))
s.listen(5)
while True:
print("Waiting for connection...")
clientsocket, address = s.accept()
connections.append((conn_id, clientsocket))
threading.Thread(target=handle_client, args=(
clientsocket, address, conn_id), daemon=True).start()
conn_id += 1