From dcd9c76ddd2385dce17b7f36134dd0d7d731c53d Mon Sep 17 00:00:00 2001 From: tangchao0503 <735056338@qq.com> Date: Sat, 11 Oct 2025 15:11:48 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E9=94=99=E8=AF=AF=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=EF=BC=9A=E6=96=87=E4=BB=B6=E4=B8=8D=E5=AD=98=E5=9C=A8=E3=80=81?= =?UTF-8?q?sif=E8=AE=A1=E7=AE=97=E5=A4=B1=E8=B4=A5=EF=BC=9B=202=E3=80=81?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E6=9F=90=E4=B8=80=E7=9B=AE=E5=BD=95=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E5=B0=86=E6=96=B0=E7=94=9F=E6=88=90=E7=9A=84csv?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=88=B0ftp=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 6 ++ upload_file_to_ftp.py | 130 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 upload_file_to_ftp.py diff --git a/main.py b/main.py index 5936f0b..45ad599 100644 --- a/main.py +++ b/main.py @@ -202,6 +202,8 @@ class CSVFileHandler(FileSystemEventHandler): # 上传多个文件 success_count = 0 for file_path in file_paths: + if not os.path.exists(file_path): + continue try: filename = os.path.basename(file_path) with open(file_path, "rb") as f: @@ -451,6 +453,7 @@ class CSVFileHandler(FileSystemEventHandler): command_str_sfm = program_path + " " + standard_sif_path + " " + input_path + " " + output_path_sfm + " " + param_sfm return_code = os.system(command_str_3fld) + print(command_str_3fld) return_code = os.system(command_str_sfld) return_code = os.system(command_str_sfm) print(f"命令返回状态码: {return_code}") @@ -458,6 +461,9 @@ class CSVFileHandler(FileSystemEventHandler): return output_path_3fld, output_path_sfld, output_path_sfm def add_validity_column_to_file(self, file_path, quality_level): + if not os.path.exists(file_path): + return + # 创建临时文件 temp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, newline='') diff --git a/upload_file_to_ftp.py b/upload_file_to_ftp.py new file mode 100644 index 0000000..11296e1 --- /dev/null +++ b/upload_file_to_ftp.py @@ -0,0 +1,130 @@ +import csv, tempfile, os, re +import struct +import time +import numpy as np +import argparse +import paramiko +import shutil +import configparser +from ftplib import FTP +from pathlib import Path +from watchdog.observers import Observer +from watchdog.events import FileSystemEventHandler +from datetime import datetime + +import ssl +from ftplib import FTP_TLS, error_perm + +def load_config(config_path='config.ini'): + config = configparser.ConfigParser() + config.read(config_path) + return config + +class CSVFileHandler(FileSystemEventHandler): + def __init__(self, ftp_config): + super().__init__() + self.ftp_config = ftp_config + + def on_created(self, event): + if event.is_directory: + return + if event.src_path.lower().endswith('.csv'): + file_path = os.path.abspath(event.src_path) + print(f"发现CSV文件: {file_path}----------------------------------------------------------------------------") + + time.sleep(0.1) # 文件一出现就处理文件,偶发permission deny,所以等待100ms + + self.send_via_ftps(file_path) + + def send_via_ftps(self, file_path, max_retries=3, retry_delay=5): + retries = 0 + ftps = None + + while retries < max_retries: + try: + print("正在尝试连接 FTPS 服务器...") + + # 建立 FTPS 连接 + ftps = FTP_TLS() + # 忽略自签名证书(你的服务器证书是 self-signed) + ftps.context = ssl._create_unverified_context() + ftps.connect( + host=self.ftp_config['FTP']['host'], + port=int(self.ftp_config['FTP'].get('port', 21)), # 默认 21, 你的可能是 65521 + timeout=30 + ) + + # 登录 + ftps.login( + user=self.ftp_config['FTP']['user'], + passwd=self.ftp_config['FTP']['password'] + ) + print("FTPS 连接成功,准备上传文件...") + + # 切换到安全数据通道 + ftps.prot_p() + + # 检查并切换到目标目录 + remote_dir = self.ftp_config['FTP'].get('target_dir', '.') + try: + ftps.cwd(remote_dir) + except error_perm: + print(f"远程目录不存在,尝试创建: {remote_dir}") + ftps.mkd(remote_dir) + ftps.cwd(remote_dir) + + # 上传多个文件 + success_count = 0 + if not os.path.exists(file_path): + continue + try: + filename = os.path.basename(file_path) + with open(file_path, "rb") as f: + ftps.storbinary(f"STOR {filename}", f) + print(f"✅ 文件上传成功: {filename}") + success_count += 1 + except Exception as e: + print(f"❌ 文件上传失败 {file_path}: {e}") + + return True + + except Exception as e: + retries += 1 + print(f"❌ FTPS 连接失败(尝试 {retries}/{max_retries}): {e}") + if retries < max_retries: + time.sleep(retry_delay) + finally: + if ftps: + try: + ftps.quit() + except Exception: + ftps.close() + + print(f"❌ 上传失败(已达最大重试次数 {max_retries})") + return False + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + parser = argparse.ArgumentParser(description="监控文件夹的状态,当出现新的csv时,提取sif,并通过ftp发送。", prog='sif.') + + parser.add_argument('-i', '--input_ini', required=True, type=str, help='输入ini配置文件路径。') + + parser.add_argument("-v", "--version", action='version', version='%(prog)s 1.0') + # parser.add_argument('-v', '--verbose', action='store_true', help='启用详细模式') + + args = parser.parse_args() + + ftp_config = load_config(args.input_ini) + event_handler = CSVFileHandler(ftp_config) + observer = Observer() + observer.schedule(event_handler, ftp_config['monitor']['WATCH_DIR'], recursive=True) + observer.start() + print(f"正在监控目录:{ftp_config['monitor']['WATCH_DIR']}") + + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + observer.stop() + observer.join()