-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
387 lines (365 loc) · 11.9 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# client.py
# Import libraries
import sys, os, socket, json
# Get message
def get_message(sock):
message = ''
sock.settimeout(3)
while True:
try:
m = sock.recv(1024).decode()
if not m:
break
message += m
except:
break
message = json.loads(message)
return message
# Client initialize
def init(server, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
s.send('init'.encode())
message = get_message(s)
if message['ok']:
min_free = None
for storage in message['storages']:
server, port = storage.split(' ')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as st:
st.connect((server, int(port)))
st.send('init'.encode())
free = int(st.recv(1024).decode())
if not min_free or free < min_free:
min_free = free
print(min_free, 'bytes free')
return True
return False
# Create new file
def create_file(server, port, rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'touch ' + rel_path
s.send(message.encode())
message = get_message(s)
if message['ok']:
for storage in message['storages']:
server, port = storage.split(' ')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as st:
st.connect((server, int(port)))
msg = 'touch ' + message['uuid']
st.send(msg.encode())
return True
return False
# Read file
def read_file(server, port, rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'get ' + rel_path
s.send(message.encode())
message = get_message(s)
if message['ok']:
server, port = message['storage'].split(' ')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as st:
st.connect((server, int(port)))
msg = 'get ' + message['uuid']
st.send(msg.encode())
st.settimeout(3)
with open(os.path.basename(rel_path), 'wb') as fs:
while True:
try:
data = st.recv(1024)
if not data:
break
fs.write(data)
except:
break
return True
return False
# Write file
def write_file(server, port, local_path, rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'put ' + os.path.basename(local_path) + ' ' + rel_path
s.send(message.encode())
message = get_message(s)
if message['ok']:
for storage in message['storages']:
server, port = storage.split(' ')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as st:
st.connect((server, int(port)))
msg = 'put ' + message['uuid']
st.send(msg.encode())
st.recv(1024)
with open(local_path, 'rb') as fs:
while True:
data = fs.read(1024)
if not data:
break
st.send(data)
return True
return False
# Delete file
def delete(server, port, rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'rm ' + rel_path
s.send(message.encode())
message = get_message(s)
if message['ok']:
if 'uuids' in message:
confirm = input('%s not empty. Are you sure (y/n)? ' % (rel_path))
if confirm == 'y' or confirm == 'yes':
s.send('y'.encode())
for uuid in message['uuids']:
for storage in message['storages']:
server, port = storage.split(' ')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as st:
st.connect((server, int(port)))
msg = 'rm ' + uuid
st.send(msg.encode())
return True
else:
s.send('n'.encode())
else:
if 'uuid' in message:
for storage in message['storages']:
server, port = storage.split(' ')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as st:
st.connect((server, int(port)))
msg = 'rm ' + message['uuid']
st.send(msg.encode())
return True
return False
# Get information about the file
def get_file_info(server, port, rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'info ' + rel_path
s.send(message.encode())
message = get_message(s)
if message['ok']:
server, port = message['storage'].split(' ')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as st:
st.connect((server, int(port)))
msg = 'info ' + message['uuid']
st.send(msg.encode())
msg = get_message(st)
print('%s\t %d bytes\t %s\t %s' %(msg['mode'], msg['size'], msg['mtime'], rel_path))
return True
return False
# Copy file
def copy_file(server, port, src_rel_path, dst_rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'cp ' + src_rel_path + ' ' + dst_rel_path
s.send(message.encode())
message = get_message(s)
if message['ok']:
src_uuid, dst_uuid = message['uuids']
for storage in message['storages']:
server, port = storage.split(' ')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as st:
st.connect((server, int(port)))
msg = 'cp ' + src_uuid + ' ' + dst_uuid
st.send(msg.encode())
return True
return False
# Move file
def move_file(server, port, src_rel_path, dst_rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'mv ' + src_rel_path + ' ' + dst_rel_path
s.send(message.encode())
status = s.recv(1024).decode()
if status == 'ok':
return True
return False
# Get path of the file
def get_path(root, new):
return os.path.normpath(os.path.join(root, new))
# Open directory
def open_dir(server, port, rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'cd ' + rel_path
s.send(message.encode())
status = s.recv(1024).decode()
if status == 'ok':
return True
return False
# Read directory
def read_dir(server, port, rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'ls ' + rel_path
s.send(message.encode())
message = get_message(s)
if message['ok']:
for d in message['dirs']:
print('\033[1m' + d + '\033[0m', end='\t')
for f in message['files']:
print(f, end='\t')
print()
return True
return False
# Make directory
def make_dir(server, port, rel_path):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((server, port))
message = 'mkdir ' + rel_path
s.send(message.encode())
status = s.recv(1024).decode()
if status == 'ok':
return True
return False
# Make help function for the client
def help():
print("""Miscellaneous:
init Initalize storage
help Show help
exit Exit
File actions:
touch files... Create empty files in DFS
get files... Download files from DFS
put src dst Upload local file to DFS
rm files... Remove files from DFS
info files... Get files' information
cp src dst Copy a file from src to dst
mv src dst Move a file from src to dst
Directory actions:
cd dir Change working directory
ls dir List a directory
mkdir dirs... Make empty directories
rm dirs... Remove directories""")
def greeting():
print("""First time connect to the server? Type:
\033[1minit\033[0m
For help, type:
\033[1mhelp\033[0m""")
# Main method
def main():
if len(sys.argv) != 3:
print('Usage:', sys.argv[0], 'server port')
sys.exit(1)
home = '/'
server, port = sys.argv[1:]
try:
port = int(port)
except:
print('port must be an integer')
sys.exit(1)
greeting()
while True:
command = input('\033[92mclient\033[0m:\033[94m%s\033[0m$ ' % home).split()
if (len(command)) == 0:
continue
if command[0] == 'help':
help()
elif command[0] == 'exit':
break
elif command[0] == 'init':
if len(command) != 1:
print('Usage: init')
else:
if not init(server, port):
print('Failed to initalize storage')
elif command[0] == 'touch':
if len(command) < 2:
print('Usage: touch files...')
else:
for f in command[1:]:
rel_path = get_path(home, f)
if not create_file(server, port, rel_path):
print('Failed to create', f)
else:
print(f, 'created')
elif command[0] == 'get':
if len(command) < 2:
print('Usage: get files...')
else:
for f in command[1:]:
rel_path = get_path(home, f)
if not read_file(server, port, rel_path):
print('Failed to download', f)
else:
print(f, 'downloaded')
elif command[0] == 'put':
if len(command) != 3:
print('Usage: put local_file file')
else:
if not os.path.isfile(command[1]):
print(command[1], 'not exist')
else:
dst_rel_path = get_path(home, command[2])
if not write_file(server, port, command[1], dst_rel_path):
print('Failed to upload', command[1])
else:
print(command[1], 'uploaded')
elif command[0] == 'rm':
if len(command) < 2:
print('Usage: rm files... | directories...')
else:
for t in command[1:]:
rel_path = get_path(home, t)
if not delete(server, port, rel_path):
print('Failed to delete', t)
else:
print(t, 'deleted')
elif command[0] == 'info':
if len(command) < 2:
print('Usage: info files...')
else:
for f in command[1:]:
rel_path = get_path(home, f)
if not get_file_info(server, port, rel_path):
print('Failed to get', f, 'info')
elif command[0] == 'cp':
if len(command) != 3:
print('Usage: cp src dst')
else:
src_rel_path = get_path(home, command[1])
dst_rel_path = get_path(home, command[2])
if not copy_file(server, port, src_rel_path, dst_rel_path):
print('Failed to copy from', command[1], 'to', command[2])
else:
print(command[1], 'coppied')
elif command[0] == 'mv':
if len(command) != 3:
print('Usage: mv src dst')
else:
src_rel_path = get_path(home, command[1])
dst_rel_path = get_path(home, command[2])
if not move_file(server, port, src_rel_path, dst_rel_path):
print('Failed to move', command[1], 'to', command[2])
else:
print(command[1], 'moved')
elif command[0] == 'cd':
if len(command) > 2:
print('Usage: cd dir')
else:
rel_path = get_path(home, command[1])if len(command) == 2 else '/'
if not open_dir(server, port, rel_path):
print('Failed to change to', command[1])
else:
home = rel_path
elif command[0] == 'ls':
if len(command) > 2:
print('Usage: ls dir')
else:
rel_path = get_path(home, command[1] if len(command) == 2 else '.')
if not read_dir(server, port, rel_path):
print('Failed to list', command[1])
elif command[0] == 'mkdir':
if len(command) < 2:
print('Usage: mkdir dirs...')
else:
for d in command[1:]:
rel_path = get_path(home, d)
if not make_dir(server, port, rel_path):
print('Failed to create', d)
else:
print(d, 'created')
else:
print('Command not found')
if __name__ == '__main__':
main()