Grabbing user's agent and OS type

  • Thread starter Íéêüëáïò Êïýñáò
  • Start date
Í

Íéêüëáïò Êïýñáò

Hello,

I use

os.environ["HTTP_USER_AGENT"] and is very convenient to retrieve the
user's agent type

but how could we also retrieve the user's OS type?

OS type and agent type and version do appear in the same string.
I somehow have to grab the 'OS' type(Windows) and the user's agent/
version(Chrome 11)


The string is:
Code:
Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/534.23 (KHTML, like
Gecko) Chrome/11.0.686.0 Safari/534.23

How am i supposed to filter the string since the subsparts of the
string isn't always the same and maybe also in different positions
within the each time different returned string?

For example from he above string i only need to know that the OS type
is 'Windows' and that the agent type is 'Chrome 11'

But those strings vary in each case.

If it cant be done by filtering the value of
os.environ["HTTP_USER_AGENT"] is there any other easier way that i can
retrive those values?
 
S

Steven D'Aprano

Hello,

I use

os.environ["HTTP_USER_AGENT"] and is very convenient to retrieve the
user's agent type

but how could we also retrieve the user's OS type? 'posix'


OS type and agent type and version do appear in the same string. I
somehow have to grab the 'OS' type(Windows) and the user's agent/
version(Chrome 11)


The string is:
Code:
Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/534.23 (KHTML, like
Gecko) Chrome/11.0.686.0 Safari/534.23

How am i supposed to filter the string since the subsparts of the string
isn't always the same and maybe also in different positions within the
each time different returned string?

useragent = "Mozilla/5.0 blah blah blah"
agent = useragent.lower()
if "mozilla" in agent:
print "user-agent claims to be Mozilla compatible"
if "chrome" in agent:
print "user-agent claims to be Chrome compatible"
if "safari" in agent:
print "user-agent claims to be Safari compatible"
if "python" in agent:
print "user-agent claims to be a Python program, module or script"
if "lynx" in agent:
print "user-agent claims to be the Lynx browser"
if "links" in agent:
print "user-agent claims to be the Links browser"
if "windows" in agent:
print "user-agent claims that the operating system is Windows"
# and so on
print "remember that user-agents can, and frequently do, lie"


I think you give the user-agent string too much credit. Despite what some
people think, including some browser developers, it's a free-form string
and can contain anything the browser wants. There's no guarantee that
fields will appear in a particular order, or even appear at all. If
you're doing feature detection by parsing the UA string, you're in a
state of sin.
 
Î

Îικόλαος ΚοÏÏας

Thanks a lot Steven!

The following code worked like a charm!

******************
agent = os.environ['HTTP_USER_AGENT']

# determination of user browser
agent = agent.lower()
if 'chrome' in agent:
agent = 'Chrome'
if 'firefox' in agent:
agent = 'Firefox'
if 'opera' in agent:
agent = 'Opera'
if 'safari' in agent:
agent = 'Safari'
if 'msie' in agent:
agent = 'IE'
***************

I just want to have an idea of what browser a guest is using when
hitting my webpage.

http://www.superhost.gr/?show=log

now includes also that filed in the logging process.

But if i wanted the OS also?

sys.platform is for retrieving the host OS type that the script is
running and not the guest OS type.
 
B

Benjamin Kaplan

2011/3/11 Íéêüëáïò Êïýñáò said:
Thanks a lot Steven!

The following code worked like a charm!

******************
agent = os.environ['HTTP_USER_AGENT']

# determination of user browser
agent = agent.lower()
if 'chrome' in agent:
       agent = 'Chrome'
if 'firefox' in agent:
       agent = 'Firefox'
if 'opera' in agent:
       agent = 'Opera'
if 'safari' in agent:
       agent = 'Safari'
if 'msie' in agent:
       agent = 'IE'
***************

