Use a loopback-only test server
The example uses only the Python standard library and binds an unused port on 127.0.0.1. It does not contact an external site or require credentials. The first GET stores a response body and its ETag; the second sends that tag in If-None-Match. The fixed representation deliberately isolates the unchanged case. Cache-Control: no-cache requires validation before reuse rather than prohibiting storage; no-store is a different directive. The client code explicitly keeps the stored body.
Trace the response and stored body
Run the code and observe a 200 response followed by a 304 response. urllib represents the latter as HTTPError, so the program catches it and checks the status. The 304 does not supply a replacement response body; the example reuses the bytes saved from the first request. Real HTTP caches also update stored metadata and calculate freshness. This small program intentionally makes storage and reuse explicit rather than pretending to implement a complete browser caching subsystem.
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread
from urllib.request import Request, urlopen
from urllib.error import HTTPError
BODY = b"revision one\n"
ETAG = '"example-v1"'
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
same = self.headers.get("If-None-Match") == ETAG
self.send_response(304 if same else 200)
self.send_header("ETag", ETAG)
self.send_header("Cache-Control", "no-cache")
self.end_headers()
if not same: self.wfile.write(BODY)
def log_message(self, *args): pass
server = HTTPServer(("127.0.0.1", 0), Handler)
worker = Thread(target=server.serve_forever, daemon=True)
worker.start()
url = f"http://127.0.0.1:{server.server_port}/item"
try:
with urlopen(url) as first:
saved = first.read(); tag = first.headers["ETag"]
print(first.status, saved.decode().strip(), tag)
try:
with urlopen(Request(url, headers={"If-None-Match": tag})):
raise AssertionError("Expected 304 for the matching validator")
except HTTPError as response:
body = response.read()
response.close()
print(response.code, "body_bytes", len(body))
assert response.code == 304 and body == b""
assert saved == BODY
print("reuse", saved.decode().strip())
finally:
server.shutdown(); worker.join(); server.server_close()Change the validator carefully
Send a different If-None-Match value to exercise the 200 path. Changing the body while retaining a supposedly equivalent validator can incorrectly hide a representation change, so validator generation must follow the representation’s semantics. The toy server compares one exact tag string; it does not implement lists of entity tags, weak-tag comparison or wildcard conditions. Production conditional-request handling should use a conforming HTTP implementation instead of copying that simplified equality test into a general-purpose server.
Interpret savings without removing the round trip
A 304 can avoid retransmitting a large body, but a network round trip still occurs. Reduced payload bytes and reduced waiting time are different effects. A fresh cached response may sometimes be reused without origin contact, according to its cache policy. Authorization and Vary can also change which stored representation and validator apply. Shut down the loopback server, join its thread and close the socket when finished. There is no reason to expose this instructional server publicly.
An ETag is a validator associated with a representation, not an encoding from which the body can generally be reconstructed. Keep the validator paired with the stored response it identifies. Reusing a tag from another user’s representation or a different variant can invalidate the comparison. The explicit saved variable in the example makes that pairing visible without claiming to implement every real cache-key dimension.