-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.py
111 lines (93 loc) · 3.76 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import threading
import socket
# Now this Host is the IP address of the Server, over which it is running.
# I've user my localhost.
host = "192.168.2.104"
port = 5555 # Choose any random port which is not so common (like 80)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the server to IP Address
server.bind((host, port))
# Start Listening Mode
server.listen()
# List to contain the Clients getting connected and nicknames
clients = []
nicknames = []
# 1.Broadcasting Method
def broadcast(message):
for client in clients:
client.send(message)
# 2.Receiving Messages from client then broadcasting
def handle(client):
while True:
try:
msg = message = client.recv(1024)
if msg.decode('ascii').startswith('KICK'):
if nicknames[clients.index(client)] == 'admin':
name_to_kick = msg.decode('ascii')[5:]
kick_user(name_to_kick)
else:
client.send('Command Refused!'.encode('ascii'))
elif msg.decode('ascii').startswith('BAN'):
if nicknames[clients.index(client)] == 'admin':
name_to_ban = msg.decode('ascii')[4:]
kick_user(name_to_ban)
with open('bans.txt', 'a') as f:
f.write(f'{name_to_ban}\n')
print(f'{name_to_ban} was banned by the Admin!')
else:
client.send('Command Refused!'.encode('ascii'))
else:
broadcast(message) # As soon as message received, broadcast it.
except socket.error:
if client in clients:
index = clients.index(client)
# Index is used to remove client from list after getting disconnected
client.remove(client)
client.close()
nickname = nicknames[index]
broadcast(f'{nickname} left the Chat!'.encode('ascii'))
nicknames.remove(nickname)
break
# Main Receive method
def receive():
while True:
client, address = server.accept()
print(f"Connected with {str(address)}")
# Ask the clients for Nicknames
client.send('NICK'.encode('ascii'))
nickname = client.recv(1024).decode('ascii')
# If the Client is an Admin prompt for the password.
with open('bans.txt', 'r') as f:
bans = f.readlines()
if nickname + '\n' in bans:
client.send('BAN'.encode('ascii'))
client.close()
continue
if nickname == 'admin':
client.send('PASS'.encode('ascii'))
password = client.recv(1024).decode('ascii')
# I know it is lame, but my focus is mainly for Chat system and not a Login System
if password != 'adminpass':
client.send('REFUSE'.encode('ascii'))
client.close()
continue
nicknames.append(nickname)
clients.append(client)
print(f'Nickname of the client is {nickname}')
broadcast(f'{nickname} joined the Chat'.encode('ascii'))
client.send('Connected to the Server!'.encode('ascii'))
# Handling Multiple Clients Simultaneously
thread = threading.Thread(target=handle, args=(client,))
thread.start()
def kick_user(name):
if name in nicknames:
name_index = nicknames.index(name)
client_to_kick = clients[name_index]
clients.remove(client_to_kick)
client_to_kick.send('You Were Kicked from Chat !'.encode('ascii'))
client_to_kick.close()
nicknames.remove(name)
broadcast(f'{name} was kicked from the server!'.encode('ascii'))
# Calling the main method
print('Server is Listening ...')
receive()