Package: mobile.sniffer
ContentsIntroductionFeaturesSupported sniffing backendsInstallationDependenciesUsage examplesRedirection exampleFeature extraction exampleMatch-making process for WurflChained exampleAutomatic database installersSource code and issue trackingCommercial support and developmentAuthorChangelog1.0.00.9.30.9.20.9.10.90.1.10.1Introductionmobile.snifferis Python framework for abstracting mobile browser detection and feature database access.When rendering web pages for mobile phones one must deal with varying handset features:
different screen sizes and shapes, different supported file formats, different sets of web browser features.
Also, the fact that you know the user is browsing on a mobile phone is most critical for building
successful mobile web user experience.mobile.snifferprovides two phase mobile phone detection (a.k.a sniffing)mobile detection- this simply detects whether a browser is a mobile phone based or not.
This is done inmobile/sniffer/detect.pymodule. This is useful to redirect to your
visitors from a web site to a mobile site if they are using a mobile phone to arrive on your web site.mobile handset feature extraction- the handset database is looked for a mobile web browser
user agent match. Since there might be version changes, local varieties, etc. in user agent
strings, heurestics are applied to the string matching. If a database entry is found, with
certain match accuracy, it’s records like device screen width and height are made
available to the web server so that it can tailor HTML, image and video output suitable
for this particular mobile phone.Mobile detection can be done with a fast regular expression match. Mobile handset feature
extraction always requires a some sort of database of mobile phone entries and mobile.sniffer framework
provides abstraction of these databases.FeaturesEasily plug-in mobile redirects to your Python based web sitesAble to source data from multiple sniffing backends leading better handset coverageAutomatically download, parse and cache complex RDF based WAP profilesVery convenient Python API designed by professionalsOpen sourceUnit test coverageThe code is Django, WSGI and Zope/Plone compatible.Supported sniffing backendsWurflApexVertex. Commercially available frommFabrik.DeviceAtlas. Commercially available.WAP profiles. User agents post a link to their WAP profile data, which is an XML file
and maintained by the handset manufacturer. (note: as WAP is deprecating protocol these are not supported on newer smartphones)Installationmobile.snifferis distributed as Python egg in PyPi repository.You can install it using standard Python egg installation methodseasy_installpipbuildoutDependenciesNotePython package comes with a copy of Wurfl database which dates around the release.
You might want to update this.You might need to install additional libraries depending on what handset database you useWurfl:pywurlf libraryandpython-LevenshteinWAP profiles: Django (for database abstraction) and rdflibApex Vertex: Django  (for database abstraction)Usage examplesThere is no single standard to name properties queried from the handset database.
For legacy reasons, we use DeviceAtlas database column names (keys)
and then map them to database-dependent keys.Redirection exampledetect_mobile_browser(user_agent)will return True of False
whether the HTTP request was made by a mobile phone.Example:from mobile.sniffer.detect import  detect_mobile_browser
from mobile.sniffer.utilities import get_user_agent

# Get HTTP_USER_AGENT from HTTP request object
ua = get_user_agent(self.request)
if ua:
    # Apply reg
    if detect_mobile_browser(ua):
        # Redirect the visitor from a web site to a mobile site
        pass
    else:
        # A regular web site visitor
        pass
else:
    # User agent header is missing from HTTP request
    return FalseFeature extraction exampleThis example will work out of the box with the included pywurlf database.Example:try:
    from mobile.sniffer.wurlf.sniffer import WurlfSniffer

    # Wrapper sniffer instance
    # All start-up delay goes on this line
    sniffer = WurlfSniffer()
except ImportError, e:
    import traceback
    traceback.print_exc()
    logger.exception(e)
    logger.error("Could not import Wurlf sniffer... add pywurfl and python-Lehvenstein to buildout.cfg eggs section")
    sniffer = None

def sniff_request(request):
    """
    @param request: Request can be Django, WSGI or Zope HTTPRequest object
    """

    if not sniffer:
        # We failed to initialize Wurfl
        return None

    user_agent = sniffer.sniff(request)

    if user_agent == None:
        # No match in the handset database,
        return None
    else:
        return user_agent # mobile.sniffer.wurlf.sniffer.UserAgent object


def web_or_mobile(request)
        ua = sniff_request(request)

        # How certain we must be about UA
        # match to make decisions
        # float 0...1, the actual value is UA search algorithm specific
        # We use JaroWinkler as the default algorithm
        certainty_threshold = 0.7

        if ua.get("is_wireless_device") and ua.getCertainty() > certainty_threshold:
                # Mobile code
                pass
        else:
                # Webby code
                passMatch-making process for WurflSince Wurfl is the default backend the process of finding UA record is explained more carefullyWurlf database is usually loaded during the start-up (slow operation) - it is possible
to make this to use lazy initialization patternThe search algorithm is initialized with certain match threshold - all matches below this threshold
will be ignored. The default search algorithm is JaroWinkler from Lehvenstein Python package.When the user agent is searchedTake in HTTP request User-Agent headerGo through all entries in databaseMatch this entry against incoming User-Agent using the search algorithmFirst search pass is doing using exact string matches (no algorithm involved). In this
case exposed certainty will be 1.1.If there was no match in the first pass, do the second pass using the search algorithmIf match is found and threshold is exceed return this user agent recordUser agent record is retrofitted with the information how accurate the match was
(ua.getCertainty() method exposes this)Chained exampleUse all available handset information sources to accurately get device data.
Matching is done on property level - if one data source lacks the property information the next data source is tried. Finally if the handset is unknown, but it publishes WAP profile information, the profile is downloaded and analyzed and saved for further requests.Example:from mobile.sniffer.chain import ChainedSniffer
from mobile.sniffer.apexvertex.sniffer import ApexVertexSniffer
from mobile.sniffer.wapprofile.sniffer import WAPProfileSniffer
from mobile.sniffer.deviceatlas.sniffer import DeviceAtlasSniffer

# Create all supported sniffers
da = DeviceAtlasSniffer(da_api_file)
apex = ApexVertexSniffer()
wap = WAPProfileSniffer()

# Preferred order of sniffers
sniffer = ChainedSniffer([apex, da, wap])

ua = sniffer.sniff(request) # Sniff HTTP_USER_AGENT, HTTP_PROFILE and many other fields
property = ua.get("usableDisplayWidth") # This will look up data from all the databases in the chainAutomatic database installersProprietary handset databases do not publicly distribute their APIs or data.
mobile.sniffer deals with the problem by automatic installation wrappers.
Also, these handset database APIs are not open source compatible which makes
it further difficult to use them in open source projects.
Instead of manually download and set up bunch of files each time
you deploy your code on a new server, just make call to one magical Python function which
will take care of all of this for you.Source code and issue trackingThe project is hosted atGoogle Code project repository.Commercial support and developmentThis package is licenced under open source GPL 2 license.Commercial CMS and mobile development support optionsare available from Web and Mobiel web site.Our top class Python developers are ready to help you with
any software development needs.AuthormFabrik Research Oy- Python and Plone professionals for hire.mFabrik Web & Mobile - multichannel CMS made easymFabrik web sitemFabrik mobile siteBlogChangelog1.0.0Updated Wurfl db [miohtama]0.9.3It’s spellt LeveNshtein - why this guy would have just be called John Doe [miohtama]0.9.2It’s spellt Leveshtein [miohtama]0.9.1Depend on Levehstein [miohtama]0.9Major product rework [miohtama]0.1.1Updated README to describe detection and redirects [miohtama]0.1Initial release