473,508 Members | 3,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting Browsers in Python

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
Jul 18 '05 #1
4 14165
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?

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

Jul 18 '05 #2
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.
Jul 18 '05 #3
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>


Jul 18 '05 #4
> 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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2675
by: Jay | last post by:
I'm sure this is a really dumb question, but how do you detect a variable type in Python? For example, I want to know if the variable "a" is a list of strings or a single string. How do I do...
10
4094
by: Frances Del Rio | last post by:
pls, why is this not working? <SCRIPT language=JavaScript type="text/javascript"> var br = '<SCRIPT language=Javascript' br += 'src="js_pop.js" type="text/javascript">' br += '</SCRIPT>' var...
20
7877
by: Mandy Memphis | last post by:
If I perform a mousedown within a document, move the mouse outside the browser window, and then release the mouse button, the document.onmouseup event does not fire. Is there any way to detect a...
13
1778
by: ima | last post by:
I've been studying a great book on css and now that I've put together a few pages using it, I'm finding out that my book needed one more very important chapter. Introducing css in the real world. ...
79
3706
by: VK | last post by:
I wandering about the common proctice of some UA's producers to spoof the UA string to pretend to be another browser (most often IE). Shouldn't it be considered as a trademark violation of the...
2
1906
by: Jeremy S. | last post by:
In an ASP.NET 1.1 Web application, how can I detect - with *reasonable* accuracy - whether any particular browser is a downlevel browser? My objective is to insert a different menu (different...
7
2984
by: Nathan Sokalski | last post by:
I want to make sure I am doing a browser detection that will work once IE7 is released. My current detection statement (written using VB.NET) is: If Me.Request.Browser.Browser.ToUpper() = "IE"...
1
4138
by: Grzegorz Klimsa | last post by:
Hi ! I have a problem wiht detecting resolution of client web browser I prepare several files of css style for different browsers and different resolutions, (such as : Style_1024x768.css ;...
15
4192
by: RobG | last post by:
When using createEvent, an eventType parameter must be provided as an argument. This can be one of those specified in DOM 2 or 3 Events, or it might be a proprietary eventType. My problem is...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.