-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
90 lines (73 loc) · 2.53 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
import time
from person import Person
# GLOBAL CONSTANTS
HOST = 'localhost'
PORT = 5000
ADDR = (HOST, PORT)
MAX_CONNETIONS = 10
BUFSIZ = 512
# GLOBAL VARIABLES
persons = []
SERVER = socket(AF_INET, SOCK_STREAM)
SERVER.bind(ADDR) # set up server
def broadcast(msg, name):
"""
send new messages to all clients
:param msg: bytes["utf8"]
:param name: str
:return:
"""
for person in persons:
client = person.client
try:
client.send(bytes(name, "utf8") + msg)
except Exception as e:
print("[EXCEPTION]", e)
def client_communication(person):
"""
Thread to handle all messages from client
:param person: Person
:return: None
"""
client = person.client
# first message received is always the persons name
name = client.recv(BUFSIZ).decode("utf8")
person.set_name(name)
msg = bytes(f"{name} has joined the chat!", "utf8")
broadcast(msg, "") # broadcast welcome message
while True: # wait for any messages from person
msg = client.recv(BUFSIZ)
if msg == bytes("{quit}", "utf8"): # if message is qut disconnect client
client.close()
persons.remove(person)
broadcast(bytes(f"{name} has left the chat...", "utf8"), "")
print(f"[DISCONNECTED] {name} disconnected")
break
else: # otherwise send message to all other clients
broadcast(msg, name+": ")
print(f"{name}: ", msg.decode("utf8"))
def wait_for_connection():
"""
Wait for connecton from new clients, start new thread once connected
:return: None
"""
while True:
try:
client, addr = SERVER.accept() # wait for any new connections
person = Person(addr, client) # create new person for connection
persons.append(person)
print(f"[CONNECTION] {addr} connected to the server at {time.time()}")
Thread(target=client_communication, args=(person,)).start()
except Exception as e:
print("[EXCEPTION]", e)
break
print("SERVER CRASHED")
if __name__ == "__main__":
SERVER.listen(MAX_CONNETIONS) # open server to listen for connections
print("[STARTED] Waiting for connections...")
ACCEPT_THREAD = Thread(target=wait_for_connection)
ACCEPT_THREAD.start()
ACCEPT_THREAD.join()
SERVER.close()