-
Notifications
You must be signed in to change notification settings - Fork 5
/
filetransfer_abc.py
275 lines (209 loc) · 7.75 KB
/
filetransfer_abc.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
from abc import ABCMeta, abstractmethod
import socket
from datetime import datetime as dt
from datetime import timedelta as td
from ftplib import FTP_TLS, FTP, error_reply, error_perm
class filetransfer_abc:
__metaclass__ = ABCMeta
@abstractmethod
def do_something(self): pass
@abstractmethod
def connect(self): pass
class ftp_si(filetransfer_abc):
username = ''
passwd = ''
ftp = None
def do_mystuff(self):
print "Do my stuff!"
def do_something(self):
print "Do something!"
def __init__(self, host, useSSL):
self.ftp = None
self.useSSL = useSSL
self.host = host
useSSL = False
def connect(self, the_user, the_passwd):
self.username = the_user
self.passwd = the_passwd
ok=True
msg = ''
if self.useSSL is True:
self.ftp = FTP_TLS(self.host)
else:
self.ftp=FTP(self.host)
try:
loginResponse = self.ftp.login(self.username, self.passwd)
msg = 'Login ok'
except socket.gaierror:
self.ftp = None
ok = False
msg = 'Server address could not be found.'
except (error_perm, error_reply):
info = traceback.format_exception(*sys.exc_info())
for i in info: sys.stderr.write(i)
ok = False
msg = error_msg
else:
if '230' in loginResponse:
ok = True
else:
ok = False
msg = error_msg
return msg
def setdir(self, path):
self.ftp.cwd(path)
@property
def currentdir(self):
"""Returns the current working directory at the server"""
return self.ftp.pwd()
def listfiles_basic(self, path):
return self.ftp.nlst(path)
def listfiles_ftpdetails(self, path, callback_function):
try:
self.ftp.retrlines('LIST %s' % path, callback_function)
return
except:
print 'Exception in ServerWatcher.getDirs'
info = traceback.format_exception(*sys.exc_info())
for i in info: sys.stderr.write(i)
return []
def deleteFile(self, filename):
"""
Deletes the file `filename` to the server
:param filename: Absolute or relative path to the file
"""
try:
# print 'Deleting %s' % filename
self.ftp.delete(filename)
return True
except (error_reply, error_perm):
# print 'Error deleting %s' % filename
return False
def fileExists(self, filepath):
try:
self.ftp.sendcmd('SIZE %s' % filepath)
except (error_reply, error_perm):
exists = False
else:
exists = True
def fileSize (self, filename):
try:
download_size = int(self.ftp.sendcmd('SIZE %s' % filename).split(' ')[-1])
except (IOError, OSError):
download_size = -1
# self.ioError.emit(localpath)
except (error_reply, error_perm) as ftperr:
# print 'Error downloading %s, %s' % (filename, ftperr)
download_size = -1
return download_size
def downloadFile(filename, callback_function):
downloaded = True
try:
self.ftp.retrbinary('RETR %s' % filename, handleChunk)
except (IOError, OSError):
downloaded = False
# self.ioError.emit(localpath)
except (error_reply, error_perm) as ftperr:
# print 'Error downloading %s, %s' % (filename, ftperr)
downloaded = False
return downloaded
def uploadFile (self, filename, callback_function):
try:
# Uploads file and updates its modified date in the server
# to match the date in the local filesystem.
self.upload_progress = 0
self.upload_size = os.path.getsize(localpath)
# self.fileEvent.emit(localpath)
self.ftp.storbinary('STOR %s' % filename,
open(localpath, 'rb'),
1024,
handle)
# print 'Upload finished'
with File.fromPath(filename) as uploaded:
modified = uploaded.localmdate
uploaded.servermdate = modified
self.setLastModified(filename, modified)
uploaded = True
except (IOError, OSError):
uploaded = False
self.ioError.emit(localpath)
except (error_reply, error_perm, OSError) as err:
print 'Error uploading %s, %s' % (filename, err)
uploaded = False
def lastModified(self, filename):
"""
Uses the MDTM FTP command to find the last modified timestamp
of the file `filename`.
Returns a `datetime.datetime` object in UTC representing the file's
last modified date and time.
:param filename: Relative or absolute path to the file
"""
timestamp = self.ftp.sendcmd('MDTM %s' % filename)
if '213 ' not in timestamp:
# Second chance was found to be needed in some cases.
timestamp = self.ftp.sendcmd('MDTM %s' % filename)
timestamp = timestamp.split(' ')[-1]
dateformat = '%Y%m%d%H%M%S.%f' if '.' in timestamp else '%Y%m%d%H%M%S'
try:
mtime = dt.strptime(timestamp, dateformat)
except ValueError:
mtime = dt.utcnow()
return mtime
def setLastModified(self, serverpath, newtime):
"""
Uses the MFMT or MDTM FTP commands to set `newtime` as the modified timestamp of the
file `serverpath` on the server.
:param serverpath: Relative or absolute path to the file
:param newtime: datedatime object holding the required time
"""
cmds = ['MFMT', 'MDTM']
for cmd in cmds:
try:
self.ftp.sendcmd(
'%s %s %s' % (cmd, newtime.strftime('%Y%m%d%H%M%S'), serverpath))
return
except (error_perm, error_reply) as e:
if cmd == cmds[len(cmds) - 1]:
# If is the last comand, re-raise the exception, else
# keep trying.
raise e
else:
continue
def mkpath(self, path):
"""
Creates the path `path` on the server by recursively
created folders, if needed.
:param path: Absolute path on the server to be created
"""
try:
self.ftp.cwd(path)
except error_perm:
# `cwd` call failed. Need to create some folders
make_dir = '/'
steps = path.split('/')
for step in steps:
if len(step) == 0:
continue
make_dir += '%s/' % step
try:
self.ftp.mkd(make_dir)
except error_perm:
# Probably already exists
continue
else:
# `cwd` call succeed. No need to create
# any folders
self.ftp.cwd('/')
return
if __name__ == '__main__':
print "hello"
c = ftp_si('ftp.iqstorage.com', True)
c.connect('testuser', 'test')
a = list
a = c.listfiles_basic('/')
print a
#for s in a:
# print s
print "Done"
#c.do_something()
#c.do_mystuff()