class SFTP(paramiko.Transport):
def __init__(self, host, port=22):
self.sftp = ""
super(SFTP, self).__init__(host, port)
def connect(self, username, password):
super(SFTP, self).connect(username=username, password=password)
self.sftp = paramiko.SFTPClient.from_transport(self)
def put(self, local_path, remote_path):
self.sftp.put(local_path, remote_path)
def get(self, remote_path, local_path):
self.sftp.get(remote_path, local_path)
def is_file_exsit(self, remote_dir, file_name):
file_list = [i.filename for i in self.sftp.listdir_attr(remote_dir)]
if file_name in file_list:
return True
else:
return False
def listdir(self, remote_dir, ret_list):
file_list = self.sftp.listdir_attr(remote_dir)
for i in file_list:
data = {"name": i.filename, "children": [], "id": i.filename}
if stat.S_ISDIR(i.st_mode):
# data["iconOpen"] = "/static/img/folder_closed.png"
# data["iconClose"] = "/static/img/folder_open.png"
data["is_dir"] = True
ret_list.append(data)
child_remote_dir = remote_dir + "/" + i.filename
self.listdir(child_remote_dir, data["children"])
else:
data["icon"] = SITE_URL + "static/img/file.png"
data["is_dir"] = False
ret_list.append(data)
return ret_list
def close(self):
self.sftp.close()
super(SFTP, self).close()