forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
75 lines (61 loc) · 2.02 KB
/
server.py
File metadata and controls
75 lines (61 loc) · 2.02 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
69
70
71
72
73
74
75
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
import time
from person import Person
HOST = "localhost"
PORT = 5500
ADDR = (HOST, PORT)
MAX_CONNECTION = 1
BUFFSIZ = 1024
persons = []
SERVER = socket(AF_INET, SOCK_STREAM)
SERVER.bind(ADDR)
def broadcast(msg, name):
for person in persons:
client = person.client
client.send(bytes(name, "utf8" + msg))
def client_communication(person):
'''handle all messages from client'''
client = person.client
name = client.recv(BUFFSIZ).decode("utf8")
person.set_name(name)
msg = bytes(f"{name} has joined the chat", "utf8")
broadcast(msg, name)
while True:
try:
msg = client.recv(BUFFSIZ)
print(f"{name}:", msg.decode("utf8"))
if msg == bytes("{quit}", "utf8"):
broadcast(f"{name} has left the chat ...", "")
client.send(bytes("{quit}", "utf8"))
client.close()
persons.remove(person)
break
else:
broadcast(msg, name+": ")
except Exception as e:
print("[EXCEPTION]", e)
break
def wait_for_connection():
'''wait for connection from new clients ,start new thread'''
run = True
while run:
try:
client, addr = SERVER.accept()
person = Person(addr, client)
persons.append(person)
print(f"[CONNECTION] {addr} connected to server at {time.time()} ")
Thread(target=client_communication, args=(person,)).start()
except Exception as e:
print("[EXCEPTION]", e)
run = False
print("server crash")
SERVER = socket(AF_INET, SOCK_STREAM)
SERVER.bind(ADDR)
if __name__ == "__main__":
SERVER.listen(MAX_CONNECTION)
print("waiting for connection")
ACCEPT_THREAD = Thread(target=wait_for_connection)
ACCEPT_THREAD.start()
ACCEPT_THREAD.join()
SERVER.close()