我有一个python的web应用程序,web服务器是用library web.py实现的。
但是,当浏览器在web服务器上发送请求时,例如在/static/index.html上,它在http头文件中包含“if-match-none”和“if-modified-since”字段,并且服务器检查html页面请求是否自上次以来已被修改(以及服务器使用http 304-not modified的响应)。
在任何情况下,我如何强制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()

10-04 11:48
查看更多