-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
76 lines (70 loc) · 2.1 KB
/
client.py
File metadata and controls
76 lines (70 loc) · 2.1 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
import socket
import threading
import pickle
# Choosing Nickname
nickname = input("Choose your nickname: ")
# Accepting the 1st encryption key from the client
fl = False
key = ""
x=0
while not fl:
try:
key = input("Enter the encryption key: ")
check = int(key)
except ValueError:
print("In-Valid Input (Enter a number)")
continue
fl = True
# Connecting To Server
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 5500))
# Listening to Server and Sending Nickname
def receive():
while True:
try:
# Receive Message From Server
# If 'NICK-ADMIN' Send nickname and key
message = client.recv(1024).decode('ascii')
if message == 'NICK-ADMIN':
data={"nickname":nickname, "key":key}
d=pickle.dumps(data)
client.send(d)
else:
print(message)
except:
# Close Connection When Error
print("Ended connection/An error has occured.")
client.close()
break
# Sending Messages To Server
def write():
while True:
msg=input()
if msg == '!END':
client.close()
message = '{}: {}'.format(nickname, msg)
data={"message": message, "msg": msg, "posString":""}
d=pickle.dumps(data)
client.send(d)
break
st=[]
pos=[]
x= int(key)%64
for i in range(len(msg)):
c = msg[i]
if c.isalpha():
st.append(chr(ord(c)-x))
pos.append(str(i)+" ")
else:
st.append(c)
encodedString = ''.join(st)
posString = "".join(pos)
message = '{}: {}'.format(nickname, encodedString)
data={"message": message, "msg": encodedString, "posString": posString}
d=pickle.dumps(data)
client.send(d)
# Starting Threads For Listening And Writing
receive_thread = threading.Thread(target=receive)
receive_thread.start()
write_thread = threading.Thread(target=write)
write_thread.start()