Python / mod_python browser detection?

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

What I'm trying to find from Python is a 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 http agent / http browser?

+4
source share
3 answers

HTTP_USER_AGENT contains this information and will be passed in the environment variables used by your application. 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 

This is the basic CGI thing, but this is how mod_python provides it to you.

+3
source

The method suggested by Jed Smith works, but I was sure there was an easier way.

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

 req.headers_in[ 'User-Agent' ] 

There is no need to call req.add_common_vars() when using this method.

+2
source

If you use Django -Framework, you get such a user agent

 request.META['HTTP_USER_AGENT'] 

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

Installation works through pip

 pip install httpagentparser 

Hope this helps ... I searched Google for about 30 minutes until I found something useful :)

Ron

+2
source

All Articles