python cherrypy - how to add header
Asked Answered
B

1

11

How can I add retry-header in cherrypy?

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

This retry-header dt works.

Benignant answered 19/5, 2011 at 6:2 Comment(2)
Just a little suggestion: The common indentation level in python is 4 spaces - you might want to follow it instead of using 2 spaces.Demonetize
ok thanks. can you help me in getting answer of my question?Benignant
L
26

When you set a status code by raising HTTPError, the headers in cherrypy.response.headers are ignored. Set the HTTP status by setting cherrypy.response.status instead:

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())
Lamb answered 19/5, 2011 at 6:29 Comment(1)
You're welcome! If the answer solved your problem, please consider accepting it to show others that your question has been answered and to give credit for the answer: meta.stackexchange.com/questions/5234/…Polygenesis

© 2022 - 2024 — McMap. All rights reserved.