Files
scuc-qt-course/docs/teaching/serve.py
T
张宗平 b89819d5db 新增 4 天教案大纲 + reveal.js 交互幻灯片(docs/teaching/)
大纲按「3天C++强化(含OOP)+1天Qt桌面」组织,讲:练≈1:2.7;每个知识点直接
链接仓库真实代码文件与 ERRATA/VERSION_NOTES 里记录的具体坑。幻灯片
vendor 了 reveal.js 5.2.1(核心+highlight插件),零 CDN 依赖,配 serve.py
本地起服务(仅绑定 127.0.0.1)供离线播放,代码链接可点开查看源码。
2026-07-01 16:51:12 +08:00

38 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""本地起 HTTP 服务播放教案幻灯片(离线可用)。
用法:python3 docs/teaching/serve.py [端口,默认 8000]
浏览器打开 http://localhost:<端口>/docs/teaching/slides/index.html
只绑定 127.0.0.1(不对局域网暴露)——仓库根目录含 .env(wiki 登录凭据),
必须避免整个仓库被同一 Wi-Fi/局域网上的其他设备访问到。
"""
import http.server
import mimetypes
import os
import socketserver
import sys
# 幻灯片里点击的源码链接(.c/.cpp/.py/.md 等)需要以纯文本显示,而不是被当成
# 未知二进制触发下载——默认 mimetypes 对 .cpp/.c 等会猜成 text/x-csrc 之类,
# 浏览器不会内联渲染,这里统一覆盖成 text/plain。
for ext in (".c", ".cpp", ".h", ".hpp", ".cc", ".qrc", ".py", ".md"):
mimetypes.add_type("text/plain", ext)
repo_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.chdir(repo_root)
try:
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
except ValueError:
print(f"用法:python3 {sys.argv[0]} [端口号]")
sys.exit(1)
class ReuseAddrTCPServer(socketserver.TCPServer):
allow_reuse_address = True
with ReuseAddrTCPServer(("127.0.0.1", port), http.server.SimpleHTTPRequestHandler) as httpd:
print(f"Serving {repo_root} at http://localhost:{port}/docs/teaching/slides/index.html")
httpd.serve_forever()