-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
349 lines (251 loc) · 12.9 KB
/
classes.py
File metadata and controls
349 lines (251 loc) · 12.9 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from time import sleep
import threading
import requests
import socket
import tqdm
import json
import os
class _Types:
ERROR = "error"
WARNING = "warning"
SUCCESS = "success"
IMPORTANT = "important"
DEBUG = "debug"
ADEBUG = "adebug"
class _Colors:
HEADER = '\033[95m'
BLUE = '\033[94m'
CYAN = '\033[96m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
colors = _Colors()
types = _Types()
def force_divide(n1, n2):
division_result = n1 // n2
dr_times_10 = division_result*10
excess = n1 - dr_times_10
return [dr_times_10, division_result, excess]
def prints(type: str, text: str, end_char: str = "", config = None):
if type == "error":
print(f"{colors.RED}[@] {text}{colors.ENDC}{end_char}")
elif type == "warning":
print(f"{colors.YELLOW}[!] {text}{colors.ENDC}{end_char}")
elif type == "success":
print(f"{colors.GREEN}[*] {text}{colors.ENDC}{end_char}")
elif type == "important":
print(f"{colors.BOLD}{colors.UNDERLINE}[*] {text}{colors.ENDC}{end_char}")
elif type == "debug":
if config is None:
prints(types.ERROR, "No config object passed to debug prints function")
if isinstance(config, dict):
if config["debug_mode"] == True:
print(f"{colors.CYAN}[DEBUG] {text}{colors.ENDC}{end_char}")
elif type == "adebug":
print(f"{colors.CYAN}{text}{colors.ENDC}{end_char}")
class Client:
"""
A client class to handle connections and file requests from a server.
Attributes:
myip (str): The IP address of the client.
sock (socket.socket): The socket object for the client.
Methods:
connect_to_server(ip: str, port: int):
Connects to the server at the specified IP address and port.
stop():
Closes the connection to the server.
request_file(filename: str):
Requests a file from the server and saves it to the 'Downloads' directory.
"""
def __init__(self, config):
self.myip = requests.get('https://api.ipify.org').text
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.config = config
def connect_to_server(self, ip: str, port: int):
self.server_address = (ip, port)
self.socket.connect(self.server_address)
prints(types.IMPORTANT, f"Connected to server at {ip}:{port}")
def disconnect_from_server(self):
try:
prints(type=types.SUCCESS, text=f"Disconnecting from server...")
self.socket.send("Command~#@#~disconnect".encode())
self.socket.close()
except Exception as e:
prints(type=types.ERROR, text=f"Error while disconnecting: {e}", config=self.config)
def stop(self):
self.disconnect_from_server()
self.socket.close()
prints(types.SUCCESS, "Connection closed", config=self.config)
def request_file(self, filename):
try:
prints(types.IMPORTANT, f"Requesting file '{filename}' from server...")
self.socket.send(f"Requesting~%!%~{filename}".encode())
file_size_response = self.socket.recv(1024).decode()
if file_size_response == "File not found":
prints(types.ERROR, f"File '{filename}' not found on server")
return
prints(types.DEBUG, f"File Size Response: {file_size_response}", config=self.config)
try:
file_size = int(file_size_response.split("~%!%~")[1])
except ValueError:
prints(types.ERROR, f"Invalid file size response: {file_size_response}, {file_size_response.__class__}")
file_size = int(file_size_response.split("~%!%~")[1])
mbfilesize = int((file_size/1048576) * (10**3)) / (10**3)
prints(type=types.ADEBUG, text=f"""File size is:
{file_size} Bytes
{mbfilesize}Mb""", config=self.config)
if not isinstance(file_size, int):
prints(types.ERROR, f"Invalid file size response: {file_size}, with type {file_size.__class__}")
return
prints(types.SUCCESS, f"File '{filename}' found on server with size {file_size} bytes")
prints(types.IMPORTANT, f"Receiving file '{filename}'...")
f = open(os.path.join("Downloads", filename), "wb")
file_bytes = b""
progress = tqdm.tqdm(unit="B", unit_scale=True, unit_divisor=1024, total=file_size)
### Receive Order ###
receive_order: list[int] = []
divisible, result, excess = force_divide(file_size, 10)
for i in range(10):
receive_order.append(result)
receive_order.append(excess)
receive_order.sort()
### ############# ###
### Receive ###
i = 0
for bytes_to_receive in receive_order:
i = i + 1
data = self.socket.recv(bytes_to_receive)
file_bytes += data
if len(file_bytes) == file_size:
done = True
progress.update(bytes_to_receive)
### ####### ###
file_bytes.replace(b"!<END>!", b"")
f.write(file_bytes)
progress.write(s=f"{colors.GREEN}[*] File '{filename}' received successfully.{colors.ENDC}")
except Exception as e:
prints(types.ERROR, f"Failed to receive file: {e}")
class Server():
"""
A class to represent a server that handles client connections and file requests.
Attributes
----------
ip : str
The IP address of the server.
port : int
The port number on which the server listens.
max_connections : int
The maximum number of simultaneous connections the server can handle.
server : socket.socket
The server socket.
public_ip : str
The public IP address of the server.
server_loop_thread : threading.Thread
The thread running the server loop.
server_loop_pid : int
The process ID of the server loop.
server_loop_running : bool
A flag indicating whether the server loop is running.
config : dict
Configuration settings for the server.
Methods
-------
start():
Starts the server and begins listening for connections.
server_loop(server: socket.socket):
The main loop that handles incoming client connections.
handle_client(client: socket.socket, address: tuple):
Handles communication with a connected client.
stop():
Stops the server and closes the server socket.
"""
def __init__(self, address: list[str], max_connections: int = 5):
self.ip = str(address[0])
self.port = int(address[1])
self.max_connections = max_connections
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.public_ip = requests.get('https://api.ipify.org').text
self.server_loop_thread: threading.Thread = None
self.server_loop_pid = None
self.server_loop_running = False
self.config = {
"debug_mode": False,
"fake_sleep": 0.5
}
def start(self):
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.server is None:
prints(types.ERROR, "Server socket is not initialized")
return
ip = self.ip
port = self.port
prints(type=types.DEBUG, text=f"Socket: {self.server}", config=self.config)
self.server.bind((ip, port))
self.server.listen(self.max_connections)
prints(types.SUCCESS, f"Server binded on {ip}:{port}")
self.server_loop_running = True
self.server_loop_thread = threading.Thread(target=self.server_loop, args=(self.server,), name="Server Loop")
self.server_loop_thread.start()
def server_loop(self, server: socket.socket):
self.server_loop_pid = os.getpid()
prints(types.SUCCESS, f"Server loop started with PID: {self.server_loop_pid}")
while self.server_loop_running:
try:
client, address = server.accept()
prints(types.SUCCESS, f"Accepted connection from: {address[0]}:{address[1]}")
threading.Thread(target=self.handle_client, args=(client, address), name=f"Handling {address[0]}:{address[1]}").start()
except OSError as e:
if not self.server_loop_running:
break
prints(types.ERROR, f"OSError: {e}")
except Exception as e:
prints(types.ERROR, f"Unexpected error: {e}")
def handle_client(self, client: socket.socket, address: tuple):
prints(types.SUCCESS, f"Handling client {address[0]}:{address[1]}")
try:
with client:
while self.server_loop_running:
request = client.recv(1024).decode()
if request.startswith("Requesting~%!%~"):
filename = request.split("~%!%~")[1]
prints(types.DEBUG, f"Received request for file '{filename}' from {address[0]}:{address[1]}", config=self.config)
if not os.path.exists(os.path.join("server_files", filename)):
prints(types.ERROR, f"Requested file '{filename}' does not exist.")
client.send("File not found".encode())
return
f = open(os.path.join("server_files", filename), "rb")
prints(type=types.DEBUG, text=f"Sending file size to client...", config=self.config)
file_size = int(os.path.getsize(os.path.join("server_files", filename)))
client.send(f"File size~%!%~{file_size}".encode())
prints(type=types.DEBUG, text=f"Sent file size", config=self.config)
prints(type=types.ADEBUG, text=f"Sending file to client...", config=self.config)
file_data = f.read()
client.sendall(file_data)
prints(types.SUCCESS, f"File '{filename}' with size {file_size} bytes sent to {address[0]}:{address[1]}")
f.close()
elif request.startswith("Command~#@#~"):
command = request.split("~#@#~")[1]
if command == "disconnect":
prints(type=types.DEBUG, text=f"Client {address[0]}:{address[1]} requested a disconnection.", config=self.config)
prints(type=types.DEBUG, text=f"Disconnecting client...", config=self.config)
break
else:
prints(types.ERROR, f"Invalid request from {address[0]}:{address[1]}")
except Exception as e:
prints(types.ERROR, f"Error handling client {address[0]}:{address[1]}: {e}")
finally:
client.close()
prints(types.SUCCESS, f"Connection closed for {address[0]}:{address[1]}")
def stop(self):
self.server_loop_running = False
if self.server_loop_thread:
self.server_loop_thread.join(timeout=10)
try:
self.server.close()
prints(types.DEBUG, "Server socket closed", config=self.config)
except OSError as e:
prints(types.ERROR, f"Error closing server socket: {e}")
prints(types.SUCCESS, "Server has been stopped")