-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
178 lines (132 loc) · 6.48 KB
/
server.py
File metadata and controls
178 lines (132 loc) · 6.48 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
__author__ = "Akshay Joshi"
import os
import time
# Set root directory of the repo into PATH
import sys
sys.path.append('../Question_3/')
# Import python socket library & custom file to track static variables
from socket import *
from parameters import *
class Server:
"""
Code to simulate operations of a Server.
Operations implemented:
1. Connection Setup
2. LIST_FILES
3. Upload
4. Download
5. Hash Generation & Matching
"""
def __init__(self):
self.server_port = PORT # Server Port
self.server_url = URL # Server URL
self.commands = COMMANDS # File Commands
self.seperator = SEP # Separator token for file name & file size strings
self.buffer_size = BUFFER_CAPACITY # Buffer size
self.received_files_path = RECEIVED_FILES_PATH
def send_md5_hash(self):
# Send the generated md5 hash back to client
self.connectSocket.send(self.hacker_cat_img_md5.encode())
def receive_command(self):
received = self.connectSocket.recv(self.buffer_size).decode(FORMAT)
# Check if LIST_FILES command is received
if received == COMMANDS[0]:
print(f"\nCommand received: {received}\n")
if os.listdir(self.received_files_path) == []:
msg = "[SERVER]: No files available at the moment!"
self.connectSocket.send(msg.encode())
self.connectSocket.send('DONE'.encode())
else:
files_available = os.listdir(self.received_files_path)
msg = "[SERVER]: Following files are available:-"
self.connectSocket.send(msg.encode())
for each_file in files_available:
self.connectSocket.send(each_file.encode())
self.connectSocket.send('DONE'.encode())
# Check if UPLOAD command is received
elif received == COMMANDS[1]:
print(f"\nCommand received: {received}\n")
msg = "[SERVER]: Please send corresponding filename and filesize!"
self.connectSocket.send(msg.encode())
data_received = self.connectSocket.recv(self.buffer_size).decode()
self.file_name, self.file_size = data_received.split(self.seperator)
# Typecasting from str to int
self.file_size = int(self.file_size)
msg = "[SERVER]: Ready to receive the file"
self.connectSocket.send(msg.encode())
try:
hacker_cat_temp_file = os.path.join(self.received_files_path, 'temp') + '.jpg'
with open(hacker_cat_temp_file, 'wb') as hacker_cat_img:
while True:
data_bytes = self.connectSocket.recv(self.buffer_size)
md5().update(data_bytes)
# When the End of File flag is reached (which means file transfer is complete)
if data_bytes == 'EOF'.encode():
break
hacker_cat_img.write(data_bytes)
self.hacker_cat_img_md5 = md5().hexdigest()
hacker_cat_img.close()
# Rename the temp file to something meaningfull
hacker_cat_save_file = os.path.join(self.received_files_path,
self.hacker_cat_img_md5 + ';' + self.file_name[:-4]
+ ';' + str(self.file_size)) + '.jpg'
if not os.path.isfile(hacker_cat_save_file):
os.rename(hacker_cat_temp_file, hacker_cat_save_file)
hacker_cat_img.close()
else:
os.remove(hacker_cat_temp_file)
except Exception as e:
print(e)
elif received == COMMANDS[2]:
print(f"\nCommand received: {received}\n")
msg = "[SERVER]: Please send a file ID"
self.connectSocket.send(msg.encode())
# Find the file which contains the respective file ID
file_id = self.connectSocket.recv(self.buffer_size).decode()
for fil_name in os.listdir(self.received_files_path):
if file_id in fil_name:
desired_file = fil_name
# Open the desired image based on the file ID (first 32 chars in the filename)
with open(os.path.join(self.received_files_path, desired_file), 'rb') as hacker_cat_img:
# Send the data
while True:
data_bytes = hacker_cat_img.read(self.buffer_size)
md5().update(data_bytes)
if not data_bytes:
break # All the data sent or empty file
# Using sendall instead of send as this method continues to send data
# from data_bytes until either all data has been sent or an error occurs.
# Don't need to use encode() as we are already loading the file in 'rb'
# mode
self.connectSocket.sendall(data_bytes)
time.sleep(0.1)
self.connectSocket.send('EOF'.encode())
self.hacker_cat_img_md5 = md5().hexdigest()
hacker_cat_img.close()
def communicate_to_client(self):
# Create TCP socket for future connections
self.serverSocket = socket(AF_INET, SOCK_STREAM)
# Bind URL and Port to the created socket
self.serverSocket.bind((self.server_url, self.server_port))
# Start listening for incoming connection (1 client at a time)
self.serverSocket.listen(1)
print("Server is listening on port: " + str(self.server_port))
while True:
# Accept incoming client connection
self.connectSocket, addr = self.serverSocket.accept()
print("Client connected: " + str(addr))
print("\n")
# Receive the command & decide suitable action (ex: upload/download)
self.receive_command()
self.receive_command()
# Send MD5 hash generated by the server
self.send_md5_hash()
self.receive_command()
self.receive_command()
self.send_md5_hash()
# Close TCP connection
self.connectSocket.close()
# Driver code
if __name__ == "__main__":
client = Server()
client.communicate_to_client()