浏览器检测Python / mod_python?

发布于 2024-08-10 12:15:50 字数 305 浏览 3 评论 0原文

我想在数据库中保留一些有关用户和位置的统计信息。例如,我想存储“Mozilla”、“Firefox”、“Safari”、“Chrome”、“IE”等......以及版本,可能还有操作系统。

我试图从Python中找到这个字符串;

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.14) Gecko/2009090216 Ubuntu/9.04 (jaunty) Firefox/3.0.14

有没有一种有效的方法来使用 Python 或 mod_python 来检测 http 用户代理/浏览器?

I want to keep some statistics about users and locations in a database. For instance, I would like to store "Mozilla","Firefox","Safari","Chrome","IE", etc... as well as the versions, and possibly the operating system.

What I am trying to locate from Python is this string;

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.14) Gecko/2009090216 Ubuntu/9.04 (jaunty) Firefox/3.0.14

Is there an efficient way to use Python or mod_python to detect the http user agent/browser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

や三分注定 2024-08-17 12:15:50

HTTP_USER_AGENT 包含此信息,并将在您的应用程序使用的环境变量中传递。在 mod_python 中,这表示为:

def my_request_handler(req):
    req.add_common_vars()
    agent = req.subprocess_env.get("HTTP_USER_AGENT")

    # `agent` now contains the full user agent of the browser, or None

这是一个基本的 CGI 事物,但这就是 mod_python 为您提供的方式。

HTTP_USER_AGENT contains this information, and will be passed in the environment variables your application uses. In mod_python, this is expressed as:

def my_request_handler(req):
    req.add_common_vars()
    agent = req.subprocess_env.get("HTTP_USER_AGENT")

    # `agent` now contains the full user agent of the browser, or None

It's a basic CGI thing, but this is how mod_python gives it to you.

辞旧 2024-08-17 12:15:50

杰德·史密斯建议的方法有效,但我确信还有更简单的方法。

req.headers_in 变量包含所有标头信息,您可以通过调用 mod_python 轻松访问用户代理:

req.headers_in[ 'User-Agent' ]

使用这种方法。

The method suggested by Jed Smith works, but I was sure there was a simpler way.

The req.headers_in variable contains all the header info, and you can easily access the user agent using mod_python by calling:

req.headers_in[ 'User-Agent' ]

It is not necessary to call req.add_common_vars() when using this method.

滥情哥ㄟ 2024-08-17 12:15:50

如果您使用Django框架,您会得到这样的用户代理。

request.META['HTTP_USER_AGENT']

非常好的插件httpagentparser提取每个细节并将其放入字典中。

通过 pip 安装工作

pip install httpagentparser

希望这会有所帮助...我用谷歌搜索了大约 30 分钟,直到找到有用的东西:)

Ron

If you are using the Django-Framework you get the user agent like this

request.META['HTTP_USER_AGENT']

The very nice plugin httpagentparser extracts every detail and puts it a dictionary.

Installation works via pip

pip install httpagentparser

Hope this helps... I googled about 30 min until I found something useful :)

Ron

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文