#!/usr/bin/env python3 """TSEP local HTTP experiment. Run with python3 cdn-demo.py. Two loopback servers simulate an origin and a CDN. A page and a static verification screen deliberately share the same URL and status. The SHA-256 digests cover the exact response bodies received by the lab client. The observations apply only to these fixtures. They establish nothing about a live CDN, an actual Googlebot visit, indexing, rankings or AI citations. """ from contextlib import contextmanager from hashlib import sha256 from http.client import HTTPConnection from http.server import BaseHTTPRequestHandler, HTTPServer import json from threading import Thread CLIENT = "TSEP-Lab/1.0" PATH = "/product" TYPE = "text/html; charset=utf-8" # Keep the same title on purpose. Look for a product marker in the response body. PRODUCT = b"Blue notebook

Blue notebook

SKU-42

" CHALLENGE = b"Blue notebook

Verify your browser

" class Handler(BaseHTTPRequestHandler): def log_message(self, *args): pass def send_html(self, body): self.send_response(200) self.send_header("Content-Type", TYPE) self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) class Origin(Handler): def do_GET(self): if self.path != PATH: self.send_error(404) return self.server.requests += 1 self.send_html(PRODUCT) class Edge(Handler): def do_GET(self): if self.path != PATH: self.send_error(404) return if self.server.challenge: self.send_html(CHALLENGE) return if self.server.cached is None: status, content_type, body = fetch(self.server.origin) if status != 200 or content_type != TYPE: self.send_error(502) return self.server.cached = body self.send_html(self.server.cached) @contextmanager def serve(handler, **state): with HTTPServer(("127.0.0.1", 0), handler) as server: vars(server).update(state) worker = Thread(target=server.serve_forever, args=(0.05,)) worker.start() try: yield server finally: server.shutdown() worker.join() def fetch(server): client = HTTPConnection(*server.server_address, timeout=5) try: client.request("GET", PATH, headers={"User-Agent": CLIENT}) response = client.getresponse() return response.status, response.getheader("Content-Type"), response.read() finally: client.close() def experiment(): cases = [] with serve(Origin, requests=0) as origin, serve( Edge, origin=origin, cached=None, challenge=False ) as edge: warmup = fetch(edge) warmup_calls = origin.requests if warmup != (200, TYPE, PRODUCT) or warmup_calls != 1: raise AssertionError("The warm-up must fetch the product once from the origin.") for scenario, expected in (("cache_hit", PRODUCT), ("challenge", CHALLENGE)): edge.challenge = scenario == "challenge" before = origin.requests status, content_type, body = fetch(edge) calls = origin.requests - before if (status, content_type, body, calls) != (200, TYPE, expected, 0): raise AssertionError("Unexpected HTTP result for " + scenario) html = body.decode("utf-8") cases.append({ "scenario": scenario, "status": status, "origin_requests": calls, "content_type": content_type, "title": html.split("")[1].split("")[0], "heading": html.split("

")[1].split("

")[0], "product_code": "SKU-42" if "

SKU-42

" in html else "∅", "html": html, "sha256": sha256(body).hexdigest(), }) if cases[0]["sha256"] == cases[1]["sha256"]: raise AssertionError("The two response bodies must differ.") return { "scope": "Local HTTP simulation with synthetic pages and a lab client.", "client": CLIENT, "path": PATH, "warmup_origin_requests": warmup_calls, "cases": cases, } if __name__ == "__main__": print(json.dumps(experiment(), indent=2))