-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
157 lines (119 loc) · 4.8 KB
/
client.py
File metadata and controls
157 lines (119 loc) · 4.8 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
import socket
import threading
import json
import struct
import os
import time
running = True
request_recv = 0
request_send = 0
recieving_file = False
def get_json_data(client):
output = ""
while not output.endswith("\n"):
byte = client.recv(1)
if byte == 0: return False
output += byte.decode("utf-8")
output.replace("\n", "")
return output
def handle_rec(client):
global running
global recieving_file
global request_recv
global request_send
while(running):
result = get_json_data(client)
file_name = ""
file_size = 0
total_file_size = 0
current_file_size = 0
if not result:
break
json_data = json.loads(result)
if json_data.get("type") == "request":
print(json_data.get("message"))
print("\ruse the command accept to recieve the file or reject to deny")
print("Enter command: ")
request_recv = json_data.get("from")
elif json_data.get("type") == "accept":
request_send = json_data.get("from")
print("\r" + json_data.get("message"))
print("Enter command: ")
elif json_data.get("type") == "file_metadata":
print("Creating File: " + json_data.get("file_name"))
file_name = json_data.get("file_name")
file_size = json_data.get("file_size")
total_file_size = file_size
recieving_file = True
os.makedirs("Download", exist_ok=True)
with open("Download/" + file_name, "wb") as f:
finished_downloading = False
byte_chunk_size = 1024
while (not finished_downloading):
chunck_size = byte_chunk_size
if current_file_size + chunck_size > file_size:
chunck_size = file_size - current_file_size
server_message = client.recv(chunck_size)
f.write(server_message)
current_file_size += len(server_message)
print(f"File Downloading: Filesize {current_file_size} / {file_size}")
if current_file_size >= file_size:
finished_downloading = True
recieving_file = False
# print(f"File Downloading Complete: Filesize {current_file_size} / {file_size}")
elif json_data.get("type") == "file_transfer_finished":
print(json_data.get("message"))
client = socket.create_connection(("localhost", 3000))
data = client.recv(4)
client_id = struct.unpack("i", data)[0]
print(f"Connected to server\nClient id: {client_id}")
threading.Thread(target=handle_rec, args=(client,), daemon=True).start()
while(running):
if recieving_file:
time.sleep(1)
continue
command = input("Enter command: ")
if recieving_file:
time.sleep(1)
continue
cmd = command
arguments = []
if " " in command:
cmd = command.split(" ")[0]
arguments = command.split(" ")
arguments.remove(command.split(" ")[0])
if cmd == "request":
if len(arguments) > 0:
print(f"Sending a request to client {arguments[0]}.")
client.sendall((json.dumps({"type": "request", "target": int(arguments[0])}) + "\n").encode("utf-8"))
else:
target = input("Enter request target: ")
print(f"Sending a request to client {target}.")
client.sendall((json.dumps({"type": "request", "target": int(target)}) + "\n").encode("utf-8"))
elif cmd == "exit":
running = False
elif cmd == "accept" and request_recv != 0:
client.send((json.dumps({"type": "accept", "to": request_recv}) + "\n").encode("utf-8"))
elif cmd == "reject" and request_recv != 0:
client.send((json.dumps({"type": "reject", "to": request_recv}) + "\n").encode("utf-8"))
elif cmd == "send" and request_send != 0:
file_path = ""
if len(arguments) > 0:
file_path = arguments[0]
else:
file_path = input("Enter File path: ")
file_name = file_path.split("/")[-1]
file_size = os.stat(file_path).st_size
client.sendall((json.dumps({"type": "file_metadata", "file_name": file_name, "file_size": file_size, "to": request_send}) + "\n").encode("utf-8"))
with open(file_path, "rb") as f:
chunck_size = 1024
while True:
chunk = f.read(chunck_size)
if not chunk: break
client.sendall(chunk)
client.sendall((json.dumps({
"type": "file_transfer_finished",
"message": f"Finished sending {file_name}",
"to": request_send
}) + "\n").encode("utf-8"))
client.close()