Device detection


The device detection library lives in the mobi.devices module. It consists of classifiers that can detect mobile phone devices.

Two classifiers are currently implemented :

  • MIT Classifier
  • WURFL Classifier

The MIT Classifier is based on the original implementation of the mobi-service by MIT. It contains a set of patterns to match against User Agent strings. The patterns are stored in a JSON file, exported from the database of the MIT mobi-service. The tools directory at the root of the package contains a Python script to export the MIT database to JSON format.

The WURFL (Wireless Universal Resource File) project maintains “an XML configuration file which contains information about capabilities and features of many mobile devices”. The specifications are contained in the open source XML file which is frequently updated.

In the default implementation of the classification mechanism an incoming User Agent string is checked against the MIT classifier. If the User Agent string isn’t found in the database the library falls back to the WURFL file.

The simplest way to use the device detection/classification system is to use the WSGI middleware but you may also use the library directly in your application. Although using the middleware makes it totally independant of the application or framework, as long as it is WSGI enabled (all modern Python frameworks are).

Device dectection (Classifier) middleware

The mobi.devices library provides device classification middleware.

Here an example of what it can do :

>>> from mobi.devices.wsgi import MobiDeviceMiddleware
>>> from webob import Request
>>> request = Request.blank('/')
>>> class TestApp(object):
...   def __call__(self, environ, start_response):
...     self.environ = environ
...     start_response('200 Ok', [('Content-Type', 'text/plain')])
...     return ['']
>>> request.environ['HTTP_USER_AGENT'] = "Mozilla/5.0 (iPhone Simulator; U;" \
...   "CPU iPhone OS 3_1_3 like Mac OS X; en-us) AppleWebKit/528.18" \
...   "(KHTML, like Gecko) Version/4.0 Mobile/7E18 Safari/528.16"
>>> app = TestApp()
>>> request.call_application(app)
>>> app.environ['mobi.devices.marker_name']
>>> app.environ['mobi.devices.platform']

The implemented classification will split the devices into three categories :

  • IBasicDeviceType : Phones with an HTTP browser
  • IStandardDeviceType : Phones with an HTLM/CSS browser and Touch capabilities
  • IAdvancedDeviceType : Phones with advanced (Webkit / Gecko) browser and Touch capabilities (HTML5/CSS3)

It also provides information on the platform. Platform can be one of the following :

  • computer
  • webos
  • spider
  • symbian
  • maemo
  • winmo
  • blackberry
  • featurephone
  • android
  • iphone
  • android
  • iphone
  • feature phone
  • linuxmobile

This classification is based on the MIT implementation.

Router middleware

mobi.devices also provides a router middleware that can redirect to a different URL if the device is detected as a mobile device. The middleware factory is in mobi.devices.wsgi.router.

Configuring the WSGI stack with PasteDeploy

PasteDeploy is a popular tool for configuring WSGI the middleware and endware stack. It integrates with WSGI web servers like paster or mod_wsgi for Apache.

Here is a configuration example:

# This part describe your application
# use = ...

use = egg:mobi.devices#classifier
# cache the classification on a client side cookie
cookie_cache = true
# configure caching (use beaker)
cache.type = ext:memcached
cache.url =
cache.lock_dir = /tmp/mobicache

use = egg:mobi.devices#router
# redirect mobile devices that connect to hostname
# to =

# This part configure the actual WSGI stack
pipeline = mobidevicedetection mobirouter yourapp

# This is the configuration for paster web server.
# It must have the same name as the pipeline
use = egg:Paste#http
host =
port = 8080
threadpool_workers = 7

Table Of Contents

Previous topic

Welcome to Mobi’s documentation!

Next topic

Rendering system

This Page