The checkbox changes the server header

I made a simple flask app:

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'd like to change is the server header, which is currently set Werkzeug/0.9.6 Python/2.7.6

to be something of my own choice. But I can't find anything in the documentation on how to do this.

+4


source to share


4 answers


You can use Flask's make_response method to add or change headers.



from flask import make_response

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

      

+9


source


The @bcarroll call works, but it will bypass other processes defined in the original process_response method, such as setting a 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)

      

+6


source


You can change the server header for each 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

+3


source


Overriding the server header in code doesn't work if you're using the production server like gunicorn. Your best bet is to use a proxy behind Gunicorn and change the server header.

0


source







All Articles