【问题标题】:python cherrypy - how to add headerpython cherrypy - 如何添加标题
【发布时间】:2011-05-19 06:02:55
【问题描述】:

如何在cherrypy中添加retry-header?

  import cherrypy
  import os

  class Root:

    def index(self):
      cherrypy.response.headers['Retry-After'] = 60
      cherrypy.request.headers["Age"]= 20
      cherrypy.config.update({'Retry-After': '60'})

      raise cherrypy.HTTPError(503, 'Service Unavailable')
    index.exposed = True 

    cherrypy.quickstart(Root())

此重试标头 dt 有效。

【问题讨论】:

  • 一点建议:python 中常见的缩进级别是 4 个空格 - 您可能想要遵循它而不是使用 2 个空格。
  • 好的,谢谢。你能帮我解答我的问题吗?

标签: python cherrypy


【解决方案1】:

当您通过提高HTTPError 设置状态代码时,cherrypy.response.headers 中的标头将被忽略。通过设置 cherrypy.response.status 来设置 HTTP 状态:

import cherrypy

class Root:
    def index(self):
        cherrypy.response.headers['Retry-After'] = 60
        cherrypy.response.status = 503
        # Feel free to return a better error page than the following
        return "<h1>Service Unavailable</h1>"
    index.exposed = True

cherrypy.quickstart(Root())

【讨论】:

猜你喜欢
  • 2022-07-05
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 2015-11-13
  • 2021-11-09
  • 2013-04-01
  • 2017-09-05
  • 2016-12-01
相关资源
最近更新 更多