repalviglator repalviglator - 3 months ago 70
Python Question

How do I get the user agent with Flask?

I'm trying to get access to the user agent with Flask, but I either can't find the documentation on it, or it doesn't tell me.

Answer
from flask import request
request.headers.get('User-Agent')

You can also use the request.user_agent object which contains the following attributes which are created based on the useragent string:

  • platform (windows, linux, macos, etc.)
  • browser (chrome, firefox, msie, etc.)
  • version
  • language
  • string (== request.headers.get('User-Agent'))
Comments