Search code examples
pythonbasehttpserver

Detect when the browser hits "refresh" in Python's BaseHTTPServer


How can I detect when the client hits F5 / refresh? Are there any headers sent to the server to indicate this that I can grab? I'm doing some server-side caching and I'd like to automatically expire the cache when a refresh is detected. Thanks.


Solution

  • ETags may be a more correct solution, but simply looking for the Cache-Control header is enough, since the browser will use it to indicate caches should not be used.

    Chrome sends Cache-Control: max-age=0, but some other browsers send Cache-Control: no-cache. You may want to expire cache on any cache control header you don't recognize.

    Or even:

    if any(i.lower().startswith('cache-control:') for i in self.headers): #...