0 votes
in Python Flask by
How to get the user agent in Flask?

1 Answer

0 votes
by
We can use the request object to get the User-Agent in Flask.

Use the below-mentioned code for the same.

from flask import Flask

from flask import request

 

app = Flask(__name__)

 

@app.route("/")

def index():

    val = request.args.get("var")

    user_agent = request.headers.get('User-Agent')   

 

    response = """

    <p>

    Hello, World! {}

    <br/>

    You are accessing this app with {}

    </p>

    """.format(val, user_agent)    

return response

if __name__=="__main__":

    app.run(host="0.0.0.0", port=8080)

Once you run this code and navigate to the required URL using the Chrome browser, you will see the result, as shown in the below image.

Result in Chrome

The result in Firefox will look as shown in the below image.

Result in Firefox
...