-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
107 lines (79 loc) · 3.16 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
import socket
import threading
import sys
import rsa
public_key, private_key = rsa.newkeys(1024)
client_public_keys = {}
Host = 'localhost'
Port = 9999 # Any port between 0 and 65535
clients = []
def listen_for_messages(client, username):
while True:
response = rsa.decrypt(client.recv(1024), private_key).decode('utf-8')
if response != "":
if response == "send_file":
file_data = rsa.decrypt(client.recv(1024), private_key).decode('utf-8')
file = open("new_file", "w")
file.write(file_data)
file.close()
print("File received")
# Send the received file to all clients
with open("new_file", "r") as file:
file_contents = file.read(1048576)
for user in clients:
if user[1] != client:
send_file_to_client(user[1], file_contents)
continue
message = username + " : " + response
send_message_to_all(message, client)
else:
print("Message is empty")
def send_message_to_client(client, data):
recipient_public_key = client_public_keys[client]
#print(recipient_public_key)
client.sendall(rsa.encrypt(str(data).encode('utf-8'), recipient_public_key))
def send_message_to_all(data, client):
for user in clients:
if user[1] != client:
send_message_to_client(user[1], data)
else:
send_message_to_client(user[1], "[You] :" + data)
def client_handler(client,username):
while 1:
if username != "":
clients.append((username, client))
#print(client)
send_message_to_all(username + " has joined the chat", client)
break
else:
print("Username is empty")
break
threading.Thread(target=listen_for_messages, args=(client, username,)).start()
def send_file_to_client(client, file_contents):
try:
client.sendall(rsa.encrypt("send_file".encode('utf-8'), client_public_keys[client]))
client.sendall(rsa.encrypt(str(file_contents).encode('utf-8'), client_public_keys[client]))
print("File sent to a client")
except Exception as e:
print(f"Error sending file to client: {e}")
def main():
# Creating a socket object
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
server.bind((Host, Port))
print("Server started on port: %s" % Port)
except:
print("Bind failed. Error : " + str(sys.exc_info()))
sys.exit()
server.listen(5) # Now wait for client connection.
while True:
client, address = server.accept()
client.send(public_key.save_pkcs1("PEM"))
client_public_key = rsa.PublicKey.load_pkcs1(client.recv(1024))
client_username = client.recv(1024).decode('utf-8')
print("Got connection from", address)
print("Username: ", client_username)
client_public_keys[client] = client_public_key
threading.Thread(target=client_handler, args=(client,client_username)).start()
if __name__ == '__main__':
main()