Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the user agent with Flask?

Tags:

python

flask

People also ask

What is user agent API?

User Agent API is a simple but essential tool that provides an easy way to detect devices like mobile phones, tablets and their capabilities by parsing (browser/HTTP) user agent strings. User Agent API lets you reliably identify if the user agent is; a mobile phone, a tablet or a PC based device, or.

What is user agent in python?

user_agents is a Python library that provides an easy way to identify/detect devices like mobile phones, tablets and their capabilities by parsing (browser/HTTP) user agent strings. The goal is to reliably detect whether: User agent is a mobile, tablet or PC based device.

What is a user agent?

A user agent is any software that retrieves and presents Web content for end users or is implemented using Web technologies. User agents include Web browsers, media players, and plug-ins that help in retrieving, rendering and interacting with Web content.


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'))

Note: As of werkzeug 2.0, the parsed data of request.user_agent has been deprecated; if you want to keep getting details you need to use a custom UserAgent implementation and set it as user_agent_class on a custom Request subclass, which is set as request_class on the Flask instance (or a subclass).

Here's an example implementation that uses ua-parser:

from ua_parser import user_agent_parser
from werkzeug.user_agent import UserAgent
from werkzeug.utils import cached_property


class ParsedUserAgent(UserAgent):
    @cached_property
    def _details(self):
        return user_agent_parser.Parse(self.string)

    @property
    def platform(self):
        return self._details['os']['family']

    @property
    def browser(self):
        return self._details['user_agent']['family']

    @property
    def version(self):
        return '.'.join(
            part
            for key in ('major', 'minor', 'patch')
            if (part := self._details['user_agent'][key]) is not None
        )

flask.request.user_agent.string

If you use

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

you may get: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

If you use

request.user_agent

you may get like this:

  • user_agent.platform: windows
  • user_agent.browser: chrome
  • user_agent.version: 45.0.2454.101
  • user_agent.language: None
  • user_agent.string: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36

UA usually does not contain language. If you want to get the language set in browser, you may use

request.accept_languages

It'll give you list of languages. E.g.

LanguageAccept([('en-US', 1), ('en', 0.5)])

To access the first value, you may use

request.accept_languages[0][0]

which will result in string

'en-US'

Detailed information about 'accept_language" header: https://www.w3.org/International/questions/qa-lang-priorities


The question begs for a lot more information. This library seems to fit the bill of collecting a lot of information out of flask, and has example calls to getting this information out of the application context.

https://pythonhosted.org/Flask-Track-Usage/

Usage gets stored in this format:

[
    {
            'url': str,
            'user_agent': {
                'browser': str,
                'language': str,
                'platform': str,
                'version': str,
            },
            'blueprint': str,
            'view_args': dict or None
            'status': int,
            'remote_addr': str,
            'xforwardedfor': str,
            'authorization': bool
            'ip_info': str or None,
            'path': str,
            'speed': float,
            'date': datetime,
    },
    {
        ....
    }
]

Here is one of the places in the library where the data is collected:

https://github.com/ashcrow/flask-track-usage/blob/master/src/flask_track_usage/init.py around line 158

    data = {
        'url': ctx.request.url,
        'user_agent': ctx.request.user_agent,
        'server_name': ctx.app.name,
        'blueprint': ctx.request.blueprint,
        'view_args': ctx.request.view_args,
        'status': response.status_code,
        'remote_addr': ctx.request.remote_addr,
        'xforwardedfor': ctx.request.headers.get(
            'X-Forwarded-For', None),
        'authorization': bool(ctx.request.authorization),
        'ip_info': None,
        'path': ctx.request.path,
        'speed': float(speed),
        'date': int(time.mktime(current_time.timetuple())),
        'content_length': response.content_length,
        'request': "{} {} {}".format(
            ctx.request.method,
            ctx.request.url,
            ctx.request.environ.get('SERVER_PROTOCOL')
        ),
        'url_args': dict(
            [(k, ctx.request.args[k]) for k in ctx.request.args]
        ),
        'username': None,
        'track_var': g.track_var
    }