This repository was archived by the owner on Aug 15, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathFTP-Client.py
More file actions
463 lines (367 loc) · 11.2 KB
/
Copy pathFTP-Client.py
File metadata and controls
463 lines (367 loc) · 11.2 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
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
#65010801 ภวัต ประกายทิพย์
import socket
import time
from getpass import getpass
cmd_sock = None
host = None
NOT_NEED_CONNECTION = (
"bye",
"open",
"quit"
)
NEED_CONNECTION = (
"ascii",
"binary",
"cd",
"close",
"delete",
"disconnect",
"get",
"ls",
"put",
"pwd",
"rename",
"user"
)
def ftp_send_cmd(socket: socket, str: str): #format string and send to ftp server
socket.sendall(f"{str}\r\n".encode())
return
def get_resp(socket: socket) -> str:
#TODO: return status code
return socket.recv(1024).decode().strip()
def close_cmd_sock():
global cmd_sock
try:
cmd_sock.close()
cmd_sock = None
except:
cmd_sock = None
def ftp_open_data_conn() -> socket:
global host
#bind listing socket
data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
data_sock.bind((host, 0))
data_sock.listen()
host_part = host.replace('.', ',')
port = data_sock.getsockname()[1] #get port number
port = f"{port:016b}" #convert port number to bianry
port_1 = int(port[0:8], 2) #first part of the port
port_2 = int(port[8:16], 2) #second part of the port
ftp_send_cmd(cmd_sock, f"PORT {host_part},{port_1},{port_2}")
print(get_resp(cmd_sock))
#TODO: add exception if fail
return data_sock
def measure(text: str):
def inner(func):
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
if elapsed_time == 0: #prevent very fast transfer(start_time = end_time) from causeing devide by 0
elapsed_time = 0.0001
speed = result[0] / (elapsed_time * 1000) #result[0] = size of data
status = f"ftp: {result[0]} bytes {text} in {elapsed_time:.3f}Seconds {speed:.2f}Kbytes/sec."
return status, result
return wrapper
return inner
@measure("received")
def recv_data(data_sock: socket):
data_conn, data_addr = data_sock.accept()
size = 0
data = ""
while True:
data_part = data_conn.recv(1024)
if data_part:
data += data_part.decode()
else:
break
size = len(data)
return size + 3, data #Plus 3 because of IDK
@measure("sent")
def send_data(data_sock, file):
data_conn, data_addr = data_sock.accept()
size = 0
while True:
bytes_read = file.read(1024)
if not bytes_read:
break
size += len(bytes_read)
data_conn.sendall(bytes_read)
return (size + 3,) #Plus 3 because of IDK
def ascii():
ftp_send_cmd(cmd_sock, "TYPE A")
print(get_resp(cmd_sock))
return
def binary():
ftp_send_cmd(cmd_sock, "TYPE I")
print(get_resp(cmd_sock))
return
def bye():
try:
ftp_send_cmd(cmd_sock, "QUIT")
print(get_resp(cmd_sock))
close_cmd_sock()
except:
pass
exit()
def cd(remote_dir: str, *argv):
ftp_send_cmd(cmd_sock, f"CWD {remote_dir}")
print(get_resp(cmd_sock))
return
def close():
try:
ftp_send_cmd(cmd_sock, "QUIT")
except:
print("Not connected.")
return
print(get_resp(cmd_sock))
close_cmd_sock()
def delete(remote_file: str = "", *argv):
if not remote_file:
remote_file = input("Remote file ")
if not remote_file:
print("delete remote file.")
return
ftp_send_cmd(cmd_sock, f"DELE {remote_file}")
print(get_resp(cmd_sock))
def get(remote_file: str = "", local_file: str = "", *argv):
if not remote_file:
remote_file = input("Remote file ")
if not remote_file:
print("Remote file get [ local-file ].")
return
local_file = input("Local file ")
if not local_file:
local_file = remote_file
data_sock = ftp_open_data_conn()
ftp_send_cmd(cmd_sock, f"RETR {remote_file}")
resp = get_resp(cmd_sock) #Note: double \r\n in "550 This function is not supported on this system.\r\n\r\n"
print(resp)
if resp.startswith("550"):
return
status, result = recv_data(data_sock)
file = open(local_file, 'w')
file.write(result[1])
file.close()
print(get_resp(cmd_sock))
if result[0] == 3: #if file is empty
return
print(status)
def ls(remote_dir: str = "", *argv) -> None:
data_sock = ftp_open_data_conn()
ftp_send_cmd(cmd_sock, f"NLST {remote_dir}")
resp = get_resp(cmd_sock)
resp_code = resp.split()[0]
print(resp, end='')
if resp_code == "550": #Couldn't open the file or directory
return
elif resp_code != "150":
print("Unexpected error, ls command")
return
status, result = recv_data(data_sock)
print(result[1], end="")
print(get_resp(cmd_sock)) #226 Operation successful
#TODO: add exception
print(status)
return
def ftp_open(host_local: str = None, port: str = "21", *argv):
global cmd_sock
global host
if argv:
print("Usage: open host name [port]")
return
if cmd_sock: #check if already connect
print(f"Already connected to {host}, use disconnect first.")
return
cmd_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if not host_local:
host_local = input("To ")
if not host_local:
print("Usage: open host name [port]")
return
#set recieved host from funciton to global var for future use
host = host_local
#attempt establish connection
try:
cmd_sock.connect((host_local, int(port)))
except ConnectionRefusedError:
print("> ftp: connect :Connection refused")
close_cmd_sock()
return
except socket.timeout:
print("> ftp: connect :Connection timed out")
close_cmd_sock()
return
except socket.gaierror:
print(f"Unknown host {host_local}.")
close_cmd_sock()
return
except Exception as e:
print(type(e).__name__, e.args)
return
print(get_resp(cmd_sock))
#login
user = input(f"User ({host_local}:(none)): ")
ftp_send_cmd(cmd_sock, f"USER {user}")
resp = get_resp(cmd_sock)#respond for username input
resp_code = resp.split()[0]
print(resp)
if resp_code == "501": #missing user argument
print("Login failed.")
elif resp_code == "331": #get password
password = getpass("Password: ")
print()
ftp_send_cmd(cmd_sock, f"PASS {password}")
resp = get_resp(cmd_sock)
resp_code = resp.split()[0]
if resp_code == "530": #530 Login incorrect.
print(resp)
print("Login failed")
elif resp_code == "230": #Login successful
print(resp)
else:
print("unexpected error, password")
else:
print("unexpected error, user")
def put(local_file: str = "", remote_file: str = "", *argv):
if not local_file:
local_file = input("Local file ")
if not local_file:
print("Local file put: remote file.")
return
remote_file = input("Remote file ")
if not remote_file:
remote_file = local_file
try:
file = open(local_file, "rb")
except FileNotFoundError:
print(f"{local_file}: File not found")
return
except PermissionError:
print(f"Error opening local file {local_file}.")
return
data_sock = ftp_open_data_conn()
ftp_send_cmd(cmd_sock, f"STOR {remote_file}")
print(get_resp(cmd_sock))
status, result = send_data(data_sock, file)
file.close() #TODO: user 'with' statement
data_sock.close() #TODO: user 'with' statement
print(get_resp(cmd_sock))
if result[0] == 3: #if file is empty
return
print(status)
def pwd():
ftp_send_cmd(cmd_sock, f"XPWD")
print(get_resp(cmd_sock))
return
def rename(from_name: str = "", to_name: str = "", *argv):
if not from_name: #no argument specify
from_name = input("From name ")
if not to_name: #only one argument specify
to_name = input("To name ")
ftp_send_cmd(cmd_sock, f"RNFR {from_name}")
resp = get_resp(cmd_sock)
if resp.startswith("350"): #directory exists
print(resp)
ftp_send_cmd(cmd_sock, f"RNTO {to_name}")
print(get_resp(cmd_sock))
else:
print("Unexpected error, rename command")
return
def user(user: str = "", password: str ="", *argv):
if not user:
user = input("Username: ")
if not user:
print("Usage: user username [password] [account]")
return
ftp_send_cmd(cmd_sock, f"USER {user}")
print(get_resp(cmd_sock))
if not password:
password = getpass("Password: ")
ftp_send_cmd(cmd_sock, f"PASS {password}")
resp = get_resp(cmd_sock)
resp_code = resp.split()[0]
if resp_code == "530": #530 Login incorrect.
print(resp)
print("Login failed")
elif resp_code == "230": #Login successful
print(resp)
else:
print("unexpected error, password")
while True:
input_str = input("ftp> ")
#input sanitization
input_list = input_str.split()
if not input_list: #check for empty command
continue
command = input_list[0]
arg = input_list[1:]
if command in NOT_NEED_CONNECTION:
match command:
case "bye":
bye()
case "open":
ftp_open(*arg)
case "quit":
bye()
elif command in NEED_CONNECTION:
if not cmd_sock:
print("Not connected.")
continue
match command:
case "ascii":
ascii()
case "binary":
binary()
case "cd":
cd(*arg)
case "close":
close()
case "delete":
delete(*arg)
case "disconnect": # disconnect = close
close()
case "get":
get(*arg)
case "ls":
ls(*arg)
case "put":
put(*arg)
case "pwd":
pwd()
case "rename":
rename(*arg)
case "user":
user(*arg)
else:
print("Invalid command.")
#TODO: Command
#[x] ascii
#[x] binary
#[x] bye
#[x] cd
#[x] close
#[x] delete
#[x] disconnect
#[x] get
#[x] ls
#[x] open
#[x] put
#[x] pwd
#[x] quit
#[x] rename
#[x] user
#TODO: features
#[x] count transfered data
#[x] speed
#[x] fix inaccurate speed
#[x] fix receive data function
#[x] fix ls function after fix the above
#[x] change recv_data func to work with file too
#[x] make send_data func.
#[x] fix add another print respond for 150 starting data transfer
#[x] add new line when enter password
#[x] get condition for various combination of input
#[x] get condition for various combination of input
#[x] exception for file not found in put command