CherryPy server name tag
Asked Answered
P

3

7

When running a CherryPy app it will send server name tag something like CherryPy/version. Is it possible to rename/overwrite that from the app without modifying CherryPy so it will show something else?

Maybe something like MyAppName/version (CherryPy/version)

Pedi answered 25/9, 2008 at 19:8 Comment(0)
P
5

Actually asking on IRC on their official channel fumanchu gived me a more clean way to do this (using latest svn):

import cherrypy
from cherrypy import _cpwsgi_server 
class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

serverTag = "MyApp/%s (CherryPy/%s)" % ("1.2.3", cherrypy.__version__)
_cpwsgi_server.CPWSGIServer.environ['SERVER_SOFTWARE'] = serverTag
cherrypy.config.update({'tools.response_headers.on': True,
                        'tools.response_headers.headers': [('Server', serverTag)]})
cherrypy.quickstart(HelloWorld())
Pedi answered 16/10, 2008 at 14:31 Comment(1)
Well that was in 2008, in this version i think you should go ask on their irc channel i'm sure someone will help. At that time i asked the main dev and he was a really nice guy, i'm sure he will help you.Pedi
F
10

This can now be set on a per application basis in the config file/dict

[/]  
response.headers.server = "CherryPy Dev01"
Fidelia answered 23/2, 2013 at 0:29 Comment(0)
P
5

Actually asking on IRC on their official channel fumanchu gived me a more clean way to do this (using latest svn):

import cherrypy
from cherrypy import _cpwsgi_server 
class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

serverTag = "MyApp/%s (CherryPy/%s)" % ("1.2.3", cherrypy.__version__)
_cpwsgi_server.CPWSGIServer.environ['SERVER_SOFTWARE'] = serverTag
cherrypy.config.update({'tools.response_headers.on': True,
                        'tools.response_headers.headers': [('Server', serverTag)]})
cherrypy.quickstart(HelloWorld())
Pedi answered 16/10, 2008 at 14:31 Comment(1)
Well that was in 2008, in this version i think you should go ask on their irc channel i'm sure someone will help. At that time i asked the main dev and he was a really nice guy, i'm sure he will help you.Pedi
U
4

This string appears to be being set in the CherrPy Response class:

def __init__(self):
  self.status = None
  self.header_list = None
  self._body = []
  self.time = time.time()

  self.headers = http.HeaderMap()
  # Since we know all our keys are titled strings, we can
  # bypass HeaderMap.update and get a big speed boost.
  dict.update(self.headers, {
    "Content-Type": 'text/html',
    "Server": "CherryPy/" + cherrypy.__version__,
    "Date": http.HTTPDate(self.time),
  })

So when you're creating your Response object, you can update the "Server" header to display your desired string. From the CherrPy Response Object documentation:

headers

A dictionary containing the headers of the response. You may set values in this dict anytime before the finalize phase, after which CherryPy switches to using header_list ...

EDIT: To avoid needing to make this change with every response object you create, one simple way to get around this is to wrap the Response object. For example, you can create your own Response object that inherits from CherryPy's Response and updates the headers key after initializing:

class MyResponse(Response):

    def __init__(self):
        Response.__init__(self)
        dict.update(self.headers, {
            "Server": "MyServer/1.0",
        })

RespObject = MyResponse()
print RespObject.headers["Server"]

Then you can can call your object for uses where you need to create a Response object, and it will always have the Server header set to your desired string.

Uranie answered 25/9, 2008 at 20:0 Comment(2)
if i set it in init of my root class it doesn't work. cherrypy.response.headers["Server"]="app/0.1(CherryPy/"+cherrypy.__version__+")" if i set it in every method (index,foopage,etc) it works but it's a pain to write that in every method Can it be made globally so it will work on all pages?Pedi
The easiest way to do it globally is probably to wrap the Response object itself. I'll update the post with an example.Uranie

© 2022 - 2024 — McMap. All rights reserved.