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 :
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).
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'] 'IAdvancedDeviceType' >>> app.environ['mobi.devices.platform'] 'iphone'
The implemented classification will split the devices into three categories :
It also provides information on the platform. Platform can be one of the following :
This classification is based on the MIT implementation.
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.
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:
[app:yourapp] # This part describe your application # use = ... [filter:mobidevicedetection] 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 = 127.0.0.1:11211 cache.lock_dir = /tmp/mobicache [filter:mobirouter] use = egg:mobi.devices#router # redirect mobile devices that connect to infrae.com hostname # to m.infrae.com infrae.com = http://m.infrae.com/ [pipeline:main] # This part configure the actual WSGI stack pipeline = mobidevicedetection mobirouter yourapp [server:main] # This is the configuration for paster web server. # It must have the same name as the pipeline use = egg:Paste#http host = 0.0.0.0 port = 8080 threadpool_workers = 7