import html, re, zipfile from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import unquote, urlparse import pathlib ARCHIVE = str(pathlib.Path(__file__).resolve().parent / '如东SWT40_控制律仿真台_20260906.zip') def readable(name): try: return name.encode('cp437').decode('utf-8') except UnicodeError: return name with zipfile.ZipFile(ARCHIVE) as archive: PAGES = { readable(name.rsplit('/', 1)[-1]): archive.read(name).decode('utf-8') for name in archive.namelist() if name.endswith('.html') and readable(name.rsplit('/', 1)[-1])[:1] in {'0', '1', '2', '3', '4'} } THEME = ''' ''' THERMAL = ''' ''' # 对外脱敏 —— 规则表唯一正本在 release/scrub_rules.py, 禁止在此另抄一份 from scrub_rules import scrub, residual def page_for(name): raw = PAGES[name] # Current delivered generator page references two removed top-level fields. Map them # to their structured source fields before its own simulation loop starts. if name.startswith('3_'): raw = raw.replace('TH.A_noload', 'TH.stator_model.A').replace('TH.B_copper', 'TH.stator_model.B') if name.startswith('0_'): raw = raw.replace('如东 SWT-4.0-130 控制律仪表台', '如东 SWT-4.0-130 · 四系统联调') raw = raw.replace('', '', 1) raw = raw.replace('', THEME + '', 1) header = '
DASHENG · 分系统仿真← 返回仿真中心
' raw = raw.replace('
', '
' + header, 1) raw = raw.replace('$("#th").onclick=()=>{', '$("#th").style.display="none"; $("#th").onclick=()=>{', 1) if name.startswith('3_'): raw = raw.replace('', THERMAL + '', 1) raw = scrub(raw) # 最后一道: 连注入的 header/THEME 一起洗 return raw class Handler(BaseHTTPRequestHandler): def do_GET(self): name = unquote(urlparse(self.path).path).lstrip('/') or '0_四系统合页.html' if name not in PAGES: self.send_error(404, 'simulation page not found') return body = page_for(name).encode('utf-8') self.send_response(200) self.send_header('Content-Type', 'text/html; charset=utf-8') self.send_header('Content-Length', str(len(body))) self.end_headers() self.wfile.write(body) def log_message(self, fmt, *args): print('[18792]', fmt % args, flush=True) import pathlib as _pl, sys as _sys0 _sys0.path.insert(0, str(_pl.Path(__file__).resolve().parents[1])) from src import logfile as _lf; _lf.prefix_stdout('sim_sys') # 统一日志格式 (用户令 2) ThreadingHTTPServer(('127.0.0.1', 18792), Handler).serve_forever()