-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpy
executable file
·200 lines (186 loc) · 4.33 KB
/
ftpy
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
# FTPY - a simple FTP program
# Author: Muhammad-Sharif Moustafa
from ftplib import FTP
from ftplib import error_temp
from ftplib import error_perm
from getpass import getpass
from sys import exit
import sys
import socket
import os
# Global Variables
ftp = FTP()
host = ''
user = ''
password = ''
msg = ''
connected = False
loggedIn = False
file = None
# Convenience Functions
def connect(_host):
if (_host == 'quit' or _host == 'exit'):
exit(0)
if (_host != ''):
try:
msg = ftp.connect(_host)
except error_temp as e:
print(e)
except error_perm as e:
print(e)
except socket.gaierror as e:
print("Address error: " + e.strerror)
except socket.timeout:
print("Connection timed out.")
except socket.error as e:
print("Socket error: " + str(e))
else:
print(msg)
return True
return False
def login(_user, _password):
try:
msg = ftp.login(_user, _password)
except error_temp as e:
print(e)
except error_perm as e:
print(e)
else:
print(msg)
return True
return False
def writeFileLineByLine(line):
file.write(line)
file.write('\n')
def writeBinary(data):
file.write(data)
def getUserInput(msg):
inputString = ''
# Try the Python 2 way of getting input. If that fails
# use the Python 3 way.
try:
inputString = eval("raw_input('" + msg + "')")
except (SyntaxError, NameError):
inputString = eval("input('" + msg + "')")
return inputString
# Parse command line arguments (if any) and try to connect
# with credentials in command line
if len(sys.argv) == 2:
host = sys.argv[1]
connected = connect(host)
if len(sys.argv) == 3:
# user provided host and user
host = sys.argv[1]
user = sys.argv[2]
# try to connect
connected = connect(host)
if len(sys.argv) == 4:
# user provided host and user
host = sys.argv[1]
user = sys.argv[2]
password = sys.argv[3]
# try to connect
connected = connect(host)
# try to login
if connected:
loggedIn = login(user, password)
# Connection and Login Logic
while not connected:
host = getUserInput('Host name (URL): ').strip()
connected = connect(host)
while not loggedIn:
if user == '':
user = getUserInput('Username (blank for anonymous): ').strip()
if password == '':
password = getpass('Password (blank for anonymous): ').strip()
loggedIn = login(user, password)
# if login attempt was unsuccessful, clear username and password
if not loggedIn:
user = ''
password = ''
# Command Loop
while True:
cmd = getUserInput('Command: ').strip()
# split command by spaces
cmdList = cmd.split(None, 1)
# if no commands were given, skip the loop
if len(cmdList) == 0:
continue
# if there were more than one "words" in the command,
# the words after the first one are command arguments.
if len(cmdList) > 1:
cmdArgs = cmdList[1]
# parse the command
if cmdList[0] == 'quit' or cmdList[0] == 'exit':
break;
elif cmdList[0] == 'ls':
ftp.retrlines('LIST')
elif cmdList[0] == 'cd':
try:
msg = ftp.cwd(cmdArgs)
except error_perm as e:
print(e)
else:
print(msg)
elif cmdList[0] == 'get':
try:
file = open(cmdList[1], 'wb')
ftp.retrbinary(('RETR ' + cmdArgs), writeBinary)
file.close()
except IOError as e:
print('IOError: ' + str(e))
except error_perm as e:
print(e)
else:
print('Got ' + cmdArgs)
elif cmdList[0] == 'put':
filename = os.path.basename(cmdArgs)
try:
file = open(cmdArgs, 'rb')
ftp.storbinary('STOR ' + filename, file)
file.close()
except IOError as e:
print('IOError: ' + str(e))
except error_perm as e:
print(e)
else:
print('Put ' + filename)
elif cmdList[0] == 'mkdir':
try:
print(ftp.mkd(cmdArgs))
except error_perm as e:
print(e)
else:
print('Made new directory: ' + cmdArgs)
elif cmdList[0] == 'rmdir':
try:
print(ftp.rmd(cmdArgs))
except error_perm as e:
print(e)
else:
print('Removed directory: ' + cmdArgs)
elif cmdList[0] == 'pwd':
try:
print(ftp.pwd())
except error_perm as e:
print(e)
elif cmdList[0] == 'mv':
cmdArgsList = cmdArgs.split()
try:
print(ftp.rename(cmdArgsList[0], cmdArgsList[1]))
except error_perm as e:
print(e)
else:
print('Moved ' + cmdArgsList[0] + ' to ' + cmdArgsList[1])
elif cmdList[0] == 'rm':
try:
print(ftp.delete(cmdArgs))
except error_perm as e:
print(e)
else:
print('Deleted ' + cmdArgs)
# Try quitting nicely. If that fails, force quit.
try:
print(ftp.quit())
except:
print(ftp.close())