-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
178 lines (141 loc) · 5.13 KB
/
client.py
File metadata and controls
178 lines (141 loc) · 5.13 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
### ======================================= ###
'''Rishabh Goswami & Zahir Ali
client.py
Please install dependencies from requirements.txt and look at Readme.md for details.
The messages between client and server are encrypted using AES encryption
- Client first register itself on the server and set password,
if the username already registered then client simply provide password to authenticate
- After authentication client can chose to input either string of text or a file to get the prediction
whether the message/ messages are spam or ham.
'''
### ======================================= ###
import socket
import time
from Crypto.Cipher import AES
import os
from mlmodel import terminalColor
import tqdm
os.system("")
SERVER_IP = '127.0.0.1'
PORT = 6969
ADDR = (SERVER_IP, PORT)
FORMAT = 'utf-8'
BUFFER_SIZE = 1024
key = "EuZKgxZfCGPzCZEj"
IV = "POoddMaPkiPSQlsW"
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
print(f'{terminalColor.GREEN}Connected to server.{terminalColor.END}\n')
# Send file from client to server
def sendFile():
filesize = os.path.getsize("./client/input.csv")
#client.send(f'{filesize}'.encode())
progress = tqdm.tqdm(range(filesize), desc=f"Sending File...", unit="B", unit_scale=True, unit_divisor=1024)
filetosend = open("./client/input.csv", "rb")
#data = filetosend.read(BUFFER_SIZE)
while True:
data = filetosend.read(BUFFER_SIZE)
if not data:
break
client.sendall(data)
progress.update(len(data))
filetosend.close()
# Recieve the file from server to client
def receiveFile():
print(f'{terminalColor.GREEN}Receiving File...{terminalColor.END}')
filetodown = open("./client/output.csv", "wb")
while True:
data = client.recv(BUFFER_SIZE)
filetodown.write(data)
if len(data) < BUFFER_SIZE:
print(f"{terminalColor.CYAN}Transfer Complete.{terminalColor.END}")
break
filetodown.close()
print('Output File Saved in client directory.')
# Get username from the user and send the username to the server
def getuser():
username = input('Enter your username: ')
encryptedMessage = encrypted(username)
client.send(encryptedMessage)
data = receiveMessageReturn()
if data == 'UserOK.':
print(f'{terminalColor.CYAN}User verified.{terminalColor.END}')
getpass()
else:
print(f'{terminalColor.CYAN}User does not exist. Please create a password for one-time registration.{terminalColor.END}')
createpass()
# Get password from the user if user is registered and send the password to the server
def getpass():
password = input('Enter your password: ')
encryptedMessage = encrypted(password)
client.send(encryptedMessage)
data = receiveMessageReturn()
if data == 'Password verified.':
print(f'{terminalColor.GREEN}Password verified.{terminalColor.END}')
else:
print(f'{terminalColor.RED}Password not verified.{terminalColor.END}')
if password == 'q':
conn.close() #not needed
return
getpass()
# Get password from the user if user is NOT registered and send the password to the server
def createpass():
password = input('Enter your password: ')
encryptedMessage = encrypted(password)
client.send(encryptedMessage)
data = client.recv(1024)
response = decrypted(data)
print(response)
# Encrypt the message going from client to server
def encrypted(message,key = key,IV=IV):
message = message.encode(FORMAT)
encObj = AES.new(key.encode(FORMAT), AES.MODE_CFB, IV.encode(FORMAT))
encryptedMessage = encObj.encrypt(message)
return encryptedMessage
# Decrypt the messages coming from server to client
def decrypted(ciphertext,key = key,IV=IV):
encObj = AES.new(key.encode(FORMAT), AES.MODE_CFB, IV.encode(FORMAT))
message = encObj.decrypt(ciphertext)
return message.decode(FORMAT)
def receiveMessage():
encryptedResponse = client.recv(1024)
response = decrypted(encryptedResponse)
if len(response) > 0:
print(response)
def receiveMessageReturn():
encryptedResponse = client.recv(1024)
response = decrypted(encryptedResponse)
return response
def sendMessage(msg):
encryptedMessage = encrypted(msg)
client.send(encryptedMessage)
receiveMessage() #Welcome to spamdetector, login to continue.
getuser()
receiveMessage()
# Check if user wants to send string of text or a file
msg = input('Type your Response: ')
sendMessage(msg)
receiveMessage()
if msg.lower() == 'text':
msg = input('Enter the text to process: ')
sendMessage(msg)
receiveMessage()
receiveMessage()
elif msg.lower() == 'file':
sendFile()
print(f"\nFile Sent.")
print(f'\nPlease wait for the server to process and send output...\n')
time.sleep(5)
receiveFile()
time.sleep(2)
receiveMessage()
else:
receiveMessage()
# Keep the terminal/ Console open until user input quit
while True:
a = input(f'Type \'q\' or \'quit\' to close the console. \n')
if a.lower() == 'q' or a.lower() == 'quit':
client.close()
break
else:
continue