views:

2000

answers:

7

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

+2  A: 

You can't. Best you can get is having someone collect a database of them. You can do that yourself or there are some in Internet.

iny
A: 

More to the point, depending on browser OSes and versions for capability detection is not a good strategy. What problem are you trying to solve?

DDaviesBrackett
We have a simple ticket reporter form for our web app. And we frequently ask users what's the broswer/OS etc. So we might just detect it beforehand and ask the user to confirm.Well, if thats not so trivial we will just dump the user-agent but still need to ask questions, as user might be using a different browser to submit the ticket.
Shekhar
You may wish to just let Python render the form and use Javascript to change the input field values to those of the current OS and what ever else you wish.
NerdyNick
+1  A: 

However if you wish to parse all this on the Python side you can use the XML/INI files provided at http://browsers.garykeith.com/downloads.asp to do lookups on the user agent. This is the same file that is used in php's get_browser() function.

NerdyNick
+1  A: 

Th Browser Cap Parser should work. It may be a bit slow though..

Chirayu Patel
A: 

Is there any alternative to get this in Rails?

Millisami
+4  A: 

Answering my own question ;)

Finally I decided to go by suggestion#1 i.e. write your own. And I am happy with the outcome. Please feel free to use/modify/send me patch etc.

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

Shekhar
+1  A: 

UASparser for Python by Hicro Kee. Auto updated datafile and cache from remote server with version checking.