Detecting Browsers in Python

D

Daniel Orner

Does anyone know of a simple way to have a Python script find out what
browser is accessing it? After a web search the only thing I found to
do this is Zope, but the system I'm programming doesn't use Zope and
I'm not really interested in installing it just for this minor detail.
Is there another way?

Thanks!

--Daniel Orner
 
J

Jay O'Connor

Daniel said:
Does anyone know of a simple way to have a Python script find out what
browser is accessing it? After a web search the only thing I found to
do this is Zope, but the system I'm programming doesn't use Zope and
I'm not really interested in installing it just for this minor detail.
Is there another way?

Thanks!

--Daniel Orner

Going purely from memory, "USER-AGENT" or something similar entry in the
os.environ dictionary will have a string identifying the browser. It's
only partially reliable because some browsers will pretend to be another
browser and some, I think, will allow the user to specify what they want
to pretend to be.

Anyway, play around with it and see if will give you what you need

Take care,
Jay
 
F

Fredrik Lundh

Daniel said:
Does anyone know of a simple way to have a Python script find out what
browser is accessing it? After a web search the only thing I found to
do this is Zope, but the system I'm programming doesn't use Zope and
I'm not really interested in installing it just for this minor detail.
Is there another way?

if you're using a CGI script, you can (usually) find the user agent
in the HTTP_USER_AGENT environment variable; use os.environ to
get the current value.

#!/usr/bin/python -u

import os

print "Content-type: text/plain"
print

print "User agent:", os.environ.get("HTTP_USER_AGENT", "unknown")

if you're using some other framework, you may be able to get the
user agent by looking at the HTTP headers; check the framework
docs for details.

for more info on CGI environment variables, see:

http://hoohoo.ncsa.uiuc.edu/cgi/env.html
http://httpd.apache.org/docs/env.html
http://httpd.apache.org/docs/misc/FAQ.html#cgi-spec
(etc)

</F>
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

Daniel said:
Does anyone know of a simple way to have a Python script find out what
browser is accessing it? After a web search the only thing I found to
do this is Zope, but the system I'm programming doesn't use Zope and
I'm not really interested in installing it just for this minor detail.
Is there another way?

Interpret the User-Agent field.

It depends on your system how to acess the User-Agent field. If you're
using a CGI script, try this one:

import os
print "Content-type: text/plain"
print
print "User-Agent:", os.environ.get("HTTP_USER_AGENT", "N/A")

To see what information you can access from a web request, try this CGI
script:

import cgi
cgi.test()

For actually interpreting this field, a Google search for "python parse
HTTP_USER_AGENT" would be my starting point.

HTH,

-- Gerhard

PS: In case you're using Apache, you can use modules like mod_rewrite to
redirect users to specialized pages, depending on the reported User-Agent.
 
D

Dave Brueck

Does anyone know of a simple way to have a Python script find out what
browser is accessing it? After a web search the only thing I found to
do this is Zope, but the system I'm programming doesn't use Zope and
I'm not really interested in installing it just for this minor detail.
Is there another way?

(I assume you mean that the script the browser is accessing is a CGI script)

Most browsers include a "User-Agent" in the HTTP request they make to a server.
Users can override these, but few people do, so you can semi-reliably detect
the browser that way.

I sometimes need to make sure a browser is running on "new enough" Windows, so
I use this:

ua = ua.lower()

if ua.find('win') != -1 and ua.find('win16') == -1 and \
ua.find('windows nt 4') == -1 and ua.find('winnt4') == -1:
# platform is Windows
else:
# non-Windows or old Windows

As for browser vendor, this is the pseudocode I use:

if ua.find('opera') != -1:
# opera
elif ua.find('gecko') != -1:
# gecko (moz/ns)
elif ua.find('msie') != -1:
# Most likely really is IE
else:
# somebody else

This works for what I need because usually I'm just trying to acertain if the
browser is IE on a newer Windows box, but you may need additional checks
(Google can turn up huge lists of all known default User-Agent strings so it's
fairly easy to come up with a good test suite).

-Dave
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top