废话就不说了,这里就给出我封装的 ftp 工具类,有兴趣的自己看一下吧–_--
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ftplib import FTP
from oslo_log import log
import hashlib
import os
import time
LOG = log.getLogger(__name__)
# 定义时间
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 ftp(object):
def __init__(self, ip, port, user, password):
self.ip = ip
self.port = port
self.user = user
self.password = password
# FTP登录模块
def ftp_login(self):
ftp = FTP()
try:
ftp.connect(self.ip, self.port)
except Exception:
LOG.error("FTP connect failed!!!")
try:
ftp.login(self.user, self.password)
except Exception:
LOG.error("FTP connect failed!!!")
else:
return ftp
# FTP下载
def ftp_download(self, remote_path, local_path):
ftp = FTP()
try:
ftp.connect(self.ip, self.port)
except Exception:
LOG.error("FTP connect failed!!!")
try:
ftp.login(self.user, self.password)
except Exception:
LOG.error("FTP login failed!!!")
else:
try:
file_list = ftp.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')
ftp.retrbinary('RETR %s' %
(file), local_file.write, bufsize)
ftp.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))
# FTP下载
def ftp_upload(self, remote_path, local_path):
ftp = FTP()
try:
ftp.connect(self.ip, self.port)
except Exception:
LOG.error("FTP connect failed!!!")
try:
ftp.login(self.user, self.password)
except Exception:
LOG.error("FTP login failed!!!")
else:
try:
ftp.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')
ftp.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])
# FTP文件校验
def check(self, remote_path, local_path):
# 将FTP临时文件下载到本地临时目录
tmp_path = '/tmp' + remote_path
ftp = FTP()
try:
ftp.connect(self.ip, self.port)
except Exception:
LOG.error("FTP connect failed!!!")
try:
ftp.login(self.user, self.password)
except Exception:
LOG.error("FTP login failed!!!")
else:
try:
file_list = ftp.nlst(remote_path)
except Exception:
LOG.error("remote_path error!!!")
else:
key = os.path.exists(tmp_path)
if str(key) == 'True':
pass
else:
os.makedirs(tmp_path)
try:
for file in file_list:
bufsize = 1024
file_name = file.split('/')[-1]
local_file = open(tmp_path + file_name, 'wb')
ftp.retrbinary('RETR %s' %
(file), local_file.write, bufsize)
ftp.set_debuglevel(0)
local_file.close()
except Exception:
LOG.error("cloudn't check!!!
it may be cause by your wrong FTP info!!!")
# 校验FTP临时文件的md5值
tmp_file_list = os.walk(tmp_path)
tmp_file_md5s = {}
for root, dirs, files in tmp_file_list:
for tmp_file in files:
tmp_file_path = tmp_path + tmp_file
f_tmp = open(tmp_file_path, 'r')
fcont = f_tmp.read()
f_tmp.close()
fmd5 = hashlib.md5(fcont)
tmp_file_md5s[tmp_file] = fmd5.hexdigest()
# 校验本地文件的md5值
local_file_list = os.walk(local_path)
local_file_md5s = {}
for root, dirs, files in local_file_list:
for local_file in files:
local_file_path = local_path + local_file
f_local = open(local_file_path, 'r')
fcont = f_local.read()
f_local.close()
fmd5 = hashlib.md5(fcont)
local_file_md5s[local_file] = fmd5.hexdigest()
# 开始校验文件md5值是否相同
for k, v in tmp_file_md5s.items():
try:
if tmp_file_md5s[k] == local_file_md5s[k]:
pass
else:
ftp = FTP()
ftp.connect(self.ip, self.port)
ftp.login(self.user, self.password)
bufsize = 1024
local_file = open(local_path + k, 'wb')
ftp.retrbinary('RETR %s' % (
remote_path + k), local_file.write, bufsize)
ftp.set_debuglevel(0)
local_file.close()
LOG.info("downloading %s !!!" % (remote_path + k))
except Exception:
bufsize = 1024
ftp = FTP()
ftp.connect(self.ip, self.port)
ftp.login(self.user, self.password)
local_file = open(local_path + k, 'wb')
ftp.retrbinary('RETR %s' % (remote_path + k),
local_file.write, bufsize)
ftp.set_debuglevel(0)
local_file.close()
LOG.info("downloading %s !!!" % (remote_path + k))
else:
LOG.info("%s %s check successfully!!!" %
(current_time, remote_path))
然后使用代码在这里
from tel.api.ftp_utils import ftp as FTP
# Download zip_file from FTP
ip = CONF.upf.ip
port = CONF.upf.port
user = CONF.upf.user
password = CONF.upf.password
ftp = FTP(ip, 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,部分功能解释如下
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文件



