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.
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":
OSX Recommended approach for OSX users is to use MacPorts . Once installed you can easily prepare you environment using " port":
The following set up and examples will be using Linux commands. You will need to adjust them accordingly to your environment.
- 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.
- Before matching user agents, you will need to configure the solution. A sample settings file can easily be generated running the following command:
- 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:
- Link your settings file from the "FIFTYONE_DEGREES_MOBILE_DETECTOR_SETTINGS" environment variable:
- You are now ready to test matching user agents. To check everything has been set up correctly use the command line matching utility:
51degrees-mobile-detector is created when installing 51degrees.com packages via pip.
Change the detection method:
Make sure the given file path has the correct device data and that python has the necessary permissions to reach it.
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/184.108.40.2066 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:
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
Load the module
Start matching user agents:
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.
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.
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.