-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
145 lines (122 loc) · 3.92 KB
/
server.py
File metadata and controls
145 lines (122 loc) · 3.92 KB
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
import socket
import threading
import os
import random
import pickle
# Connection Data
host = '127.0.0.1'
port = 5500
DISCONNECT_MESSAGE = "!END"
# Starting Server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
print(f"[LISTENING] Server is listening on {host}")
#Saving message data into a file
if os.path.exists('PythonText.txt'):
os.remove('PythonText.txt')
else:
print("File doesn't exist")
if os.path.exists('PythonTextFull.txt'):
os.remove('PythonTextFull.txt')
else:
print("Full File doesn't exist")
# Lists For Clients and Their Nicknames
clients = []
nicknames = []
keys = []
# Sending Messages To All Connected Clients
def broadcast(message):
for client in clients:
client.send(message)
# Handling Messages From Clients
def handle(client):
while True:
try:
d=client.recv(2048)
data=pickle.loads(d)
message=data.get('message')
msg=data.get('msg')
posString=data.get('posString')
pos=[]
if len(posString)!=0:
pos = list(map(int, posString.split()))
# Removing And Closing Clients
if msg == DISCONNECT_MESSAGE:
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
broadcast('{} left!'.format(nickname).encode('ascii'))
nicknames.pop(index)
keys.pop(index)
print(nickname," left!")
break
s=[]
# New key
y = random.randint(1, 64)
index = clients.index(client)
x = keys[index]
for i in range(0, len(msg)):
c = msg[i]
if i in pos:
s.append(chr(ord(c)+x-y))
else:
s.append(c)
st="".join(s)
f = open("PythonText.txt", "w")
f.write(st)
f.close()
f = open("PythonTextFull.txt", "a")
f.write(st)
f.write('\nKey : ' + str(y)+'\n')
f.close()
f = open("PythonText.txt", "r")
echoString = f.read()
f.close()
echoString=st
s=[]
for i in range(0, len(echoString)):
c = echoString[i]
if i in pos:
s.append(chr(ord(c)+y))
else:
s.append(c)
newMsg = "".join(s)
name=(message.split(':')[0])
newMessage = '{}: {}'.format(name, newMsg)
# Broadcasting Messages
broadcast(newMessage.encode('ascii'))
except:
# Removing And Closing Clients
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
broadcast('{} left!'.format(nickname).encode('ascii'))
nicknames.pop(index)
keys.pop(index)
break
# Receiving / Listening Function
def receive():
while True:
# Accept Connection
conn, address = server.accept()
print("Connected with {}".format(str(address)))
# Request And Store Nickname
conn.send('NICK-ADMIN'.encode('ascii'))
d=conn.recv(2048)
data=pickle.loads(d)
nickname = data.get('nickname')
key = int(data.get('key'))
nicknames.append(nickname)
clients.append(conn)
keys.append(key%64)
# Print And Broadcast Nickname
print("Nickname is {}".format(nickname))
broadcast("{} joined!".format(nickname).encode('ascii'))
conn.send('Connected to server!'.encode('ascii'))
# Start Handling Thread For Client
thread = threading.Thread(target=handle, args=(conn,))
thread.start()
receive()