前端新增播放音效

This commit is contained in:
zqc
2026-03-11 12:17:15 +08:00
parent 7c7e83eda8
commit 3cf386fbef
2 changed files with 103 additions and 3 deletions

View File

@@ -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: