Grabbing user's agent and OS type

Νικόλαος Κούρας nikos.kouras at gmail.com
Fri Mar 11 12:27:42 EST 2011


On 11 Μαρ, 18:39, Tim Chase <python.l... at tim.thechases.com> wrote:
> On 03/11/2011 09:59 AM, Νικόλαος Κούρας wrote:
>
>
>
>
>
>
>
>
>
> > ******************
> > 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

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)



More information about the Python-list mailing list