# -*- coding: utf-8 -*- r'''调试:管理页的 HEAD 为什么报告成 text/plain。''' from __future__ import annotations import os import subprocess import sys import time import urllib.request from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") PORT = 8947 def wait(seconds=12.0) -> bool: end = time.time() + seconds while time.time() < end: try: with urllib.request.urlopen( "http://127.0.0.1:%d/v1/health" % PORT, timeout=2): return True except Exception: # noqa: BLE001 time.sleep(0.5) return False def main() -> int: # 先看代码里 _head_info 到底长什么样 print(" 代码里的 _head_info 分支顺序:") src = (ROOT / "relay" / "relay.py").read_text(encoding="utf-8") start = src.find("def _head_info") for line in src[start:start + 2600].splitlines(): stripped = line.strip() if stripped.startswith(("if ", "return ok", "return no", "def ", "target =", "body =")): print(" " + stripped[:78]) print() print(" 直接 import 试调用:") sys.path.insert(0, str(ROOT / "relay")) os.environ["RELAY_PORT"] = str(PORT) data = ROOT / "_build" / "_hdrdbg" data.mkdir(parents=True, exist_ok=True) os.environ["RELAY_DATA"] = str(data) import importlib import relay as relay_mod importlib.reload(relay_mod) handler = relay_mod.Handler info = handler._head_info(None, relay_mod.ADMIN_PATH) print(" ADMIN_PATH = %r" % relay_mod.ADMIN_PATH) print(" 返回: %r" % (info,)) info2 = handler._head_info(None, "/") print(" '/' 返回: status=%s ctype=%s len=%s" % (info2["status"], info2["ctype"], info2["length"])) info3 = handler._head_info(None, "/download/ykxmz-watch-1.8.0.apk") print(" 下载返回: status=%s ctype=%s len=%s" % (info3["status"], info3["ctype"], info3["length"])) print() print(" 真起一个中转,发 HEAD 看真实响应:") env = dict(os.environ) proc = subprocess.Popen([sys.executable, "relay.py"], cwd=str(ROOT / "relay"), env=env, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) try: if not wait(): print(" [失败] 没起来") err = proc.stderr.read(2000).decode("utf-8", "replace") print(err[-800:]) return 1 for path in ("/", relay_mod.ADMIN_PATH, "/v1/health", "/download/ykxmz-watch-1.8.0.apk"): req = urllib.request.Request( "http://127.0.0.1:%d%s" % (PORT, path), method="HEAD") try: with urllib.request.urlopen(req, timeout=8) as resp: print(" %-38s %s len=%s type=%s" % (path, resp.status, resp.headers.get("Content-Length"), resp.headers.get("Content-Type"))) except Exception as exc: # noqa: BLE001 print(" %-38s 异常 %s" % (path, exc)) finally: proc.terminate() try: err = proc.stderr.read(3000).decode("utf-8", "replace") except Exception: # noqa: BLE001 err = "" try: proc.wait(timeout=6) except subprocess.TimeoutExpired: proc.kill() if "Traceback" in err: print() print(" 中转报错:") print(" " + err.strip()[-1200:].replace("\n", "\n ")) return 0 if __name__ == "__main__": sys.exit(main())