-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatserver.py
161 lines (124 loc) · 5.48 KB
/
chatserver.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import threading
import socket
host = '127.0.0.1'
port = 65534
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
clients = []
names = []
def broadcast(message, name):
temp = message.decode('utf-8')
if ':' in temp:
temp = temp.split(':')[1][1:]
for client in clients:
if (clients.index(client) != names.index(name)) and (len(temp) != 1) and (len(message)!=0):
client.send(message)
def personal(client, name, sec_user):
while True:
try:
message = client.recv(1024)
temp = message.decode('utf-8')
if ':' in temp:
temp = temp.split(':')[1][1:]
if temp=='q':
index = clients.index(client)
clients.remove(client)
client.close()
name = names[index]
broadcast(f'{name} has left the chat room!'.encode('utf-8'), name)
names.remove(name)
break
elif temp=='l':
client.send(f">>> {len(names)} users are currently active".encode('utf-8'))
lis = ", ".join(names)
client.send(f"\n>>> {lis}".encode('utf-8'))
elif temp=='h':
client.send("\n####\n\n 'l' - list all active users\n 'c' - Connect to a single user\n 'r' - Return to chat room\n 'q' - Exit chat room\n\n####\n".encode('utf-8'))
elif temp=="c":
client.send('>>> Connecting to single user... \n>>> Enter username : '.encode('utf-8'))
sec_user = client.recv(1024).decode('utf-8').split(':')[1][1:]
if sec_user not in names:
client.send(">>> User doesn't exist or is inactive ".encode('utf-8'))
continue
personal(client, name, sec_user)
break
elif temp=='r':
client.send(">>> Returning back to chat room ".encode('utf-8'))
handle_client(client, name)
break
sec_cli = clients[names.index(sec_user)]
pers_m = message.decode('utf-8').split(':')
pers_m[0] = pers_m[0]+"(Personal) "
pers_m = ":".join(pers_m)
sec_cli.send(pers_m.encode('utf-8'))
except:
index = clients.index(client)
clients.remove(client)
client.close()
name = names[index]
broadcast(f'{name} has left the chat room!'.encode('utf-8'))
names.remove(name)
break
# Function to handle clients'connections
def handle_client(client, name):
while True:
try:
message = client.recv(1024)
temp = message.decode('utf-8')
if ':' in temp:
temp = temp.split(':')[1][1:]
if temp=='q':
print(">>>",name,"left the chat room")
index = clients.index(client)
clients.remove(client)
client.close()
name = names[index]
broadcast(f'{name} has left the chat room!'.encode('utf-8'), name)
names.remove(name)
break
elif temp=='l':
client.send(f">>> {len(names)} users are currently active".encode('utf-8'))
lis = ", ".join(names)
client.send(f">>> {lis}".encode('utf-8'))
elif temp=='h':
client.send("\n####\n\n 'l' - list all active users\n 'c' - Connect to a single user\n 'r' - Return to chat room\n 'q' - Exit chat room\n\n####\n".encode('utf-8'))
elif temp=="c":
client.send('>>> Connecting to single user... \n>>> Enter username : '.encode('utf-8'))
sec_user = client.recv(1024).decode('utf-8').split(':')[1][1:]
if sec_user not in names:
client.send(">>> User is inactive ".encode('utf-8'))
continue
personal(client, name, sec_user)
break
elif temp=='r':
client.send(">>> Returning back to chat room ".encode('utf-8'))
handle_client(client, name)
break
broadcast(message, name)
except:
index = clients.index(client)
clients.remove(client)
client.close()
name = names[index]
broadcast(f'{name} has left the chat room!'.encode('utf-8'))
names.remove(name)
break
# Main function to receive the clients connection
def receive():
print('>>> Server started ...')
while True:
print('>>> Server is running and listening ...')
client, address = server.accept()
client.send('username'.encode('utf-8'))
name = client.recv(1024).decode('utf-8')
names.append(name)
clients.append(client)
print(f'>>> Connection is established with {name} {str(address)}')
broadcast(f'** {name} has connected to the chat room **'.encode('utf-8'), name)
client.send('\n** You are now connected to chat room! **'.encode('utf-8'))
client.send("\n####\n\n 'l' - list all active users\n 'c' - Connect to a single user\n 'r' - Return to chat room\n 'q' - Exit chat room\n 'h' - help\n\n####\n".encode('utf-8'))
thread = threading.Thread(target=handle_client, args=(client,name))
thread.start()
if __name__ == "__main__":
receive()