【问题标题】:Disable caching in web.py web server, ignoring HTTP header禁用 web.py Web 服务器中的缓存,忽略 HTTP 标头
【发布时间】:2016-08-22 23:57:43
【问题描述】:

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

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

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

网络服务器的代码如下。

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()

【问题讨论】:

  • 作为权宜之计,您可以将流行的 Web 浏览器配置为在“开发者控制台”打开时禁用缓存。这对我来说已经足够了。

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


【解决方案1】:

您需要在响应头中添加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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多