废话就不说了,这里就给出我封装的 ftp 工具类,有兴趣的自己看一下吧–_--
from ftplib import FTP
from oslo_log import log
import os
import time
LOG = log.getLogger(__name__)
# define time
sys_time = time.time()
sys_time_array = time.localtime(sys_time)
current_time = time.strftime("%Y-%m-%d %H:%M:%S:", sys_time_array)
class FTPUtils(FTP):
def __init__(self, host, port, user, passwd):
self.port = port
super(FTPUtils, self).__init__(host=host,
user=user,
passwd=passwd)
def search_dir(self, ftp, start_dir, ftp_dir_l):
dir_res = []
ftp.cwd(start_dir)
ftp_dir_l.append(ftp.pwd())
ftp.dir('.', dir_res.append)
for i in dir_res:
if i.startswith("d"):
self.search_dir(ftp, ftp.pwd() + "/" +
i.split(" ")[-1], ftp_dir_l)
ftp.cwd('..')
return ftp_dir_l
def search_file(self, dir_path):
ftp_dir_l = []
ftp_file_l = []
ftp_dir_l = self.search_dir(self, dir_path, ftp_dir_l)
for server_f_l in ftp_dir_l:
file_list = self.nlst(server_f_l)
for server_file in file_list:
if server_file not in ftp_dir_l:
ftp_file_l.append(server_file)
return ftp_file_l
def ftp_download(self, remote_path, local_path):
try:
file_list = self.nlst(remote_path)
except Exception:
LOG.error("remote_path error.")
else:
key = os.path.exists(local_path)
if str(key) == 'True':
pass
else:
os.makedirs(local_path)
try:
for file in file_list:
bufsize = 1024
file_name = file.split('/')[-1]
local_file = open(local_path + file_name, 'wb')
self.retrbinary('RETR %s' %
(file), local_file.write, bufsize)
self.set_debuglevel(0)
local_file.close()
except Exception:
LOG.error("%s %s download failed." %
(current_time, remote_path))
else:
LOG.error("%s %s download successfully." %
(current_time, remote_path))
def ftp_upload(self, remote_path, local_path):
try:
self.mkd(remote_path)
except Exception:
pass
try:
file_list = os.walk(local_path)
for root, dirs, files in file_list:
for file in files:
local_file = local_path + file
remote_file = remote_path + file
bufsize = 1024
fp = open(local_file, 'rb')
self.storbinary('STOR ' + remote_file, fp, bufsize)
fp.close()
except Exception:
LOG.error("%s %s upload failed." %
(current_time, local_path))
else:
LOG.error("%s %s upload successfully." %
(current_time, local_path))
def remove_local_file(self, local_file_path):
if os.path.exists(local_file_path):
os.remove(local_file_path)
else:
LOG.error('no such file:%s' % local_file_path.split('/')[-1])
def _dir_list_name(self):
paths = self.nlst(self.pwd())
return [path[path.rfind('/') + 1:] for path in paths]
def _dir_list_path(self):
lines = self.nlst(self.pwd())
return lines
然后使用代码在这里
from tel.api.ftp_utils import ftp as FTP
# Download zip_file from FTP
host = CONF.upf.host
port = CONF.upf.port
user = CONF.upf.user
password = CONF.upf.password
ftp = FTP(host, port, user, password)
remote_path = "raypick/5GC_GD_HUAWEI.zip"
local_path = CONF.upf.local_path
tmp_file_path = local_path + remote_path.split('/')[-1]
ftp.ftp_download(remote_path, local_path)
ftp.quit()
关于 ftp,部分功能解释如下
ftp登陆连接
from ftplib import FTP #加载ftp模块
ftp=FTP() #设置变量
ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.connect("IP","port") #连接的ftp sever和端口
ftp.login("user","password") #连接的用户名,密码
print ftp.getwelcome() #打印出欢迎信息
ftp.cmd("xxx/xxx") #进入远程目录
bufsize=1024 #设置的缓冲区大小
filename="filename.txt" #需要下载的文件
file_handle=open(filename,"wb").write #以写模式在本地打开文件
ftp.retrbinaly("RETR filename.txt",file_handle,bufsize) #接收服务器上文件并写入本地文件
ftp.set_debuglevel(0) #关闭调试模式
ftp.quit() #退出ftp
ftp相关命令操作
ftp.cwd(pathname) #设置FTP当前操作的路径
ftp.dir() #显示目录下所有目录信息
ftp.nlst() #获取目录下的文件
ftp.mkd(pathname) #新建远程目录
ftp.pwd() #返回当前所在位置
ftp.rmd(dirname) #删除远程目录
ftp.delete(filename) #删除远程文件
ftp.rename(fromname, toname)#将fromname修改名称为toname。
ftp.storbinaly("STOR filename.txt",file_handel,bufsize) #上传目标文件
ftp.retrbinary("RETR filename.txt",file_handel,bufsize) #下载FTP文件