1

Using Python 2.7.2 on OSX (darwin), I would like to hide or customize the "Server" response header sent by the wsgiref.simple_server.make_server().

I tried many things without any success and was pretty sure this sample code should work:

from wsgiref import simple_server

class MyWSGIRequestHandler(simple_server.WSGIRequestHandler):

    server_version = "X/1"
    sys_version = "Y/2"

httpd = simple_server.make_server('', 8082, simple_server.demo_app, handler_class=MyWSGIRequestHandler)

print "version_string: %s %s" % (httpd.RequestHandlerClass.server_version, httpd.RequestHandlerClass.sys_version)
# it prints "X/1 Y/2" 
httpd.serve_forever()

But it's always the same and there's no way to get rid of the "Server: WSGIServer/0.1 Python/2.7.2" sent by the server. I've also tried to override the version_string method in my class, for example with something like that:

class MyWSGIRequestHandler(simple_server.WSGIRequestHandler):
    def version_string(self):
        return "42"

It changes nothing, I really don't understand what's happening here.

Can someone help me please?

1 Answer 1

4

I've finally found the solution, no need to override WSGIRequestHandler.

from wsgiref.simple_server import ServerHandler
ServerHandler.server_software = "Fake Server Name Here"

And then you can call make_server().

1
  • 1
    Thank you, If you run like, python manage.py runserver, you should to append manage.py file before line execute_from_command_line(sys.argv)
    – Türkalp
    Commented Apr 21, 2020 at 13:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.