Check out the latest documentation.

Getting Started

51Degrees for Python can be built on Linux and Mac OS x. We do not support Python on Windows at present.

51Degrees Mobile Detector for Python consists of several packages available in the Python Package Index (PyPI) repository: the core package ("51degrees-mobile-detector") and two packages containing specific detection methods ("51degrees-mobile-detector-wrapper" and "51degrees-mobile-detector-trie-wrapper").

When upgrading to Premium or Enterprise solutions please note that as of Version 3 only the data file is upgraded and not the detector itself. So there is no difference between the Paid-for and Free versions of the detector other than data file used.

Getting Ready

You will need to have a C compiler and the Python development headers installed in your system. You will also need make , pip , python and gcc installed. Follow the commands below to install all of these for your chosen environment. If you would like to install the package in a separate Python virtual environment, remember to create and activate the virtual environment before you execute the "pip" command.

Linux - For example, for Ubuntu and Python 2.x, you can install the python development headers using "apt":

										
										$ 
										
									sudo apt-get install python 
										
										(
										
									this is most likely already installed
										
										)
										
										
										$ 
										
									sudo apt-get install python-pip

										
										$ 
										
									sudo apt-get install gcc

										
										$ 
										
									sudo apt-get install python-dev

										
										$ 
										
									sudo apt-get install make
									

 

OSX Recommended approach for OSX users is to use MacPorts . Once installed you can easily prepare you environment using " port":

										
										$ 
										
									sudo port install py27-pip
									

Installation

The following set up and examples will be using Linux commands. You will need to adjust them accordingly to your environment.

  1. In your terminal navigate to the correct folder and install the package with your preferred detection method. The core package will be automatically installed as a dependency.
  2. 											
    											$ sudo 
    											
    										pip install 51degrees-mobile-detector-v3-wrapper
    										
  3. Before matching user agents, you will need to configure the solution. A sample settings file can easily be generated running the following command:
  4. 											
    											$ 
    											
    										51degrees-mobile-detector settings > 
    											
    											$HOME
    											
    										/51degrees-mobile-detector.settings.py
    										

    51degrees-mobile-detector is created when installing 51degrees.com packages via pip.

  5. Edit the generated sample settings file to the preferences of your choice. In this example we are using the v3 pattern wrapper, therefore you would need to set the "DETECTION_METHOD" in the settings file correctly. An data file is also needs to be specified in "V3_WRAPPER_DATABASE". A lite data file can be downloaded from Codeplex . For example:
  6. Change the detection method:

    										DETECTION_METHOD 
    											
    											=
    											
    											
    											'v3-wrapper'
    											
    										
    V3_WRAPPER_DATABASE
    											
    											=
    											
    											
    											'51Degrees.dat'
    											
    										

    Make sure the given file path has the correct device data and that python has the necessary permissions to reach it.

  7. Link your settings file from the "FIFTYONE_DEGREES_MOBILE_DETECTOR_SETTINGS" environment variable:
  8. 											
    											$ 
    											
    											
    											export 
    											
    											
    											FIFTYONE_DEGREES_MOBILE_DETECTOR_SETTINGS
    											
    											
    											=
    											
    											
    											$HOME
    											
    										/51degrees-mobile-detector.
    											

    settings.py
  9. You are now ready to test matching user agents. To check everything has been set up correctly use the command line matching utility:
  10. 											
    											$ 
    											
    										51degrees-mobile-detector match 
    											
    											"user agent goes here"
    											
    										

    For example: Suppose that one of the visitors of a website had a user agent string that looked something like this: "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+".

    Let's run the match from command line as shown above, replacing the text between the double quotation marks with this user agent. The output from matching this user agent would look like the following:

    Output from running 51degrees python match method from command line on a Blackberry test user agent.

    For this match I was using the Lite data file and the shown output represents only a fraction of values displayed. Depending on the data file you use there may be a lot more properties.

    Or alternatively Open a Python Console

    											
    											$ 
    											
    										python
    
    										

    Load the module

    											
    											from
    											
    											
    											fiftyone_degrees
    											
    											
    											import
    											
    										 mobile_detector
    
    										

    Start matching user agents:

    											
    											>>>
    											
    										 device 
    											
    											=
    											
    										 mobile_detector
    											
    											.
    											
    										match(
    											
    											"Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X)
    AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176"
    											
    										)
    
    
    											
    											>>>
    											
    										 device
    											
    											.
    											
    										Id
    
    											
    											'15767-18117-17596-18092'
    											
    											
    											>>>
    											
    										 device
    											
    											.
    											
    										method
    
    											
    											'v3-pattern-wrapper'
    											
    											
    											>>>
    											
    										 device
    											
    											.
    											
    										IsMobile
    
    											
    											'True'
    											
    										

    Some Examples

    The Following code snippet can be saved as a .py file and will match a given string which, in this case is 'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176', then create an object and print out the contents of that object. Functionality equivalent to var_dump() function in PHP.

    											
    											from
    											
    											
    											pprint
    											
    											
    											import
    											
    										 pprint
    
    											
    											from
    											
    											
    											inspect
    											
    											
    											import
    											
    										 getmembers
    
    											
    											from
    											
    											
    											fiftyone_degrees
    											
    											
    											import
    											
    										 mobile_detector
    
    device 
    											
    											=
    											
    										 mobile_detector
    											
    											.
    											
    										match(
    											
    											'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176'
    											
    										)
    pprint(getmembers(device))
    
    										

    Simply printing data for a single user agent is a great tool in some cases. However, to truly add value to your website or product you would want to use some of the values from 51Degrees Data file to alter the output for your users based on what devices they use. Quite often it's more than enough to do a simple check for the accessing device being mobile to redirect that device to a mobile version or to supply a different .css file. The following snippet uses the 'IsMobile' property to print different messages for mobile and non-mobile devices.

    											
    											from
    											
    											
    											fiftyone_degrees
    											
    											
    											import
    											
    										 mobile_detector
    
    											

    device = mobile_detector . match( 'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B176' ) if {device . IsMobile == True }: print "Your Device is mobile" #Do something for a mobile device. I.e redirect to mobile site version or #remove sidebar etc... else : print "Your device is not mobile" #Do something for a non mobile device.

    Since Python is more of a programming language than a server framework there is no universal solution to access the user agent string like you would do in PHP. Please refer to your server manual and Python documentation to see how to access the user agent variable in your particular environment.

    For a full list of supported properties for your data file please visit the Property Dictionary . Each property on the list contains information about the Data File it's available in as well as a description and possible values.