-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
111 lines (92 loc) · 3.56 KB
/
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
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
import socket
from os import listdir, walk
from os.path import (
abspath, join, dirname, getsize,
exists, isfile, isdir, basename
)
from buffer import Buffer
from path import convert_path
class FileClient:
connection = None
def __init__(self, *args, **kwargs):
# the ip address or hostname of the server, the receiver
self.host = kwargs.get("host", "localhost")
# the port, if not set, the port 5001 is default
self.port = kwargs.get("port", 5001)
# get separator
self.separator = kwargs.get("separator", "<SEPARATOR>")
# send 4096 bytes each time step
self.buffer_size = kwargs.get("buffer_size", 4096)
# absolute path
self.app_path = kwargs.get("app_path", dirname(abspath(__file__)))
# files path
self.path = kwargs.get("path", join(self.app_path, "input"))
# initilize socket connection
self.get_connection()
def get_connection(self):
if self.connection:
self.connection.close()
# create the client socket
self.connection = socket.socket()
print(f"[+] Connecting to {self.host}:{self.port}")
# connecting to the server:
def file_print(self, filename, filepath, filesize):
print("\n[FILE NAME]:", filename)
print("[FILE PATH]:", filepath)
print("[FILE SIZE]:", filesize, "\n")
def send_file(self, filename):
# join file path with filename
filepath = join(self.path, filename)
if not exists(filepath):
print(f"[ERROR]: file {filepath} doesn't exist")
return
filetype = "file" if isfile(filepath) or not isdir(filepath) else "folder"
if filetype == "folder":
# print(filepath)
# input()
if not listdir(filepath):
self.connection.close()
self.get_connection()
else:
path = basename(convert_path(filepath))
# print(path)
for root, subdirs, files in walk(filepath):
for f_ in files:
joined_path = join(path, f_)
# print(joined_path)
# input()
self.send_file(joined_path)
return
# get the file size
filesize = getsize(filepath)
self.connection.connect((self.host, self.port))
print("[+] Connected.")
sbuf = Buffer(self.connection, self.buffer_size)
sbuf.put_utf8(f"{filepath}{self.separator}{filesize}{self.separator}{filetype}")
# pretty print at terminal
self.file_print(filename, filepath, filesize)
# start sending the file
# import tqdm
# progress = tqdm.tqdm(
# range(filesize), f"Sending {filepath}", unit="B",
# unit_scale=True, unit_divisor=1024)
with open(filepath, "rb") as file_:
sbuf.put_bytes(file_)
self.connection.close()
self.get_connection()
def bulk_send_file(self):
for filename in listdir(self.path):
self.send_file(filename)
if __name__ == "__main__":
from local import (
client_host, port, buffer_size, separator,
app_path, client_path
)
# if host not set, the default is localhost
fc = FileClient(
host=client_host, port=port, buffer_size=buffer_size,
separator=separator, app_path=app_path, path=client_path
)
# fc = FileClient()
# fc.send_file("abc.txt")
fc.bulk_send_file() # send all files from file path to server