forked from jeffxu/git-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgftpwrapper.py
66 lines (56 loc) · 1.69 KB
/
gftpwrapper.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
#!/usr/bin/env python
from ftplib import FTP, error_perm, error_temp
class GftpWrapper(object):
def __init__(self, host, account, password, port=21):
"""init ftp and login"""
try:
self.__ftp = FTP()
self.__ftp.connect(host, port)
self.__ftp.login(account, password)
self.loginSuccess = True
except error_perm:
self.loginSuccess = False
def mkdir(self, path):
"""Make dir recusivly"""
prevPath = ''
path = path.split('/')
for p in path:
if not prevPath:
prevPath = p
else:
prevPath += '/' + p
if not self.isPathExists(prevPath):
try:
self.__ftp.mkd(prevPath)
except error_temp:
return False
return True
def cd(self, path):
"""Change the current dir to the given path"""
try:
self.__ftp.cwd(path)
return True
except error_perm:
return False
def rm(self, path):
"""Rmoeve remote file."""
try:
self.__ftp.delete(path)
return True
except error_perm:
return False
def upload(self, localPath, remotePath):
try:
self.__ftp.storbinary('STOR ' + remotePath, open(localPath, 'rb'))
return True
except IOError:
return False
def isPathExists(self, path):
"""Check for given path whether exists."""
try:
self.__ftp.nlst(path)
return True
except error_temp:
return False
def close(self):
self.__ftp.close()