From 94a11861257102d3c3a4c35308ddebb609f3c7c4 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 23 Feb 2025 00:50:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ping=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 7564e31..875f1fd 100644 --- a/main.py +++ b/main.py @@ -222,20 +222,32 @@ class DNSPodManager: else: # Linux/Unix系统 ping_args = ['ping', '-c', '1', '-W', '1', ip] - result = subprocess.run(ping_args, - capture_output=True, - text=True) - available = result.returncode == 0 + # 最多尝试3次ping + max_retries = 3 + for attempt in range(max_retries): + result = subprocess.run(ping_args, + capture_output=True, + text=True) + if result.returncode == 0: + # 更新缓存 + self.ip_availability_cache[ip] = { + 'available': True, + 'last_check': now + } + return True + + # 如果不是最后一次尝试,等待短暂时间后重试 + if attempt < max_retries - 1: + time.sleep(1) # 等待1秒后重试 + # 所有尝试都失败 + logger.warning(f"IP {ip} ping测试失败(尝试{max_retries}次),最后一次命令输出:{result.stdout if result.stdout else result.stderr}") # 更新缓存 self.ip_availability_cache[ip] = { - 'available': available, + 'available': False, 'last_check': now } - - if not available: - logger.warning(f"IP {ip} ping测试失败,命令输出:{result.stdout if result.stdout else result.stderr}") - return available + return False except Exception as e: logger.error(f"Ping测试出错 - IP: {ip}, 错误信息: {str(e)}, 命令参数: {ping_args}") return False