473,769 Members | 6,034 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 14205
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_AGEN T", "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_AGEN T", "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('window s 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
2700
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 this?
10
4122
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 op = '<SCRIPT language=Javascript' op += 'src="js_pop-op.js" type="text/javascript">' op += '</SCRIPT>' if (navigator.userAgent.indexOf('Opera') != -1 ) {
20
7938
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 mouseup event outside the document? Also, how can I get the relative coordinates of the cursor while it is outside the browser? Thank you.
13
1800
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. Obviously, there are a LOT of older browsers still out there that don't support css and some newer one's that still don't support it very well. Knowing that, I have prepared a second set of pages using table layouts for those browsers. It...
79
3794
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 relevant name owner? If I make a whisky and call it "Jack Daniels", I most probably will have some serious legal problems. "Mozilla" partially appeared because NCSA stopped them from using "Mosaic" in the UA string. Is it some different...
2
1914
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 User Control containing a different menu) for downlevel vs uplevel browsers. This is very important as I have a great DHTML menu - but on many MACs, it doesn't work even though the vendor claims to cater to downlevel browsers. So I'm going to...
7
3003
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" Then 'Code to use for Internet Explorer browsers Else 'Code to use for browsers other than Internet Explorer End If
1
4153
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 ; Styleff_1024x768.css; Style_1280x1024.css ; Styleff_1280x1024.css ) I want to set css style file depends on resolution browser I wrote this script to detect type of browsers, but i don't have any idea
15
4233
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 testing for support of particular eventTypes - the DOM 2 Events Interface DocumentEvent says that if the eventType is not supported, it throws a DOM exception. This makes testing rather tough - if you try something like: if (document &&...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
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 we have to send another system

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.