Detecting Browsers in Python
Gerhard Häring
gh at ghaering.de
Tue Nov 18 11:50:00 EST 2003
Daniel Orner wrote:
> 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.
More information about the Python-list
mailing list