Detecting mobile device User-Agent.

379 views
Skip to first unread message

Paolo

unread,
Aug 30, 2011, 5:05:42 PM8/30/11
to web.py
Hello!

I'm trying to develop a mobile version of my web site.
I think that for the moment it could be quite enough to just have a
specific CSS that is used whenever a User-Agent of a mobile device is
detected in the HTTP headers.

In order to do so, I would need to inspect each and every HTTP request
and look for a mobile user agent (maybe using a regexp as shown at
detectmobilebrowsers.com) and then, if detected, passing a flag to the
template render function to make it include the CSS.

So my question is: is there a way, a function, a point in the web.py
code where I can put my function that checks for the presence of the
User-Agent at each request indipendently from the URI requested..?

(And my second question would be: am I doing anything really dumb..?
In other words: is there a much simpler way to do so..?;)

Thanks in advance.

paolo

Jian Chang

unread,
Sep 2, 2011, 12:14:06 AM9/2/11
to we...@googlegroups.com
web.ctx.env['HTTP_USER_AGENT']
define a hook function for every request?
or use decorator for every processor

2011/8/31 Paolo <skyp...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "web.py" group.
To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.


W. Martin Borgert

unread,
Sep 2, 2011, 3:27:35 AM9/2/11
to we...@googlegroups.com
On 2011-08-30 14:05, Paolo wrote:
> So my question is: is there a way, a function, a point in the web.py
> code where I can put my function that checks for the presence of the
> User-Agent at each request indipendently from the URI requested..?

In my web application I have a decorator around all GETs and
POSTs. In this decorator I check e.g. for HTTP_ACCEPT_LANGUAGE
and do permission checks. Maybe this is a good place to check
the user agent as well.

If you happen to have a login page, you could also only check
there and than store the CSS file name in the session.

> (And my second question would be: am I doing anything really dumb..?
> In other words: is there a much simpler way to do so..?;)

Sounds fine, at least I'm planning to do the same dumb thing.

I wonder, what is the right check for a mobile browser? The most
important thing is screen size, right?

Paolo

unread,
Sep 2, 2011, 4:40:24 AM9/2/11
to web.py
Thanks Jian, an hook function was just what I needed.

On Sep 2, 9:27 am, "W. Martin Borgert" <deba...@debian.org> wrote:
> I wonder, what is the right check for a mobile browser? The most
> important thing is screen size, right?

There are indeed several details regarding the capabilities of each
device...
Apart from following the W3C best practices on mobile web development
(http://www.w3.org/TR/2010/REC-mwabp-20101214/) you can use WURFL
(http://wurfl.sourceforge.net/) with its Python bindings (http://
pypi.python.org/pypi/pywurfl/) which provides all the capabilities
associated with the device that performed the request. And maybe there
are some HTML5 tags that could help...

(But, IMHO, nowadays there are only few classes of devices (say 4 or
5), with "standard" screen sizes... I don't know people who browse the
web using Nokia phones of 5 years ago... so I won't use WURFL. However
it depends also on the type of site, on its purpose and contents...)

Ben Corneau

unread,
Sep 2, 2011, 12:02:29 PM9/2/11
to web.py
I use mobile.sniffer for detection. http://pypi.python.org/pypi/mobile.sniffer.
Although I think it's actually using WURFL.

it makes it easy

from mobile.sniffer.detect import detect_mobile_browser
mobile = detect_mobile_browser(web.ctx.env['HTTP_USER_AGENT'])

Werner Keil

unread,
Aug 8, 2012, 6:15:57 PM8/8/12
to we...@googlegroups.com
As of now, (and even when this was posted, September 2011) using WURFL in most cases is no longer legitimate, unless you use WURFL's own APIs and Services.

Their new License Terms even prevent using 3rd party Scripts or APIs like Mobile.Sniffer and most likely also PyWurfl to be used with the current Closed Source WURFL after August 2011!!

If you stick with an old WURFL before that, in most cases you'd be safe, but still be aware of trolling and threats from ScienteMobile's legal team if you touch anything that is or was called WURFL.

Better look at alternatives, check out OpenDDR. It is Free and will remain Open Source just like Python itself won't be closed away by some crazy company any time soon;-) 
Reply all
Reply to author
Forward
0 new messages