-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
101 lines (77 loc) · 2.65 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
import socket
from threading import Thread
import threading
import mysql.connector
from deep_translator import GoogleTranslator
HOST = '0.0.0.0'
PORT = 5500
CHAR_LIMIT = 2048
LISTENER_LIMIT = 10
active_clients = [] # List of all connected users
mydb = mysql.connector.connect(
host = '127.0.0.1',
user = 'Findlay',
password = 'helloworld',
database = 'COMMUNIFY')
mycursor = mydb.cursor()
# Function to listen for upcoming messages from a client
def listen_for_messages(client,username):
while True:
message = ''
message = client.recv(CHAR_LIMIT).decode('utf-8')
if GoogleTranslator(source='auto',target='en').translate(message) == 'quit':
print(f"{username} has quit :(")
active_clients.remove((username, client,))
client.shutdown(socket.SHUT_RD)
#client.close()
break
elif message != '':
final_msg = username + '-' + message
send_messages_to_all(final_msg)
else:
print(f'The message sent from client {username} is empty')
def send_messages_to_all(message):
for user in active_clients:
try:
send_message_to_client(user[1],message)
except Exception as e:
print("[EXCPETION]",e)
# Function to send message to a single client
def send_message_to_client(client, message):
client.sendall(message.encode())
# Function to handle client
def client_handler(client):
# Server will listen for client message that will
# Contain the username
while True:
try:
username = client.recv(CHAR_LIMIT).decode('utf-8')
print(f"{username} has joined server!")
active_clients.append((username, client))
break
except Exception as e:
print("[EXCEPTION]",e)
break
try:
Thread(target = listen_for_messages, args = (client, username, )).start()
except Exception as e:
print("[EXCEPTION]",e)
# Main function
def main():
# Creating the server socket class object
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Provide the server with an address in the form of
# host IP and port
server.bind((HOST, PORT))
print(f"Server is running on {HOST} {PORT}")
except:
print(f'Unable to bind to host: {HOST} and port: {PORT}')
server.listen(LISTENER_LIMIT)
while True:
(client, address) = server.accept()
print(f"Successfully connected to client {address[0]} {address[1]}")
Thread(target=client_handler,args=(client, )).start()
server.close()
if __name__ == '__main__':
main()