responseheaders.py 658 Bytes
import cherrypy

class ResponseHeaders(cherrypy.Tool):

    def __init__(self, headers):
        """ This Tool adds arbitrary headers to a response object.
            These headers are defined in the site configuration under the [response-headers]
            section, and passed into this class upon instantiation in Serve.py
        """

        self.headers = headers
        cherrypy.Tool.__init__(self, 'before_finalize', self.append_headers, priority=20)

    def _setup(self):

        cherrypy.Tool._setup(self)

    def append_headers(self):

        for header, value in self.headers.items():
            cherrypy.response.headers[header] = value