Determine whether the client is mobile, operating system, browser, etc. based on useragent

Source: Internet
Author: User

The user Agent Chinese name is the users proxy, is part of the HTTP protocol, is part of the header domain, the user agent is also referred to as UA. It is a special string header that provides access to the Web site with information such as the type and version of the browser you are using, the operating system and version, the browser kernel, and so on. Through this logo, users visit the site can display a different layout to provide users with better experience or information statistics, such as mobile phone access to Google and computer access is not the same, these are Google according to the visitor's UA to judge. The UA can be disguised. (wiki)


Analyzing the user-agent of the browser we can collect information about the client: whether it is mobile phone, operating system, browser, etc.

Effect: http://1.billo.sinaapp.com/


The following is a function to get the JSON format UA

UA is a user-agent field in string format


Import Urllibimport urllib2import jsondef Getua (UA):    ua = Urllib.quote (UA)    url = "/http/ Www.useragentstring.com/?uas=%s&getJSON=all "%ua    keys = {            " Host ":" www.useragentstring.com ",            " User-agent ":" mozilla/5.0 (Windows NT 6.4; WOW64; rv:35.0) gecko/20100101 firefox/35.0 "        }    req = Urllib2. Request (URL) for    key in keys:        Req.add_header (Key,keys[key])    j = Urllib2.urlopen (req). Read ()    



request.headers[' user-agent ') can return a string of user-agent

Then through the useragentstring This site API can get a parsed user-agent JSON




identify if the mobile client has a "mobile" field in the user-agent as long as it is recognized

system Identify

If it is a Windows system, the os_name that resolves user-agent get JSON is that Windows is the kernel version


The current mainstream system identity corresponds to the following:

"Windows NT 6.4": "Windows 10",

"Windows NT 6.3": "Windows 8.1",

"Windows NT 6.2": "Windows 8",

"Windows NT 6.0": "Windows Vista",

"Windows NT 6.1": "Windows 7",

"Windows NT 5.1": "Windows XP",

Create a dictionary to traverse these identities to



Useragentstring returned Agent_name does not recognize part of the browser and only displays the kernel


Browser recognition

The standard that IE browser judges is "MSIE" field

The UC browser is judged by the "UCWEB" field and the "Ucbrowser" field

Opera browser is judged by the "opera" field

Sogou browser's criteria are "SE","Metasr" field

Sogou Mobile browser's criteria for judging is " Sogoumobilebrowser " Field

The standard of the Chrome browser is " Chrome " Field

the criteria for the Safari browser are " Safari " Field

The standard for Firefox is " Firefox " Field

QQ Browser include "Tencenttraveler" or "Qqbrowser " Field

The standard of the browser is "360SE" field

The TheWorld browser is judged by the "the World " field

The standard of roaming browser is "Maxthon" Field

Create a dictionary to store the main browser type :

Browser = {

"Sogoumobilebrowser": " Sogou Mobile browser ",

"Ucbrowser": "UC browser ",

"UCWEB": "UC browser ",

"Opera": "Opera browser ",

"Qqbrowser": "QQ browser ",

"Tencenttraveler": "QQ browser ",

"Metasr": " Sogou browser ",

"360SE": " Web browser ",

"The World": " windows of the Worlds browser ",

"Maxthon": " surfing the browser ",

}

Traverse these identities to

An example of judging the operating system and browser type based on useragent

#coding: Utf-8import tornado.webimport sysimport urllibimport urllib2import jsonimport os.pathfrom tornado.httpclient Import Asynchttpclientclass Welcomehandler (tornado.web.RequestHandler): Def get (self): UA = Self.request.headers        [' user-agent '] IsMobile = "You are not a mobile client" if Ua.find ("mobile") = =-1 Else "You are a mobile client" uainfor = Self.getua (UA) Self.render (' index.html ', User=self.current_user, head=self.request.headers[' user        -agent '], H=ismobile, ua=uainfor) def getua (self, UA):        Ua_url = Urllib.quote (UA) # goto URL Encoded URL = "Http://www.useragentstring.com/?uas=%s&getJSON=all"% Ua_url Keys = {"Host": "Www.useragentstring.com", "user-agent": "mozilla/5.0 (Windows NT 6.4; WOW64; rv:35.0) gecko/20100101 firefox/35.0 "} req = Urllib2. Request (URL) for key in Keys:req.add_header (Key,keys[key])       j = Urllib2.urlopen (req). Read () j = json.loads (j) Windows = {"Windows NT 6.4": "Windows 10", "Windows NT 6.3": "Windows 8.1", "Windows NT 6.2": "Windows 8", "Windows NT 6.0": "Windows 8", "Wi Ndows NT 6.1 ":" Windows 7 "," Windows NT 5.1 ":" Windows XP ",} Browser = {" Sogoumobilebrowser ":"        Sogou Mobile Browser "," Ucbrowser ":" UC Browser "," UCWEB ":" UC Browser "," Opera ":" Opera browser "," Qqbrowser ":" QQ Browser ", "Tencenttraveler": "QQ Browser", "Metasr": "Sogou Browser", "360SE": "360 Browser", "The World": "Window Browser", "Maxth On ":" Roaming Browser ",} for name in Windows:if ua.find (name)! = -1:j[" Os_name "] = Windows        [Name] for name in Browser:if ua.find (name)! = -1:j["Agent_name"] = Browser[name] return jsettings = {"Template_path": Os.path.join (Os.path.dirname (__file__), "Templates"), "Debug": True, }# application should be An instance of ' Tornado.web.Application ', # and don ' t wrap it with ' sae.create_wsgi_app ' application = Tornado.web.Applicat Ion ([(R '/', Welcomehandler),], **settings)



The effect is as follows:




Determine whether the client is mobile, operating system, browser, etc. based on useragent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.