-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·215 lines (174 loc) · 6.12 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#
# Written by @pradosh-arduino on github
#
import socket
import signal
import threading
import json
import time
HOST = ''
PORT = 0
MAX_MEMBERS = 0
buffer_size = 4096
# Dictionary to store client connections
clients = {}
# Extras
def color_text(text, color):
colors = {
'red': '\033[91m',
'yellow': '\033[93m',
'blue': '\033[94m',
'green': '\033[92m',
'purple': '\033[95m',
'cyan': '\033[96m',
'white': '\033[97m',
'bold': '\033[1m',
'underline': '\033[4m',
'end': '\033[0m'
}
return f"{colors[color]}{text}{colors['end']}"
def error(message):
print(color_text("[Error] " + message, 'red'))
def warn(message):
print(color_text("[Warn] " + message, 'yellow'))
def info(message):
print(color_text("[Info] " + message, 'blue'))
# End extras
def is_valid_ip(ip):
try:
socket.inet_aton(ip)
return True
except socket.error:
return False
def is_valid_port(port):
try:
return 0 < port <= 65535
except ValueError:
return False
def stop_server(signal, frame):
broadcast("Server is stopping.")
for client_socket in clients.values():
client_socket.close()
exit(0)
def handle_client(client_socket, username):
while True:
try:
message = client_socket.recv(buffer_size).decode('utf-8')
if message == '/quit':
client_socket.close()
del clients[username]
broadcast(f'<font color=\'lightgrey\'>{username} has left the chat.</font>')
break
elif message == '/ping':
broadcast('<font color=\'lightgrey\'>Pong!</font>')
elif message == '/members':
client_socket.sendall((str(list(clients.keys()))).encode())
else:
broadcast(f'{username}: {message}')
except ConnectionResetError:
client_socket.close()
del clients[username]
broadcast(f'<font color=\'lightgrey\'>{username} has left the chat.</font>')
break
except OSError:
pass
def broadcast(message):
try:
for client_socket in clients.values():
client_socket.send(message.encode('utf-8'))
except Exception as e:
error(f"Error broadcasting message: {e}")
def handle_commands(command):
command = str(command)
if command == "/quit":
broadcast("Server is stopping.")
for client_socket in clients.values():
client_socket.close()
for i in range(10): # give it some time to clean up itself.
info(f"Please wait for {10 - i} seconds.")
time.sleep(1)
info("Now its safe to press Ctrl+C")
exit(0)
elif command.startswith("/kick "):
try:
arg1 = command.split("/kick ")[1];
clients[arg1].close()
except KeyError:
error("No member named as such to kick!")
elif command == "/kick":
error("Invalid command usage. (/kick <member-name>)")
else:
error("Invalid command passed, refer documents at <not yet>")
def command_threading():
while True:
cmd = input("")
handle_commands(cmd)
def main():
try:
with open('defaults.json') as json_file:
json_data = json.load(json_file)
if json_data['ip'] == "localhost":
HOST = "127.0.0.1"
else:
HOST = json_data['ip']
PORT = json_data['port']
MAX_MEMBERS = json_data['max-members']
if not is_valid_ip(HOST):
error("Not a valid IP has been saved in ./defaults.json!")
exit(2)
if not is_valid_port(PORT):
error("Not a valid port has been saved in ./defaults.json!")
exit(2)
if not type(MAX_MEMBERS) == int:
error("Not a valid member count has been saved in ./defaults.json!")
exit(2)
info("Loaded with ./defaults.json!")
except FileNotFoundError:
HOST = input("Enter the IP to host (localhost is also accepted) : ")
if HOST == "localhost":
HOST = '127.0.0.1'
if not is_valid_ip(HOST):
error("Not a valid IP has been entered!")
exit(1)
PORT = input("Enter server port : ")
PORT = int(PORT)
if not is_valid_port(PORT):
error("Not a valid port has been entered!")
exit(1)
MAX_MEMBERS = input("Enter maximum members allowed : ")
try:
MAX_MEMBERS = int(MAX_MEMBERS)
except:
error("Only integer is accepted for maximum members.")
exit(3)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
server.bind((HOST, PORT))
except:
error("The address is already in use.")
exit(4)
server.listen()
signal.signal(signal.SIGINT, stop_server)
info(f'Server is listening on {HOST}:{PORT}')
command_thread = threading.Thread(target=command_threading)
command_thread.start()
while True:
if len(clients) > MAX_MEMBERS:
# Server is full, reject new connections
client_socket, _ = server.accept()
client_socket.send("Server is full. Please try again later.".encode('utf-8'))
client_socket.close()
continue
# Accept new connections
client_socket, client_address = server.accept()
info(f'New connection from {client_address[0]}:{client_address[1]}')
username = client_socket.recv(buffer_size).decode('utf-8')
clients[username] = client_socket
broadcast(f'<font color=\'lightgrey\'>{username} has joined the chat.</font>')
if json_data['welcome-message']:
client_socket.send(("<br>" + str(json_data['welcome-message']).replace('{username}', username)).encode('utf-8'))
# Start a new thread to handle the client
client_thread = threading.Thread(target=handle_client, args=(client_socket, username))
client_thread.start()
if __name__ == "__main__":
main()