Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing HTTP User-Agent string

What is the best method to parse a User-Agent string in Python to reliably detect

  1. Browser
  2. Browser version
  3. OS

Or perhaps any helper library that does it

like image 443
Shekhar Avatar asked May 29 '09 18:05

Shekhar


People also ask

What is user agent parsing?

This information, gleaned directly from the User-Agent string itself (a process known as User-Agent parsing) typically includes browser, web rendering engine, operating system and device. Deeper information can be returned when the User-Agent string is mapped to an additional set of data about the underlying device.

What is a user agent string?

A browser's User-Agent string (UA) helps identify which browser is being used, what version, and on which operating system. When feature detection APIs are not available, use the UA to customize behavior or content to specific browser versions.

What does a user agent string contain?

Described in the HTTP standard, the User-Agent string contains a number of tokens that refer to various aspects of the request, including the browser's name and version, rendering engine, device's model number, operating system and its version, etc.

Where is the user agent string?

The User-Agent (UA) string is contained in the HTTP headers and is intended to identify devices requesting online content. The User-Agent tells the server what the visiting device is (among many other things) and this information can be used to determine what content to return.


1 Answers

I finally decided to write my own, and I am happy with the outcome. Please feel free to use/modify/send me patches, etc.

It's here: http://pypi.python.org/pypi/httpagentparser

Usage example:

>>> import httpagentparser >>> s = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.9 (KHTML, like Gecko) \         Chrome/5.0.307.11 Safari/532.9" >>> print(httpagentparser.simple_detect(s)) ('Linux', 'Chrome 5.0.307.11') >>> print(httpagentparser.detect(s)) {'os': {'name': 'Linux'},  'browser': {'version': '5.0.307.11', 'name': 'Chrome'}}  >>> s = "Mozilla/5.0 (Linux; U; Android 2.3.5; en-in; HTC_DesireS_S510e Build/GRJ90) \         AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1" >>> print(httpagentparser.simple_detect(s)) ('Android Linux 2.3.5', 'Safari 4.0') >>> print(httpagentparser.detect(s)) {'dist': {'version': '2.3.5', 'name': 'Android'}, 'os': {'name': 'Linux'}, 'browser': {'version': '4.0', 'name': 'Safari'}} 
like image 175
Shekhar Avatar answered Oct 02 '22 18:10

Shekhar