Skip to main content
 首页 » 编程设计

python之在 Python BaseHTTPServer 中检测浏览器何时命中 "refresh"

2025年05月04日12think

我如何检测客户端何时按下 F5/刷新?是否有任何 header 发送到服务器以表明我可以获取?我正在做一些服务器端缓存,我想在检测到刷新时自动使缓存过期。谢谢。

请您参考如下方法:

ETags 可能是更正确的解决方案,但只需查找 Cache-Control header 就足够了,因为浏览器将使用它来指示不应使用缓存。

Chrome 发送 Cache-Control: max-age=0,但其他一些浏览器发送 Cache-Control: no-cache。您可能希望在您不认识的任何缓存控制 header 上使缓存过期。

甚至:

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