-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friendly_client.py
51 lines (44 loc) · 1.17 KB
/
friendly_client.py
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
import socket
import sys
import time
import pickle
import os
c = socket.socket()
address = sys.argv[1]
port = sys.argv[2]
filename = sys.argv[3] if len(sys.argv) > 3 else None
c.connect((address, int(port)))
files = pickle.loads(c.recv(4096))
if filename is None:
separator = {
"posix": "/", # Linux
"nt": "\\", # Windows
"java": "/" # MacOS
}
for directory in files:
for file in directory[2]:
print(f"{directory[0]}{separator[os.name]}{file}")
c.close()
sys.exit(2)
c.send(bytes(filename, encoding="utf8"))
status = str(c.recv(256), encoding="utf8")
if status == "NO\n":
print("No such file exists on the server.")
else:
status = int(status)
print("Download started.")
with open("download/" + filename, "w") as file:
file.write("")
ts = time.time()
size = status
while status > 0:
data = c.recv(256)
with open("download/" + filename, "ab") as file:
file.write(data)
print("█", end="")
status -= 256
download_time = time.time() - ts
print(f"\nDownload complete."
f"Download time: {download_time}"
f"Download size: {size}"
f"Download speed: {size / download_time}")