-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTransfer.py
More file actions
103 lines (76 loc) · 3.31 KB
/
FileTransfer.py
File metadata and controls
103 lines (76 loc) · 3.31 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
import gzip
import os
def compress_data(data):
try:
with gzip.compress(data) as compressed_data:
return compressed_data
except Exception as e:
print(f"Compression error: {e}")
return None
def decompress_data(compressed_data):
try:
with gzip.decompress(compressed_data) as decompressed_data:
return decompressed_data
except Exception as e:
print(f"Decompression error: {e}")
return None
def receive_file_from_client(client_socket, file_path):
save_directory = r"C:\Users\User\OneDrive\Desktop" # Specify the directory where you want to save the file
os.makedirs(save_directory, exist_ok=True) # Create the directory if it doesn't exist
try:
file_name = os.path.basename(file_path.strip())
abs_file_path = os.path.abspath(os.path.join(save_directory, file_name))
print(f"Saving file to: {abs_file_path}")
with open(abs_file_path, "wb") as file:
while True:
data = client_socket.recv(4096)
if not data:
break
print(f"Received data length: {len(data)}")
file.write(data)
print(f"File saved successfully at: {abs_file_path}")
# Sending acknowledgment back to the client
client_socket.send("File received successfully".encode('utf-8'))
# Ensure the file is closed before returning
file.close()
with open(abs_file_path, "rb") as file:
file_content = file.read()
print(f"Content of saved file length: {len(file_content)}")
# Add a return statement to break out of the file processing loop
return True
except OSError as e:
print(f"OS Error ({e.errno}): {e.strerror}")
except Exception as e:
print(f"Error while receiving file: {e}")
# Return False if there was an issue with file processing
return False
def receive_file_from_server(client_socket, file_path):
save_directory = r"C:\Users\User\Downloads" # Specify the directory where you want to save the file
os.makedirs(save_directory, exist_ok=True)
try:
file_name = os.path.basename(file_path.strip())
abs_file_path = os.path.abspath(os.path.join(save_directory, file_name))
print(f"Saving file to: {abs_file_path}")
with open(abs_file_path, "wb") as file:
while True:
data = client_socket.recv(4096)
if not data:
break
print(f"Received data length: {len(data)}")
file.write(data)
print(f"File saved successfully at: {abs_file_path}")
# Sending acknowledgment back to the client
client_socket.send("File received successfully".encode('utf-8'))
# Ensure the file is closed before returning
file.close()
with open(abs_file_path, "rb") as file:
file_content = file.read()
print(f"Content of saved file length: {len(file_content)}")
# Add a return statement to break out of the file processing loop
return True
except OSError as e:
print(f"OS Error ({e.errno}): {e.strerror}")
except Exception as e:
print(f"Error while receiving file: {e}")
# Return False if there was an issue with file processing
return False