Flask change the server header

Question:

I’ve made a simple flask application:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET / HTTP/1.1
host:google.be

HTTP/1.0 404 NOT FOUND
Content-Type: text/html
Content-Length: 233
Server: Werkzeug/0.9.6 Python/2.7.6
Date: Mon, 08 Dec 2014 19:15:43 GMT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>
Connection closed by foreign host.

One of the things I would like the change is the server header which at the moment is set as Werkzeug/0.9.6 Python/2.7.6 to something my own chosing. But I can’t seem to find anything in the documentation on how to do this.

Asked By: Lucas Kauffman

||

Answers:

You can use Flask’s make_response method to add or modify headers.

from flask import make_response

@app.route('/index')
def index():
    resp = make_response("Hello, World!")
    resp.headers['server'] = 'ASD'
    return resp
Answered By: Ishaan

You can change the Server header for every response by overriding the Flask.process_response() method.

    from flask import Flask
    from flask import Response

    SERVER_NAME = 'Custom Flask Web Server v0.1.0'

    class localFlask(Flask):
        def process_response(self, response):
            #Every response will be processed here first
            response.headers['server'] = SERVER_NAME
            return(response)

    app = localFlask(__name__)


    @app.route('/')
    def index():
        return('<h2>INDEX</h2>')

    @app.route('/test')
    def test():
        return('<h2>This is a test</h2>')

http://flask.pocoo.org/docs/0.12/api/#flask.Flask.process_response

Answered By: bcarroll

@bcarroll’s answer works but it will bypass other processes defined in original process_response method such as set session cookie.
To avoid the above:

class localFlask(Flask):
    def process_response(self, response):
        #Every response will be processed here first
        response.headers['server'] = SERVER_NAME
        super(localFlask, self).process_response(response)
        return(response)
Answered By: Xinyu Zhao

Overriding Server header in code does not work if You use production server like gunicorn. The better way is to use proxy server behind gunicorn and there change Server header.

Answered By: Darek

TL;DR – overwrite /python3.8/http/server.py send_response method. Comment the server header addition line.

Why?
Adding/Manipulating headers in flask (in any way that mentioned above) will fire the response with the configured headers from flask to the web server but the WSGI logic (which happens independently, after & before flask logic) will be the last one to modify those values if any.

In your case(Werkzeug) some headers are hard-coded in python http module which werkzeug depending on. The server header is one of them.

Answered By: IlayG01

Easy way:

@app.after_request
def changeserver(response):
    response.headers['server'] = SERVER_NAME
    return response
Answered By: JPN
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.