-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
61 lines (49 loc) · 1.95 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
import socket
from _thread import *
from colorama import Fore,Back
srvr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 127
srvr.bind(('', port))
print("Socket successfully created at port : ",port)
print("Server Is Up And Running ! !")
clients = []
srvr.listen()
print("Socket is Listening...")
welcome_string = " Welcome to The Chatroom ! ! "
def Client_Thread(client, address):
while True:
try:
msg = client.recv(3072).decode()
if msg:
msg_sent = Fore.GREEN + "From: " + str(address[0]) + ':' + str(address[1]) + '\n' + Fore.YELLOW + "Message: " + str(msg) + Fore.RESET
print(msg_sent)
sendtoall(msg_sent, client)
else:
RemoveFromList(client)
client_left = Back.WHITE + Fore.BLACK + str(address[0]) + ':' + str(
address[1]) + " Left The Chatroom ! !" + Back.RESET+ Fore.RESET
print('\n',client_left,'\n')
sendtoall(client_left,client)
return
except:
continue
def sendtoall(msg, sender):
for client in clients:
if client != sender:
try:
client.send(msg.encode())
except:
client.close()
RemoveFromList(client)
def RemoveFromList(client):
if client in clients:
clients.remove(client)
while True:
client, address = srvr.accept()
print(Back.WHITE,Fore.BLACK,f"New User Joined {address[0]}:{address[1]}\n\n",Back.RESET,Fore.RESET)
welcome = Back.WHITE + Fore.BLACK + str(address[0]) + " : " + str(address[1]) + welcome_string + Back.RESET + Fore.RESET
user_joined = Back.WHITE + Fore.BLACK + str(address[0]) + " : " + str(address[1]) + " Just Joined The Chatroom ! !" + Back.RESET + Fore.RESET
sendtoall(user_joined, client)
client.send(welcome.encode())
clients.append(client)
start_new_thread(Client_Thread, (client, address))