-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp.py
844 lines (719 loc) · 27 KB
/
ftp.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
import os
import sys
import select
import socket
from socket import _GLOBAL_DEFAULT_TIMEOUT
from pathlib import Path
from tqdm import tqdm
CRLF = '\r\n'
B_CRLF = b'\r\n'
FTP_PORT = 21
MAXLINE = 8192
_227_re = None
_150_re = None
# https://stackoverflow.com/questions/287871/print-in-terminal-with-colors
ENDC = '\033[0m'
BOLD = '\033[1m'
ITALIC = '\033[3m'
UNDERLINE = '\033[4m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
OKBLUE = '\033[94m'
BGCOLOR = '\033[6;30;42m'
# Exception raised when an error or invalid response is received
class Error(Exception): pass
class error_reply(Error): pass # unexpected [123]xx reply
class error_temp(Error): pass # 4xx errors
class error_perm(Error): pass # 5xx errors
class error_proto(Error): pass # response does not begin with [1-5]
all_errors = {Error, IOError, EOFError}
class FTP:
def __init__(self, host=None, user=None, passwd=None, acct=None,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):
self.host = ''
self.port = FTP_PORT
self.sock = None
self.file = None
self.welcome = None
self.passiveserver = 1
self.maxline = MAXLINE
self.source_address = source_address
self.encoding = 'latin-1' # Extended ASCII
self.timeout = timeout
if host:
self.connect(host)
if user:
self.login(user, passwd, acct)
def putline(self, cmd):
cmd = cmd + CRLF
self.sock.sendall(cmd.encode(self.encoding))
# RFC-959 Page 35
def getline(self):
line = self.file.readline()
if not line:
raise EOFError
if line[-2:] == CRLF:
line = line[:2]
elif line[-1:] in CRLF:
line = line[:-1]
# print(line)
return line
def getmultiline(self):
line = self.getline()
if line[3:4] == '-':
code = line[:3]
while 1:
nextline = self.getline()
line = line + ('\n' + nextline)
if nextline[:3] == code and nextline[3:4] != '-':
break;
return line
def getresp(self):
resp = self.getmultiline()
self.lastresp = resp[:3]
c = resp[:1]
if c in {'1','2','3'}:
return resp
if c == '4':
raise error_temp(resp)
if c == '5':
raise error_perm(resp)
raise error_proto(resp)
def send_noop(self):
# https://stackoverflow.com/questions/15170503/checking-a-python-ftp-connection
self.voidcmd('NOOP')
def sendcmd(self, cmd):
self.putline(cmd)
return self.getresp()
def voidcmd(self, cmd):
self.putline(cmd)
resp = self.getresp()
if resp[:1] != '2':
raise error_reply(resp)
return resp
def voidresp(self):
"""Expect a response beginning with '2'."""
resp = self.getresp()
if resp[:1] != '2':
raise error_reply(resp) # empty exception
return resp
def login(self, user='', passwd='', acct=''):
if not user: user = 'anonymous'
if not passwd: passwd = ''
if not acct: acct = ''
if user == 'anonymous' and passwd in {'','-'}:
passwd = passwd + 'anonymous@'
resp = self.sendcmd('USER ' + user)
if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd)
if resp[0] == '3': resp = self.sendcmd('ACCT ' + acct)
if resp[0] != '2': raise error_reply(resp)
return resp
def connect(self, host=None, port=None, timeout=None, source_address=None):
# Override presets
if host is not None: self.host = host
if port is not None: self.port = port
if timeout is not None: self.timeout = timeout
if source_address is not None: self.source_address
# IPv4 address of FTP host
host_addr = (self.host, self.port)
# Convenience function: socket -> bind -> connect
self.sock = socket.create_connection(
host_addr, timeout=self.timeout, source_address=self.source_address)
# self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
# necessary
self.af = self.sock.family
# file-like interface for socket reading
self.file = self.sock.makefile(mode='r', encoding=self.encoding)
self.welcome = self.getresp()
return self.welcome
def sendport(self, host, port):
'''Send a PORT command with the current host and the given
port number.
'''
hbytes = host.split('.')
pbytes = [repr(port//256), repr(port%256)]
bytes = hbytes + pbytes
cmd = 'PORT ' + ','.join(bytes)
return self.voidcmd(cmd)
def sendeprt(self, host, port):
'''Send an EPRT command with the current host and the given port number.'''
af = 0
if self.af == socket.AF_INET:
af = 1
if self.af == socket.AF_INET6:
af = 2
if af == 0:
raise error_proto('unsupported address family')
fields = ['', repr(af), host, repr(port), '']
cmd = 'EPRT ' + '|'.join(fields)
return self.voidcmd(cmd)
def makeport(self):
err = None
sock = None
for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res
try:
sock = socket.socket(af, socktype, proto)
sock.bind(sa)
except OSError as _:
err = _
if sock:
sock.close()
sock = None
continue
break
if sock is None:
if err is not None:
raise err
else:
raise OSError("getaddrinfo returns an empty list")
sock.listen(1)
port = sock.getsockname()[1]
host = self.sock.getsockname()[0]
if self.af == socket.AF_INET:
resp = self.sendport(host, port)
else:
resp = self.sendeprt(host, port)
if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
sock.settimeout(self.timeout)
return sock
def makepasv(self):
if self.af == socket.AF_INET:
host, port = parse227(self.sendcmd('PASV'))
else:
host, port = parse229(self.sendcmd('EPSV'), self.sock.getpeername())
return host, port
def ntransfercmd(self, cmd, rest=None):
size = None
if self.passiveserver:
host, port = self.makepasv()
conn = socket.create_connection((host, port), self.timeout,
source_address=self.source_address)
try:
if rest is not None:
self.sendcmd("REST %s" % rest)
resp = self.sendcmd(cmd)
if resp[0] == '2':
resp = self.getresp()
if resp[0] != '1':
raise error_reply(resp)
except:
conn.close()
raise
else:
with self.makeport() as sock:
if rest is not None:
self.sendcmd("REST %s" % rest)
resp = self.sendcmd(cmd)
if resp[0] == '2':
resp = self.getresp()
if resp[0] != '1':
raise error_reply(resp)
conn, __ = sock.accept()
if self.timeout is not _GLOBAL_DEFAULT_TIMEOUT:
conn.settimeout(self.timeout)
if resp[:3] == '150':
size = parse150(resp)
return conn, size
def transfercmd(self, cmd, rest=None):
return self.ntransfercmd(cmd, rest)[0]
def retrlines(self, cmd, callback=None):
if callback is None:
callback = print
resp = self.sendcmd('TYPE A')
# num_lines = sum(1 for l in open('ftp.py', 'r'))
with self.transfercmd(cmd) as conn, \
conn.makefile('r', encoding=self.encoding) as fp:
# import time
# for i in tqdm(range(num_lines + 1)):
while True:
# time.sleep(0.02)
line = fp.readline(self.maxline + 1)
if len(line) > self.maxline:
raise Error("got more than %d bytes" % self.maxline)
if not line:
break
if line[-2:] == CRLF:
line = line[:-2]
elif line[-1:] == '\n':
line = line[:-1]
callback(line);
# self.send_noop();
return self.voidresp()
def storlines(self, cmd, fp, callback=None):
self.voidcmd('TYPE A') # type ASCII (text)
with self.transfercmd(cmd) as conn:
while True:
buf = fp.readline(self.maxline + 1)
if not buf:
break
if buf[-2:] != B_CRLF:
if buf[-1] in B_CRLF: buf = buf[:-1]
buf = buf + B_CRLF
conn.sendall(buf)
if callback:
callback(buf)
return self.voidresp()
def retrbinary(self, cmd, callback, n_block, blocksize=8192, rest=None):
self.voidcmd('TYPE I') # type Image (binary)
with self.transfercmd(cmd, rest) as conn:
for i in tqdm(range(n_block)):
data = conn.recv(blocksize)
if not data:
break
callback(data)
if (i + 1) % 5 == 0:
self.send_noop()
return self.voidresp()
def storbinary(self, cmd, fp, n_block, blocksize=8192, callback=None, rest=None):
self.voidcmd('TYPE I')
with self.transfercmd(cmd, rest) as conn:
for i in tqdm(range(n_block + 1)):
buf = fp.read(blocksize)
if not buf:
break
conn.sendall(buf)
if callback:
callback(buf)
if (i + 1) % 5 == 0:
self.send_noop()
return self.voidresp()
def pwd(self):
'''Return current working directory
'''
resp = self.voidcmd('PWD')
return parse257(resp)
def dir(self, *args):
callback = None
if args[-1:] and type(args[-1]) != type(''):
args, callback = args[:-1], args[-1]
cmd = ' '.join(['LIST', *args])
self.retrlines(cmd, callback)
def nlst(self, *args):
'''Return a list of files in a given directory (default the current).'''
cmd = 'NLST'
for arg in args:
cmd = cmd + (' ' + arg)
files = []
self.retrlines(cmd, files.append)
return files
def mlsd(self, path="", facts=[]):
if facts:
self.sendcmd("OPTS MLST " + ";".join(facts) + ";")
if path:
cmd = "MLSD %s" % path
else:
cmd = "MLSD"
lines = []
self.retrlines(cmd, lines.append)
for line in lines:
facts_found, _, name = line.rstrip(CRLF).partition(' ')
entry = {}
for fact in facts_found[:-1].split(";"):
key, _, value = fact.partition("=")
entry[key.lower()] = value
yield (name, entry)
def size(self, filename):
self.voidcmd('TYPE I')
# The SIZE command is defined in RFC-3659
resp = self.sendcmd('SIZE ' + filename)
if resp[:3] == '213':
s = resp[3:].strip()
return int(s)
def cwd(self, dirname):
if dirname == '..':
try:
return self.voidcmd('CDUP')
except Error:
pass
elif dirname == '':
dirname = '.'
cmd = f'CWD {dirname}'
return self.voidcmd(cmd) # expect 2xx response
def rename(self, fromname, toname):
resp = self.sendcmd('RNFR ' + fromname)
if resp[0] != '3':
raise error_reply(resp)
return self.voidcmd('RNTO ' + toname)
def mkd(self, dirname):
resp = self.voidcmd('MKD ' + dirname)
if not resp.startswith('257'):
return ''
return parse257(resp)
def rmd(self, dirname):
return self.voidcmd('RMD ' + dirname)
def delete(self, filename):
resp = self.sendcmd('DELE ' + filename)
if resp[:3] in {'250', '200'}:
return resp
else:
raise error_reply(resp)
def quit(self):
resp = self.voidcmd('QUIT')
if self.file:
self.file.close()
self.sock.close()
self.file = None
self.sock = None
return resp
def format_size_(self, n_byte):
n_byte = int(n_byte)
KB, MB, GB = 2**10, 2**20, 2**30
if n_byte < KB:
return '{:>6} B '.format(n_byte)
elif n_byte < MB:
return '{:>6} KiB'.format(n_byte // KB)
elif n_byte < GB:
return '{:>6} MiB'.format(n_byte // MB)
else:
return '{:>6} GiB'.format(n_byte // GB)
def pretty_mlsd(self, path='', short=False):
file_list = self.mlsd(path=path, facts=["type", "size", "perm"])
for f in file_list:
if f[0].startswith('.'):
continue
if not short:
print("{:<12}\t\t".format(f[1].get('perm', '---------')), end='')
print(f"{self.format_size_(f[1].get('size', -1))}\t\t", end='')
if f[1].get('type', 'none') == 'dir':
print(f'{BOLD}{f[0]}{ENDC}')
else:
print(f'{f[0]}')
def parse150(resp):
if resp[:3] != '150':
raise error_reply(resp)
global _150_re
if _150_re is None:
import re
_150_re = re.compile(
r"150 .* \((\d+) bytes\)", re.IGNORECASE | re.ASCII)
m = _150_re.match(resp)
if not m:
return None
return int(m.group(1))
def parse227(resp):
if resp[:3] != '227':
raise error_reply(resp)
global _227_re
if _227_re is None:
import re
_227_re = re.compile(r'(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)', re.ASCII)
m = _227_re.search(resp)
if not m:
raise error_proto(resp)
numbers = m.groups()
host = '.'.join(numbers[:4])
port = (int(numbers[4]) << 8) + int(numbers[5])
return host, port
def parse229(resp, peer):
if resp[:3] != '229':
raise error_reply(resp)
left = resp.find('(')
if left < 0: raise error_proto(resp)
right = resp.find(')', left + 1)
if right < 0:
raise error_proto(resp)
if resp[left + 1] != resp[right - 1]:
raise error_proto(resp)
parts = resp[left + 1:right].split(resp[left+1])
if len(parts) != 5:
raise error_proto(resp)
host = peer[0]
port = int(parts[3])
return host, port
def parse257(resp):
if resp[:3] != '257':
raise error_reply(resp)
if resp[3:5] != ' "':
return '' # Not compliant to RFC 959, but UNIX ftpd does this
dirname = ''
i = 5
n = len(resp)
while i < n:
c = resp[i]
i = i+1
if c == '"':
if i >= n or resp[i] != '"':
break
i = i+1
dirname = dirname + c
return dirname
def print_warning(warning):
print(f'{BOLD}{WARNING}{ITALIC}[ERROR] {warning}{ENDC}')
def print_info(info, end=None):
if end is not None:
print(f'{BOLD}{ITALIC}[INFO] {info}{ENDC}', end=end)
else:
print(f'{BOLD}{ITALIC}[INFO] {info}{ENDC}')
def timeout_input(prompt, timeout=10):
try:
if prompt:
print(prompt)
i, __, __ = select.select([sys.stdin], [], [], timeout)
return sys.stdin.readline().strip() if i else None
except:
raise
finally:
print(ENDC, end='')
def test():
ftp = FTP('127.0.0.1','hatsu3','password')
resp = ftp.sendcmd("")
print(repr(resp))
ftp.quit()
if __name__ == '__main__':
# run_ftp_server.py
# - host = 127.0.0.1:8821
# - home = /Users/hatsu3
# - username = hatsu3
# - password = password
host = '127.0.0.1'
port = 8822
user = 'username'
passwd = 'password'
ftp_client = FTP()
ftp_client.connect(host=host, port=port)
# -> 220 pyftpdlib 1.5.4 ready.
ftp_client.login(user=user, passwd=passwd)
# -> 331 Username ok, send password.
# -> 30 Login successful.
# ftp_client.dir('.', print) # ftp_client.dir('./Library')
# # -> 200 Type set to: ASCII.
# # -> 227 Entering passive mode (127,0,0,1,204,237).
# # -> 125 Data connection already open. Transfer starting.
# # -> -rw-r--r-- 1 hatsu3 staff 267 Jul 18 2018 .489614.padl
# # -> -r-------- 1 hatsu3 staff 7 Oct 29 12:26 .CFUserTextEncoding
# # -> drwx------ 2 hatsu3 staff 64 Dec 26 16:19 .CMVolumes
# # -> <OMITTED> ...
while True:
# cmd = input(f'{UNDERLINE}{BOLD}FTP ➜ ').split()
print()
prompt = f'{UNDERLINE}{BOLD}FTP {OKBLUE}{host}:{port} {OKGREEN}{os.getcwd()} ⌁ {ftp_client.pwd()} ➜{ENDC}{UNDERLINE}{BOLD}'
cmd = timeout_input(prompt, timeout=20)
while cmd is None:
ftp_client.send_noop()
cmd = timeout_input('', timeout=20)
cmd = cmd.split()
if not cmd: # empty input
continue
cmd_type = cmd[0].lower()
cmd_args = cmd[1:]
if cmd_type == 'exit' or cmd_type == 'quit':
ftp_client.quit()
break
elif cmd_type == 'll':
try:
dirname = '.' if not cmd_args else cmd_args[0]
ftp_client.dir(dirname, print)
except error_perm:
print_warning(f'{dirname}: No such directory')
elif cmd_type == 'ls':
try:
dirname = '.' if not cmd_args else cmd_args[0]
ftp_client.pretty_mlsd(dirname, short=True)
except error_perm:
print_warning(f'{dirname}: No such directory')
elif cmd_type == 'lh':
try:
dirname = '.' if not cmd_args else cmd_args[0]
ftp_client.pretty_mlsd(dirname, short=False)
except error_perm:
print_warning(f'{dirname}: No such directory')
elif cmd_type == 'cd':
try:
dirname = '.' if not cmd_args else cmd_args[0]
print_info(f'Changing working directory to {dirname}')
ftp_client.cwd(dirname)
except error_perm:
print_warning(f'{dirname}: No such directory')
elif cmd_type == 'pwd':
print_info(f'Current working directory: {ftp_client.pwd()}')
elif cmd_type == 'download_text':
try:
if len(cmd_args) != 1:
print('download <FILENAME>')
continue
filename = cmd_args[0]
# local_path = Path(filename).name
ftp_client.retrlines(f'RETR {filename}', callback=print)
remote_path = Path(ftp_client.pwd()) / filename
print_info(f'Downloaded text file {remote_path}')
except error_perm:
print_warning(f'{filename}: No such directory')
elif cmd_type == 'store_text':
if len(cmd_args) != 1:
print('store <FILENAME>')
continue
filename = cmd_args[0]
file_path = Path(filename)
print_info(f'Checking {file_path}...')
if not file_path.is_file():
print_warning(f'No such file')
continue
fp = file_path.open(mode='rb')
ftp_client.storlines(f'STOR {filename}', fp=fp, callback=None)
remote_path = Path(ftp_client.pwd()) / Path(filename).name
print_info(f'{filename} saved at {remote_path}')
elif cmd_type == 'help':
print('''
- exit
- ls </./../DIRNAME>
- cd </./../DIRNAME>
- pwd
- download_text / download <FILENAME>
- store_text / store <FILENAME>
- help
- mkdir <DIRNAME>
- rmdir <DIRNAME>
- rename <FROM_NAME> <TO_NAME>
- sz <FILENAME>
- rm <FILENAME>
- ccd <PATH>
- cpwd
- cls
''')
elif cmd_type == 'download':
# download binary files
try:
if len(cmd_args) != 1:
print('download <FILENAME>')
continue
filename = cmd_args[0]
local_path = Path(filename).name
sz = ftp_client.size(filename)
blocksize = 8192
n_block = sz // blocksize + 1
with open(local_path, 'wb') as fp:
ftp_client.retrbinary(f'RETR {filename}', callback=(lambda x: fp.write(x)), \
blocksize=blocksize, n_block=n_block)
remote_path = Path(ftp_client.pwd()) / filename
print_info(f'Downloaded binary file {remote_path}')
except error_perm:
print_warning(f'{filename}: No such directory')
elif cmd_type == 'store':
# uploas binary files
if len(cmd_args) != 1:
print('store <FILENAME>')
continue
filename = cmd_args[0]
file_path = Path(filename)
print_info(f'Checking {file_path}...')
if not file_path.is_file():
print_warning(f'{filename}: No such file')
continue
sz = os.path.getsize(filename)
blocksize = 8192
n_block = sz // blocksize + 1
fp = file_path.open(mode='rb')
ftp_client.storbinary(f'STOR {filename}', fp=fp, callback=None, \
blocksize=blocksize, n_block=n_block)
remote_path = Path(ftp_client.pwd()) / Path(filename).name
print_info(f'{filename} saved at {remote_path}')
elif cmd_type == 'rm':
try:
if len(cmd_args) != 1:
print('rm <FILENAME>')
continue
filename = cmd_args[0]
ftp_client.delete(filename)
remote_path = Path(ftp_client.pwd()) / filename
print_info(f'Deleted {remote_path}')
except error_perm:
print_warning(f'{filename}: No such file')
elif cmd_type == 'mkdir':
if len(cmd_args) != 1:
print('mkdir <DIRNAME>')
continue
dirname = cmd_args[0]
ftp_client.mkd(dirname)
print_info(f'Created directory {dirname}')
elif cmd_type == 'rmdir':
try:
if len(cmd_args) != 1:
print('rmdir <DIRNAME>')
continue
dirname = cmd_args[0]
ftp_client.rmd(dirname)
print_info(f'Deleted directory {dirname}')
except error_perm:
print_warning(f'{dirname}: No such directory')
elif cmd_type == 'rename':
try:
if len(cmd_args) != 2:
print('rename <FROM_NAME> <TO_NAME>')
continue
from_name, to_name = cmd_args
ftp_client.rename(from_name, to_name)
print_info(f'Renamed {from_name} to {to_name}')
except error_perm:
print_warning(f'{from_name}: No such file or directory')
elif cmd_type == 'sz':
try:
if len(cmd_args) != 1:
print('sz <FILENAME>')
continue
filename = cmd_args[0]
sz = ftp_client.size(filename)
print_info(f'Size of {filename} is {sz} bytes')
except error_perm:
print_warning(f'{filename}: No such file or directory')
elif cmd_type == 'ccd':
try:
if len(cmd_args) != 1:
print('ccd <PATH>')
continue
new_path = cmd_args[0]
os.chdir(new_path)
print_info(f'[client] Working directory changed to {os.getcwd()}')
except FileNotFoundError:
print_warning(f'{new_path}: No such directory')
except:
print_warning(f'Failed to cd to {new_path}')
elif cmd_type == 'cpwd':
print(f'[client] Current working directory is {os.getcwd()}')
elif cmd_type == 'cls':
for f in os.listdir('.'):
if f.startswith('.'):
continue
sz = os.path.getsize(f)
print_info(ftp_client.format_size_(sz) + '\t\t', end='')
print(f if os.path.isfile(f) else f'{BOLD}{f}{ENDC}')
elif cmd_type == 'continue_download':
try:
if len(cmd_args) != 1:
print('download <FILENAME>')
continue
filename = cmd_args[0]
print_info(f'Checking {filename}...')
if not Path(filename).is_file():
print_warning(f'{filename}: No such file')
continue
local_sz = os.path.getsize(Path(filename).name)
remote_sz = ftp_client.size(filename)
remain_sz = remote_sz - local_sz
blocksize = 8192
n_block = remain_sz // blocksize + 1
if n_block == 0:
print_warning('Tranfer completed. Nothing to download')
continue
print_info(f'Continue downloading {filename}, remaining {ftp_client.format_size_(remain_sz)}')
local_path = Path(filename).name
remote_path = Path(ftp_client.pwd()) / filename
with open(local_path, 'ab') as fp:
ftp_client.retrbinary(f'RETR {filename}', callback=(lambda x: fp.write(x)), \
blocksize=blocksize, n_block=n_block, rest=local_sz)
print_info(f'Downloaded binary file {remote_path}')
except error_perm:
print_warning(f'{filename}: No such directory')
else:
print_warning('Invalid command. Try help')
# filename = input('Which file to retrieve: ')
# # <- Users/hatsu3/Documents/GitHub/ftpclient/ftp.py
# ftp_client.retrlines('RETR ' + filename, callback=print)
# # -> 200 Type set to: ASCII.
# # -> 227 Entering passive mode (127,0,0,1,243,131).
# # -> 125 Data connection already open. Transfer starting.
# # -> import socket
# # -> <OMITTED> ...
# # -> 226 Transfer complete.
# ftp_client.quit()
# # -> 21 Goodbye.