Nacre/tests/smoke/cache/server.py

33 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""HTTP cache check server for tests/smoke/cache.txt: serves a page whose stylesheet may be cached for an hour,
and /count, the number of times the stylesheet was fetched. Listens on 127.0.0.1:8765 until killed."""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
fetches = 0
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
global fetches
if self.path == "/style.css":
fetches += 1
self.reply(b"body { background: #2a7; }", "text/css", "max-age=3600")
elif self.path == "/count":
self.reply(str(fetches).encode(), "text/plain", "no-store")
else:
page = b'<!doctype html><title>Cache</title><link rel="stylesheet" href="/style.css"><p>Cache check</p>'
self.reply(page, "text/html", "no-cache")
def reply(self, body, kind, cache):
self.send_response(200)
self.send_header("Content-Type", kind)
self.send_header("Cache-Control", cache)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
ThreadingHTTPServer(("127.0.0.1", 8765), Handler).serve_forever()