#!/usr/bin/env python3 """ 诊断脚本:验证图片 URL 和 Word 文档 URL 是否可访问 """ import urllib.request import requests import sys from typing import Dict, List, Tuple def check_url(url: str, headers: Dict[str, str] = None, timeout: int = 10) -> Tuple[bool, str, int]: """ 检查 URL 是否可访问 Args: url: 要检查的 URL headers: HTTP 头 timeout: 超时时间(秒) Returns: (is_valid, error_message, status_code) """ if headers is None: headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', 'Accept': '*/*', } try: req = urllib.request.Request(url, headers=headers) with urllib.request.urlopen(req, timeout=timeout) as response: status_code = response.getcode() content_type = response.headers.get('Content-Type', '') # 读取前 100 字节检查 preview = response.read(100) # 检查是否为 HTML 错误页面 if b' 0: print("=" * 80) print("⚠️ 警告:部分 URL 无法访问") print("=" * 80) print("可能的原因:") print("1. URL 已过期(阿里云 OSS 签名 URL 有时效性)") print("2. 访问权限不足(需要认证或 IP 白名单)") print("3. 网络连接问题(Docker 容器网络配置)") print("4. 文件已被删除或移动") print() print("建议:") print("- 检查这些 URL 是否在浏览器中可以访问") print("- 如果使用签名 URL,确保 URL 未过期") print("- 检查 Docker 容器的网络配置和 DNS 设置") print("- 考虑使用公开可访问的 URL 或配置 OSS 访问权限") sys.exit(1) else: print("✅ 所有 URL 均可访问") sys.exit(0) if __name__ == "__main__": main()