python下操作ftp上传

时间:2022-11-26 08:00:35

生产情况:tomcat下业务log备份,目录分多级,然后对应目录格式放到ftp上;所以,结构上 我就是一级一级目录进行判断(因为我没有找到在ftp一次判断其子目录是否存在),还有一个low点就是我没有找到怎样一次性的调用ftp的login因为现在每次判断都需要登录一下,最终功能是实现了;想着先贴出来

#!/usr/local/bin/python3.5
###Description: 上传业务log到NFS199
###Author: Danny.Deng
###DateTime: 2016-11-25
import os,sys,shutil,time,datetime,re,socket,subprocess,ftplib
##########################
backup_dir = "/dockerlogs/"
###############ip地址依赖hosts文件中的hostname解析
ip_addr = socket.gethostbyname(socket.gethostname())
ports = sorted(os.listdir(backup_dir))
yesterday = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y%m%d")
thirday = (datetime.datetime.now() - datetime.timedelta(days=2)).strftime("%Y%m%d")
yearday = (datetime.datetime.now() - datetime.timedelta(days=365)).strftime("%Y%m%d")
###############取log文件
pattern = re.compile(r'' + thirday + '*.log$' )
year_pattern = re.compile(r'' + yearday + '*.log$' )
#pattern = re.compile(r'' + thirday + '*.log' )
###############取服务端口列表目录
for port in (ports):
port_dir = backup_dir + port
ftp_dir = ip_addr + "/" + port
##############判断ftp上是否有对应目录,没有则创建,此次判断是两个目录 如:192.168.20.130/8000 这两级是否存在
ftp = ftplib.FTP("192.168.xxxxx")
ftp.login("syxxx","xxxx",10)
try:
ftp.mkd(ip_addr)
ftp.cwd(ip_addr)
try:
ftp.mkd(port)
ftp.quit()
except ftplib.error_perm:
ftp.quit()
except ftplib.error_perm:
ftp.cwd(ip_addr)
try:
ftp.mkd(port)
ftp.quit()
except ftplib.error_perm:
ftp.quit()
##############取端口目录下的service目录
for services in sorted(os.listdir(port_dir)):
services_dir = port_dir + "/" + services
##############判断services目录是否存在,即第三级 如:192.168.20.130/8000/yunwei
ftp = ftplib.FTP("19xxxxx")
ftp.login("sxxx","sxxxxx",10)
try:
ftp.cwd(ftp_dir)
except ftplib.error_perm:
pass
try:
ftp.mkd(services)
except ftplib.error_perm:
ftp.quit()
#############遍历service目录中的符合的文件
for file in sorted(os.listdir(services_dir)):
match = pattern.search(file)
y_match = year_pattern.search(file)
if match:
#############匹配到文件后进行 文件名 更换操作,方便上传
ftpdir_service = ftp_dir + "/" + services
#############登录到ftp上的对应目录,准备上传
ftp = ftplib.FTP("xxxxx")
ftp.login("xxxxx","xxxxxx",10)
try:
ftp.cwd(ftpdir_service)
except ftplib.error_perm:
pass
filename = services_dir + "/" + file
file_gz = file + ".gz"
#############上传压缩文件到ftp
os.environ['filename'] = str(filename)
os.system('gzip $filename')
filename_gz = filename + ".gz"
#############只读模式打开本地需要上传的文件
filename_put = open(filename_gz,'rb')
ftp.storbinary('STOR %s' % os.path.basename(filename_gz),filename_put)
os.remove(filename_gz)
#############替换文件日期,即取出一年前的文件格式进行后续的ftp.delete
year_filename = filename_gz.replace(thirday,yearday)
#############错误处理,如果文件不存在时 pass
#ftp.dir(file_gz)
try:
ftp.delete(year_filename)
except ftplib.error_perm:
pass