问题:如何使用Flask获取用户代理?

我试图通过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.


回答 0

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

您还可以使用request.user_agent包含以下属性的对象,这些属性是基于useragent字符串创建的:

  • 平台(Windows,Linux,MacOS等)
  • 浏览器(chrome,firefox,msie等)
  • 语言
  • 字串(== request.headers.get('User-Agent')
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'))

回答 1

flask.request.user_agent.string
flask.request.user_agent.string

回答 2

如果您使用

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

您可能会得到:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 45.0.2454.101 Safari / 537.36

如果您使用

request.user_agent

您可能会像这样:

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

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

回答 3

UA通常不包含语言。如果要在浏览器中设置语言,可以使用

request.accept_languages

它会给您语言列表。例如

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

要访问第一个值,您可以使用

request.accept_languages[0][0]

这将导致字符串

'en-US'

有关“ accept_language”标头的详细信息:https ://www.w3.org/International/questions/qa-lang-priorities

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


回答 4

这个问题请求更多信息。该库似乎适合从烧瓶中收集大量信息的清单,并具有示例调用以从应用程序上下文中获取此信息。

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

使用情况将以以下格式存储:

[
    {
            '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,
    },
    {
        ....
    }
]

这是图书馆中收集数据的地方之一:

https://github.com/ashcrow/flask-track-usage/blob/master/src/flask_track_usage/ init .py在第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
    }

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
    }

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。