From 3cf386fbef5c7dcfee484b92a851976dea92a464 Mon Sep 17 00:00:00 2001 From: zqc <835569504@qq.com> Date: Wed, 11 Mar 2026 12:17:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E6=96=B0=E5=A2=9E=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E9=9F=B3=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web_page_2/http_server.py | 46 +++++++++++++++++++++++++++++- web_page_2/index.html | 60 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/web_page_2/http_server.py b/web_page_2/http_server.py index 162da07..3ff8e5a 100644 --- a/web_page_2/http_server.py +++ b/web_page_2/http_server.py @@ -6,6 +6,24 @@ import socket class APIHandler(SimpleHTTPRequestHandler): # 设置超时,避免长时间占用连接 timeout = 30 + + # MIME 类型映射 + MIME_TYPES = { + '.html': 'text/html; charset=utf-8', + '.css': 'text/css', + '.js': 'application/javascript', + '.json': 'application/json', + '.png': 'image/png', + '.jpg': 'image/jpeg', + '.jpeg': 'image/jpeg', + '.gif': 'image/gif', + '.svg': 'image/svg+xml', + '.ico': 'image/x-icon', + '.mp3': 'audio/mpeg', + '.wav': 'audio/wav', + '.mp4': 'video/mp4', + '.txt': 'text/plain; charset=utf-8', + } def log_message(self, format, *args): # 自定义日志格式 @@ -31,7 +49,13 @@ class APIHandler(SimpleHTTPRequestHandler): # 默认访问使用 api=1 self.serve_file('index.html', query='api=1') else: - self.send_error(404, 'Not Found') + # 处理静态文件请求 + # 移除开头的 / + filename = path.lstrip('/') + if os.path.exists(filename): + self.serve_static_file(filename) + else: + self.send_error(404, 'Not Found') except Exception as e: print(f"Error handling request: {e}") self.send_error(500, 'Internal Server Error') @@ -67,6 +91,26 @@ class APIHandler(SimpleHTTPRequestHandler): print(f"Error serving file: {e}") raise + def serve_static_file(self, filename): + """提供静态文件服务""" + try: + # 获取文件扩展名 + ext = os.path.splitext(filename)[1].lower() + content_type = self.MIME_TYPES.get(ext, 'application/octet-stream') + + self.send_response(200) + self.send_header('Content-type', content_type) + self.send_header('Access-Control-Allow-Origin', '*') + self.end_headers() + + with open(filename, 'rb') as f: + content = f.read() + + self.wfile.write(content) + except Exception as e: + print(f"Error serving static file: {e}") + self.send_error(500, 'Internal Server Error') + def check_port_available(port): """检查端口是否可用""" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: diff --git a/web_page_2/index.html b/web_page_2/index.html index fdc3224..3eda82a 100644 --- a/web_page_2/index.html +++ b/web_page_2/index.html @@ -169,6 +169,26 @@ color: #60a5fa; border: 1px solid rgba(59, 130, 246, 0.3); } + .enable-sound-btn { + padding: 4px 12px; + background: #3b82f6; + color: #fff; + border: none; + border-radius: 4px; + font-size: 12px; + cursor: pointer; + transition: background 0.2s; + flex-shrink: 0; + margin-left: 8px; + } + .enable-sound-btn:hover { + background: #2563eb; + } + .enable-sound-btn:disabled { + background: #1f2937; + color: #6b7280; + cursor: not-allowed; + } @@ -176,7 +196,10 @@