import gradio as gr import requests import time import socket def check_url_status(): """检查目标URL的状态""" target_url = "http://47.95.6.204:51000/" # 首先检查端口是否开放 try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5) result = sock.connect_ex(('47.95.6.204', 51000)) sock.close() if result != 0: return f"❌ 端口连接失败: 无法连接到 47.95.6.204:51000 (端口可能未开放)" except Exception as e: return f"❌ 网络错误: {str(e)}" # 然后尝试HTTP请求 try: response = requests.get(target_url, timeout=10) return f"✅ URL状态: {response.status_code} - 可访问" except requests.exceptions.ConnectTimeout: return f"❌ 连接超时: 服务器响应时间过长" except requests.exceptions.ConnectionError: return f"❌ 连接错误: 无法连接到服务器" except Exception as e: return f"❌ URL错误: {str(e)}" def ping_server(): """简单的服务器连通性测试""" try: import subprocess result = subprocess.run(['ping', '-n', '2', '47.95.6.204'], capture_output=True, text=True, timeout=10) if result.returncode == 0: return "✅ 服务器IP可达" else: return "❌ 服务器IP不可达" except Exception as e: return f"❌ Ping测试失败: {str(e)}" def create_webpage_display(): """创建显示指定网页的HTML内容""" # 添加详细的调试信息 debug_info = f"调试时间: {time.strftime('%Y-%m-%d %H:%M:%S')}
" debug_info += f"目标URL: http://47.95.6.204:51000/
" debug_info += f"网络连通性: {ping_server()}
" debug_info += f"服务状态: {check_url_status()}

" iframe_html = f'''

🔍 详细诊断信息:

{debug_info}

📋 可能的解决方案:

如果服务器不可达,可以尝试以下方案:

  1. 检查服务器是否正在运行
  2. 确认端口47.95.6.204:51000是否开放
  3. 检查防火墙设置
  4. 使用下面的演示URL进行测试

🌐 方式1: 直接链接测试

🔗 点击测试原始链接

🔗 点击测试演示链接 (httpbin.org)

🖼️ 方式2: iframe嵌入测试

原始URL iframe:

演示URL iframe (应该可以正常显示):

📊 技术说明

在Hugging Face Spaces上部署时需要注意:

''' return iframe_html # 创建 Gradio 界面 with gr.Blocks(title="网页显示器", theme=gr.themes.Soft()) as demo: gr.Markdown("# 🌐 网页内容显示器") gr.Markdown("**目标**: 显示来自 `http://47.95.6.204:51000/` 的网页内容") with gr.Tab("📋 诊断信息"): # 显示网页内容 webpage_html = gr.HTML(value=create_webpage_display()) # 添加刷新按钮 refresh_btn = gr.Button("🔄 刷新诊断", variant="primary") refresh_btn.click(fn=create_webpage_display, outputs=webpage_html) with gr.Tab("🔧 备用方案"): gr.Markdown("### 如果原始服务器无法访问,可以尝试以下方案:") # 添加获取网页源码的功能 def get_webpage_content(): try: response = requests.get("http://47.95.6.204:51000/", timeout=5) content = response.text # 简单处理HTML内容 import re content = re.sub(r'', '', content, flags=re.DOTALL) return f"

✅ 网页源码获取成功:

{content[:3000]}{'...' if len(content) > 3000 else ''}
" except Exception as e: return f"

❌ 获取失败:

{str(e)}

建议:

" content_btn = gr.Button("📄 尝试获取网页源码", variant="secondary") content_output = gr.HTML() content_btn.click(fn=get_webpage_content, outputs=content_output) gr.Markdown("---") # 添加演示功能 def show_demo_content(): demo_html = '''

🎉 演示网页内容

这是一个演示,说明iframe功能正常工作。

如果你的服务器恢复,应该显示类似的内容:

部署到Hugging Face时注意:

''' return demo_html demo_btn = gr.Button("🎬 显示演示内容", variant="secondary") demo_output = gr.HTML() demo_btn.click(fn=show_demo_content, outputs=demo_output) # 启动应用 if __name__ == "__main__": demo.launch()