Detecting Browsers in Python
Fredrik Lundh
fredrik at pythonware.com
Tue Nov 18 11:48:35 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?
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>
More information about the Python-list
mailing list