Files
test/sf_mrs_connect/models/ftp_operate.py
2024-07-31 14:54:15 +08:00

141 lines
5.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
import os
from ftplib import FTP
import logging
# FTP接口类
class FtpController():
'''
这是ftp接口类在类初始化的时候就连接了ftp服务器能否成功连接有反馈。
类中定义了两个接口:上传接口和删除接口
'''
ftp = FTP()
def __init__(self, host, port, username, password):
try:
self.ftp.connect(host, port)
self.ftp.login(username, password)
logging.info("ftp连接成功")
except Exception:
logging.info("ftp连接失败")
def file_exists_1(self, path):
# 检查文件是否存在于FTP服务器上
try:
logging.info("path:%s" % path)
logging.info("dirname:%s" % os.path.dirname(path))
directories = os.path.normpath(path).split(os.path.sep)
# 切换到上级目录
for directory in directories:
if directory:
# 检查目录是否存在
if (directory in ['NC']) or (directory not in ['home', 'ftp', 'ftp_root', 'NC']):
self.ftp.cwd(directory)
return os.path.basename(path)
except Exception as e:
logging.error(f"Error checking file: {e}")
return False
def file_exists(self, path):
# 检查文件是否存在于FTP服务器上
try:
logging.info("dirname:%s" % os.path.dirname(path))
self.ftp.cwd(os.path.dirname(path))
files = self.ftp.nlst()
return os.path.basename(path) in files
except Exception as e:
logging.error(f"Error checking file: {e}")
return False
# 下载目录下的pdf文件(程序单/检测文件)
def download_program_file(self, target_dir, serverdir):
if not os.path.exists(serverdir):
os.makedirs(serverdir)
try:
logging.info('FTP目录:%s' % target_dir)
logging.info("进入FTP目录 ")
remotenames = self.ftp.nlst()
logging.info('FTP目录文件:%s' % remotenames)
for file in remotenames:
server = os.path.join(serverdir, file)
if file.find(".pdf") != -1:
self.download_file(server, file)
return True
except:
return False
finally:
self.ftp.quit()
logging.info("ftp已关闭")
# # 检测字符串的编码
# def detect_encoding(self, s):
# result = chardet.detect(s)
# return result['encoding']
# 下载目录下的文件
def download_file_tree(self, target_dir, serverdir):
if not os.path.exists(serverdir):
os.makedirs(serverdir)
try:
logging.info("进入FTP目录 ")
self.ftp.pwd()
logging.info('当前目录:%s' % self.ftp.pwd())
logging.info('目录:%s' % target_dir)
target_dir1 = target_dir.split('/')
logging.info('目录1:%s' % target_dir1[1])
self.ftp.cwd(target_dir1[1]) # 切换工作路径
logging.info('目录2:%s' % target_dir1[2])
self.ftp.cwd(target_dir1[2]) # 切换工作路径
logging.info('目录3:%s' % target_dir1[3])
self.ftp.cwd(target_dir1[3]) # 切换工作路径
logging.info('目录4:%s' % target_dir1[4])
self.ftp.cwd(target_dir1[4]) # 切换工作路径
remotenames = self.ftp.nlst()
logging.info('FTP目录文件:%s' % remotenames)
for file in remotenames:
server = os.path.join(serverdir, file)
if file.find(".") != -1:
self.download_file(server, file)
return 1
except Exception:
return 0
finally:
self.ftp.quit()
logging.info("ftp已关闭")
# 下载目录下的检测文件
def download_reportfile_tree(self, target_dir, serverdir, reportpath):
if not os.path.exists(serverdir):
os.makedirs(serverdir)
try:
logging.info("进入FTP目录-检测文件")
logging.info('serverdir:%s' % serverdir)
target_dir1 = target_dir.split('/')
logging.info('目录1:%s' % target_dir1[1])
self.ftp.cwd(target_dir1[1]) # 切换工作路径
logging.info('目录2:%s' % target_dir1[2])
self.ftp.cwd(target_dir1[2]) # 切换工作路径
logging.info('目录2:%s' % target_dir1[3])
self.ftp.cwd(target_dir1[3]) # 切换工作路径
remotenames = self.ftp.nlst()
logging.info('FTP目录检测报告文件:%s' % remotenames)
for filename in remotenames:
if os.path.basename(filename) == os.path.basename(reportpath):
server = os.path.join(serverdir, filename)
logging.info('server%s' % server)
self.download_file(server, filename)
return 1
except Exception:
return 0
finally:
self.ftp.quit()
logging.info("ftp已关闭")
# 下载指定目录下的指定文件
def download_file(self, serverfile, remotefile):
file_handler = open(serverfile, 'wb')
self.ftp.retrbinary('RETR ' + remotefile, file_handler.write)
file_handler.close()