Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python cherrypy - how to add header

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.

like image 558
sam Avatar asked May 19 '11 06:05

sam


1 Answers

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())
like image 125
Pär Wieslander Avatar answered Oct 31 '22 19:10

Pär Wieslander