How do I get the user agent with Flask?

前端 未结 5 666
轻奢々
轻奢々 2020-12-02 09:35

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.

5条回答
  •  庸人自扰
    2020-12-02 10:15

    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

提交回复
热议问题