-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp-challenge.py
139 lines (128 loc) · 4.51 KB
/
ftp-challenge.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
from ftplib import FTP
import zipfile
import os.path, os
def helpMenu():
print("FTP Client 0.1.1")
print("Available commands:")
print("LIST\t\t:Directory listing on the current directory")
print("DOWNLOAD\t:Download a file from server")
print("UPLOAD\t\t:Upload a file to server")
print("CREATEDIR\t:Make a directory on the current directory")
print("DELETEDIR\t:Delete a file from the server")
print("UPTRACT\t\t:Extract a zip and upload it to the server")
print("PWD\t\t:See current directory")
def placeFiles(ftp, path):
for name in os.listdir(path):
localpath = os.path.join(path, name)
if os.path.isfile(localpath):
print("Uploading: ", name, localpath)
ftp.storbinary('STOR ' + name, open(localpath,'rb'))
elif os.path.isdir(localpath):
print("Making directory:", name)
try:
ftp.mkd(name)
except error_perm as e:
if not e.args[0].startswith('550'):
raise
print("Change directory:", name)
ftp.cwd(name)
placeFiles(ftp, localpath)
print("Change directory:", "..")
ftp.cwd("..")
print("FTP CLI 0.1.1")
x = input('IP: ')
y = input('Username: ')
z = input('Password: ')
f = FTP(x)
print("Welcome Message:\n"), f.getwelcome()
f.login(y, z)
print ("Current working directory: " + f.pwd())
print("Type HELP to see available commands")
while True:
command = input('>> ')
if command == "LIST":
a = f.mlsd()
print('List of directory: ')
for i in a:
dire = "[" + i[1]['type'] +"]\t" + i[0] + "\t"
print(dire)
elif command == "DOWNLOAD":
try:
print('Enter file name (Press Ctrl+C or KeyboardInterrupt to cancel): ')
b = input()
fd = open(b, 'wb')
f.retrbinary('RETR ' + b, fd.write)
print ("Download "+ b +" success.")
fd.close()
except KeyboardInterrupt:
print("Cancelled!")
continue
elif command == "UPLOAD":
try:
print('Enter file name (Press Ctrl+C or KeyboardInterrupt to cancel): ')
c = input()
fd = open(c, 'rb')
f.storbinary('STOR ' + c, fd)
print ("Upload " + c + " success.")
fd.close()
except KeyboardInterrupt:
print("Cancelled!")
continue
elif command == "CREATEDIR":
try:
print('Enter directory name (Press Ctrl+C or KeyboardInterrupt to cancel): ')
d = input()
f.mkd(d)
print ("Create "+ d + " directory success.")
except KeyboardInterrupt:
print("Cancelled!")
continue
elif command == "RENAME":
try:
e = input('Select file to rename (Press Ctrl+C or KeyboardInterrupt to cancel): ')
g = input('Rename to (Press Ctrl+C or KeyboardInterrupt to cancel): ')
f.rename(e, g)
print ("Renaming from " + e + " to " + g +" success.")
except KeyboardInterrupt:
print("Cancelled!")
continue
elif command == "CHANGEDIR":
h = input('Select directory: ')
f.cwd(h)
print ("Current working directory:", f.pwd())
elif command == "DELETEDIR":
try:
print("Select directory to delete (Press Ctrl+C or KeyboardInterrupt to cancel): ")
k = input()
f.rmd(k)
except KeyboardInterrupt:
print("Cancelled!")
continue
print ("Delete success.")
elif command == "UPTRACT":
try:
print('Select file to upload & extract (Press Ctrl+C or KeyboardInterrupt to cancel): ')
j = input()
if zipfile.is_zipfile(j):
zip_ref = zipfile.ZipFile(j, 'r')
os.mkdir(j[:-4])
try:
zip_ref.extractall(j[:-4])
except:
print ("Extract failed.")
else:
print ("Extract success.")
try:
placeFiles(f, j[:-4])
except:
print ("Upload " + j + " failed.")
else:
print ("Upload " + j + " success.")
except KeyboardInterrupt:
print("Cancelled!")
continue
elif command == "PWD":
print ("Current working directory:", f.pwd())
elif command == "HELP":
helpMenu()
f.quit()