# -*- coding: utf-8 -*-"""werkzeug.useragents~~~~~~~~~~~~~~~~~~~This module provides a helper to inspect user agent strings. This moduleis far from complete but should work for most of the currently availablebrowsers.:copyright: 2007 Pallets:license: BSD-3-Clause"""import reclass UserAgentParser(object):"""A simple user agent parser. Used by the `UserAgent`."""platforms = ((" cros ", "chromeos"),("iphone|ios", "iphone"),("ipad", "ipad"),(r"darwin|mac|os\s*x", "macos"),("win", "windows"),(r"android", "android"),("netbsd", "netbsd"),("openbsd", "openbsd"),("freebsd", "freebsd"),("dragonfly", "dragonflybsd"),("(sun|i86)os", "solaris"),(r"x11|lin(\b|ux)?", "linux"),(r"nintendo\s+wii", "wii"),("irix", "irix"),("hp-?ux", "hpux"),("aix", "aix"),("sco|unix_sv", "sco"),("bsd", "bsd"),("amiga", "amiga"),("blackberry|playbook", "blackberry"),("symbian", "symbian"),)browsers = (("googlebot", "google"),("msnbot", "msn"),("yahoo", "yahoo"),("ask jeeves", "ask"),(r"aol|america\s+online\s+browser", "aol"),(r"opera|opr", "opera"),("edge", "edge"),("chrome|crios", "chrome"),("seamonkey", "seamonkey"),("firefox|firebird|phoenix|iceweasel", "firefox"),("galeon", "galeon"),("safari|version", "safari"),("webkit", "webkit"),("camino", "camino"),("konqueror", "konqueror"),("k-meleon", "kmeleon"),("netscape", "netscape"),(r"msie|microsoft\s+internet\s+explorer|trident/.+? rv:", "msie"),("lynx", "lynx"),("links", "links"),("Baiduspider", "baidu"),("bingbot", "bing"),("mozilla", "mozilla"),)_browser_version_re = r"(?:%s)[/\sa-z(]*(\d+[.\da-z]+)?"_language_re = re.compile(r"(?:;\s*|\s+)(\b\w{2}\b(?:-\b\w{2}\b)?)\s*;|"r"(?:\(|\[|;)\s*(\b\w{2}\b(?:-\b\w{2}\b)?)\s*(?:\]|\)|;)")def __init__(self):self.platforms = [(b, re.compile(a, re.I)) for a, b in self.platforms]self.browsers = [(b, re.compile(self._browser_version_re % a, re.I))for a, b in self.browsers]def __call__(self, user_agent):for platform, regex in self.platforms: # noqa: B007match = regex.search(user_agent)if match is not None:breakelse:platform = Nonefor browser, regex in self.browsers: # noqa: B007match = regex.search(user_agent)if match is not None:version = match.group(1)breakelse:browser = version = Nonematch = self._language_re.search(user_agent)if match is not None:language = match.group(1) or match.group(2)else:language = Nonereturn platform, browser, version, languageclass UserAgent(object):"""Represents a user agent. Pass it a WSGI environment or a user agentstring and you can inspect some of the details from the user agentstring via the attributes. The following attributes exist:.. attribute:: stringthe raw user agent string.. attribute:: platformthe browser platform. ``None`` if not recognized.The following platforms are currently recognized:- `aix`- `amiga`- `android`- `blackberry`- `bsd`- `chromeos`- `dragonflybsd`- `freebsd`- `hpux`- `ipad`- `iphone`- `irix`- `linux`- `macos`- `netbsd`- `openbsd`- `sco`- `solaris`- `symbian`- `wii`- `windows`.. attribute:: browserthe name of the browser. ``None`` if not recognized.The following browsers are currently recognized:- `aol` *- `ask` *- `baidu` *- `bing` *- `camino`- `chrome`- `edge`- `firefox`- `galeon`- `google` *- `kmeleon`- `konqueror`- `links`- `lynx`- `mozilla`- `msie`- `msn`- `netscape`- `opera`- `safari`- `seamonkey`- `webkit`- `yahoo` *(Browsers marked with a star (``*``) are crawlers.).. attribute:: versionthe version of the browser. ``None`` if not recognized... attribute:: languagethe language of the browser. ``None`` if not recognized."""_parser = UserAgentParser()def __init__(self, environ_or_string):if isinstance(environ_or_string, dict):environ_or_string = environ_or_string.get("HTTP_USER_AGENT", "")self.string = environ_or_stringself.platform, self.browser, self.version, self.language = self._parser(environ_or_string)def to_header(self):return self.stringdef __str__(self):return self.stringdef __nonzero__(self):return bool(self.browser)__bool__ = __nonzero__def __repr__(self):return "<%s %r/%s>" % (self.__class__.__name__, self.browser, self.version)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。