-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
123 lines (95 loc) · 3.4 KB
/
Server.py
File metadata and controls
123 lines (95 loc) · 3.4 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import socket
from time import sleep
import threading
from matplotlib import pyplot as plt
import numpy as np
host = "127.0.0.1"
# do not take any reserved or well known ports
port = 55555
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
clients = []
nicknames = []
# broadcasting messages from the server to all the clients
def broadcast(message):
# implement the filtering of the messages here
# implement the brand analytics and tracking here
for client in clients:
client.send(message)
# brand names collection
brandName = {"bmw": 0, "toyota": 0}
xLevel = []
yLevel = []
def handle(client):
# blacklist of words
blacklist = ["male", "female", "Hindu", " Muslim", "Christian", "BMP", "Awami league"]
# brand names
y = xLevel
x = yLevel
while True:
try:
# receiving 1024 bytes
message = client.recv(1024)
message1 = message.decode('ascii')
message1 = message1.split(" ")
# print(message1) # to check the list
for i in message1:
if i in blacklist:
index = message1.index(i)
message1[index] = "*" * len(i)
for brand in brandName:
if brand in message1:
brandName[brand] += 1
x.append(brandName[brand])
y.append(brand)
message = " ".join(message1)
# data needs to be in bytes
message = message.encode("ascii")
broadcast(message) # broadcast takes bytes to broadcast
except:
# find out the index of the failed client frm the clients list
index = clients.index(client)
clients.remove(client)
client.close()
# we also remove the nickname of the removed client
nickname = nicknames[index]
broadcast(f"{nickname} left the chat!".encode('ascii'))
nicknames.remove(nickname)
break
# data graph show of brand names
def brandNamesDataDisplay():
sleep(20)
x = np.array(xLevel)
y = np.array(yLevel)
plt.bar(x, y)
plt.show()
print(brandName)
def receive():
while True:
print("receive function is running on the server!")
# returns a tuple
client, address = server.accept()
# you have cut down the address str type casting
print(f"Connected with {str(address)}")
# we need to ask the client for the nickname
# made change here
name = "NICK"
client.send(name.encode('ascii'))
nickname = client.recv(1024).decode('ascii')
nicknames.append(nickname)
clients.append(client)
print(f"Nickname of the client is {nickname}")
broadcast(f"{nickname} joined the chat".encode('ascii'))
# letting know the specific client that it has connected to the server
client.send("Connected to the server".encode('ascii'))
# define and run a thread
# because we want to be able to handle multi clients same time
thread = threading.Thread(target=handle, args=(client,))
thread.start()
# main method
print("Server is listening..........")
# receiving on another thread because matplotlib need to run on main thread or it crashes
t = threading.Thread(target=receive)
t.start()
brandNamesDataDisplay()