python - 在 web.py web 服务器中禁用缓存,忽略 HTTP header

标签 python http caching web.py http-status-code-304

我有一个 python 网络应用程序,网络服务器是用库 web.py 实现的。

但是,当浏览器向 Web 服务器发送请求时,例如/static/index.html,它在 http header 中包含字段“IF-MATCH-NONE”和“IF-MODIFIED-SINCE”以及服务器检查 html 页面请求自上次以来是否已被修改(以及服务器响应 http 304 - 未修改)...

如何在任何情况下强制响应 html 页面,即使它没有被修改?

Web 服务器的代码如下。

import web

urls= (
    '/', 'redirect',
    '/static/*','index2',
    '/home/','process'
)

app=web.application(urls,globals())


class redirect:
        def GET(self):
                ..              
                return web.redirect("/static/index.html")

        def POST(self):
                ..
                raise web.seeother("/static/index.html")

class index2:
    def GET(self):
        ...
                some checks
                ....


if __name__=="__main__":
    app.run()

最佳答案

需要在响应头中添加Cache-Control字段:

web.header("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")

例如:

import web

urls = ("/.*", "hello")
app = web.application(urls, globals())

class hello(object):
    def GET(self):
        web.header("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
        return "Hello, world!"

if __name__ == "__main__":
    app.run()

关于python - 在 web.py web 服务器中禁用缓存,忽略 HTTP header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36910952/

相关文章:

python - 解释器维护的整数缓存有什么用?

python - 搜索列表列表并显示结果

使用 NLTK 的 Python 在 sent_tokenize 和 word_tokenize 处显示错误

Python urllib 只下载网页的某些部分?

Python Tkinter Canvas 的边框与 `create_window' 创建的窗口重叠

javascript - 混合应用程序 : Change HTTP user agent header with AngularJS

json - 差异 : JSONP vs GET

flutter - 如何修复 HttpException : Connection closed before full header was received

azure - 在 azure 服务结构中实现缓存

javascript - 清除azure中javascript的缓存