For compatibility reasons (and because it's also WebKit based), Chrome
also identifies itself as Safari. Just look at the User-Agent string
you posted in the original message:

Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/534.23 (KHTML, like
Gecko) Chrome/11.0.686.0 Safari/534.23



Because of the order of your checks here (and because you're using if
instead of elif), Chrome will be reported as Safari. I suppose you can
fix it for now, but what happens when another browser comes along and
claims to be MSIE-compatible?

Notice that Chrome, like every other browser, introduces itself as
Netscape 5 (Mozilla). After giving you the platform, Chrome correctly
identifies itself as using WebKit as the rendering engine but then
name-drops KHTML (which Webkit was forked from) and Gecko (Firefox's
rendering engine) so that it will get the version of sites optimized
for those engines (if they haven't identified webkit yet) before
identifying itself as Chrome and then Safari (for the same reason).
Like Steven said, User-Agent strings aren't all that useful for
identifying the browser.
I just want to have an idea of what browser a guest is using when
hitting my webpage.

http://www.superhost.gr/?show=log

now includes also that filed in the logging process.

But if i wanted the OS also?

I don't think anyone fakes the OS, unless some website is being really
stupid and claims "Windows-only" instead of "IE-only". Just check for
the OS name.
 
T

Tim Chase

******************
agent = os.environ['HTTP_USER_AGENT']

# determination of user browser
agent = agent.lower()
if 'chrome' in agent:
agent = 'Chrome'
if 'firefox' in agent:
agent = 'Firefox'
if 'opera' in agent:
agent = 'Opera'
if 'safari' in agent:
agent = 'Safari'
if 'msie' in agent:
agent = 'IE'
***************

I just want to have an idea of what browser a guest is using when
hitting my webpage.

http://www.superhost.gr/?show=log

now includes also that filed in the logging process.

I now have a sudden urge to use Firefox's User-Agent Switcher
add-on to change my UA string to something like "Chrome Firefox
MSIE Opera Safari Mozilla Lynx Links Dillo Win32 Windows Linux
OpenBSD Mac OS X" and browse your site :)

-tkc
 
Í

Íéêüëáïò Êïýñáò

******************
agent = os.environ['HTTP_USER_AGENT']
# determination of user browser
agent = agent.lower()
if 'chrome' in agent:
   agent = 'Chrome'
if 'firefox' in agent:
   agent = 'Firefox'
if 'opera' in agent:
   agent = 'Opera'
if 'safari' in agent:
   agent = 'Safari'
if 'msie' in agent:
   agent = 'IE'
***************
I just want to have an idea of what browser a guest is using when
hitting my webpage.

now includes also that filed in the logging process.

I now have a sudden urge to use Firefox's User-Agent Switcher
add-on to change my UA string to something like "Chrome Firefox
MSIE Opera Safari Mozilla Lynx Links Dillo Win32 Windows Linux
OpenBSD Mac OS X" and browse your site :)

-tkc

ROFLMAO !!!

I guess the previous code that i corrected after seeing Benjamin's
response:

==================
# determination of user browser
agent = agent.lower()
if 'chrome' in agent:
agent = 'Chrome'
elif 'firefox' in agent:
agent = 'Firefox'
elif 'opera' in agent:
agent = 'Opera'
elif 'safari' in agent:
agent = 'Safari'
elif 'msie' in agent:
agent = 'Explorer'
else:
agent = 'Other'
====================

would still be failing since it seems that it is a matter of order
what response will be presented.

So if you use firefox for browser with the UA string tweaker on with a
string of "Chrome Firefox
MSIE Opera Safari Mozilla Lynx Links Dillo Win32 Windows Linux OpenBSD
Mac OS X" and browse my site the result will always be 'Chrome'
since the 1st match will always be TRUE and nothing else get
checked. :)

But in a real example if a UA string contains the substring 'Chrome'
then the browser must be Chrome(there is no point containing the
substring 'Firefox' in UA when the browser is 'Chrome', unlike Mozilla
which is a common subtring to both of the browsers UA)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top