import json, sys, threading, time from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer LOG = [] class H(BaseHTTPRequestHandler): def log_message(self, *a): pass def do_GET(self): body = json.dumps({"data": [{"id": "deepseek-flash"}, {"id": "deepseek-v4-pro"}]}).encode() self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def do_POST(self): n = int(self.headers.get("Content-Length", 0)) raw = self.rfile.read(n) try: LOG.append(json.loads(raw)) except Exception: LOG.append({"_raw": raw[:200].decode("latin1")}) self.send_response(200) self.send_header("Content-Type", "text/event-stream") self.end_headers() def ev(o): self.wfile.write(b"data: " + json.dumps(o).encode() + b"\n\n") self.wfile.flush() base = {"id": "x", "object": "chat.completion.chunk", "model": "deepseek-flash"} ev({**base, "choices": [{"index": 0, "delta": {"role": "assistant", "content": "reply-%d" % len(LOG)}}]}) ev({**base, "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 10, "completion_tokens": 2, "total_tokens": 12}}) self.wfile.write(b"data: [DONE]\n\n") self.wfile.flush() def start(): s = ThreadingHTTPServer(("127.0.0.1", 0), H) threading.Thread(target=s.serve_forever, daemon=True).start() return s, "http://127.0.0.1:%d/v1" % s.server_address[1]