Device Detection services for 51Degrees Pipeline

onpremise/gettingstarted_console.py

Getting Started Console Example - On-premise.

Getting Started Console Example - On-premiseThis example shows how to use 51Degrees On-premise device detection to determine details about a device based on its User-Agent and User-Agent Client Hint HTTP header values.

You will learn:

  1. How to create a Pipeline that uses 51Degrees On-premise device detection
  2. How to pass input data (evidence) to the Pipeline
  3. How to retrieve the results

This example is available in full on GitHub.

This example requires a local data file. The free 'Lite' data file can be downloaded with the scripts in the data GitHub repository, which this repository includes at cxx/data. Fetch the submodules by running git submodule update --init --recursive, then run sh cxx/data/get-data-files.sh 51Degrees-LiteV4.1.hash from the repository root, which saves the file as cxx/data/device-detection/51Degrees-LiteV4.1.hash.

The Lite data file is only used for illustration, and has limited accuracy and capabilities. The Enterprise data file has every property. Contact us about a copy.

Required PyPi Dependencies:

# *********************************************************************
# This Original Work is copyright of 51 Degrees Mobile Experts Limited.
# Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House,
# Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
#
# This Original Work is licensed under the European Union Public Licence
# (EUPL) v.1.2 and is subject to its terms as set out below.
#
# If a copy of the EUPL was not distributed with this file, You can obtain
# one at https://opensource.org/licenses/EUPL-1.2.
#
# The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
# amended by the European Commission) shall be deemed incompatible for
# the purposes of the Work and the provisions of the compatibility
# clause in Article 5 of the EUPL shall not apply.
#
# If using the Work as, or as part of, a network application, by
# including the attribution notice(s) required under Article 5 of the EUPL
# in the end user terms of the application under an appropriate heading,
# such notice(s) shall fulfill the requirements of that article.
# *********************************************************************
import sys
from fiftyone_devicedetection.devicedetection_pipelinebuilder import DeviceDetectionPipelineBuilder
# Where to ask for a copy of the Enterprise data file, tagged with this
# example.
CONTACT_US = "/contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-onpremise-gettingstarted_console.py&utm_term=enterprise-data-file"
# The properties only the Enterprise data file has, with the names they are
# shown with. The free Lite data file has the ones the example shows first.
ENTERPRISE_PROPERTIES = [
("Hardware Vendor", "hardwarevendor"),
("Hardware Name", "hardwarename"),
("Device Type", "devicetype")]
class GettingStartedConsole():
def run(self, data_file, logger, output):
# In this example, we use the DeviceDetectionPipelineBuilder
# and configure it in code. For more information about
# pipelines in general see the documentation at
# /documentation/_pipeline_api__concepts__configuration__builders__index.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-onpremise-gettingstarted_console.py&utm_term=run
pipeline = DeviceDetectionPipelineBuilder(
data_file_path = data_file,
# We use the MaxPerformance profile, which loads the whole data
# file into memory. The free Lite data file is about 35 MB, so
# that costs little memory and gives the fastest detections.
# See the documentation for more detail on this and other
# configuration options:
# /documentation/_device_detection__features__performance_options.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-onpremise-gettingstarted_console.py&utm_term=run
# /documentation/_pipeline_api__features__automatic_datafile_updates.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-onpremise-gettingstarted_console.py&utm_term=run
# /documentation/_pipeline_api__features__usage_sharing.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-onpremise-gettingstarted_console.py&utm_term=run
performance_profile = "MaxPerformance",
# inhibit sharing usage for this test, usually this
# should be set "true"
usage_sharing = False,
# Inhibit auto-update of the data file for this example
auto_update = False,
licence_keys = "").add_logger(logger).build()
# The pipeline is closed at the end of the with block, which
# releases the native data it holds. An application normally builds
# one pipeline when it starts, uses it for every detection, and
# closes it when it stops.
with pipeline:
ExampleUtils.check_data_file(pipeline, logger)
# carry out some sample detections
needs_enterprise = False
for values in EVIDENCE_VALUES:
needs_enterprise |= self.analyseEvidence(
values, pipeline, output)
if needs_enterprise:
output("\n" + ExampleUtils.get_enterprise_note(CONTACT_US))
def analyseEvidence(self, evidence, pipeline, output):
# FlowData is a data structure that is used to convey
# information required for detection and the results of the
# detection through the pipeline.
# Information required for detection is called "evidence"
# and usually consists of a number of HTTP Header field
# values, in this case represented by a dictionary of header
# name/value entries.
# Closing the FlowData, which the with statement does at the end of
# the block, releases the results of the detection straight away,
# rather than when the garbage collector next runs.
with pipeline.create_flowdata() as data:
message = []
# List the evidence
message.append("Input values:\n")
for key in evidence:
message.append(f"\t{key}: {evidence[key]}\n")
output("".join(message))
# Add the evidence values to the flow data
data.evidence.add_from_dict(evidence)
# Process the flow data.
data.process()
message = []
message.append("Results:\n")
# Now that it's been processed, the flow data will have
# been populated with the result. In this case, we want
# information about the device, which we can get by
# asking for a result matching named "device"
device = data.device
# Display the results of the detection, which are called
# device properties. See the property dictionary at
# /developers/property-dictionary?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-onpremise-gettingstarted_console.py&utm_term=analyseevidence
# for details of all available properties.
self.outputValue("Mobile Device", device.ismobile, message)
self.outputValue("Platform Name", device.platformname, message)
self.outputValue("Platform Version", device.platformversion, message)
self.outputValue("Browser Name", device.browsername, message)
self.outputValue("Browser Version", device.browserversion, message)
# The engine says which properties its data file has, and the
# value of a property it does not have is not read.
engine = pipeline.get_element("device")
needs_enterprise = False
for name, property in ENTERPRISE_PROPERTIES:
value = ExampleUtils.get_enterprise_value(
engine, device, property)
needs_enterprise |= value == ExampleUtils.NEEDS_ENTERPRISE
message.append(f"\t{name}: {value}\n")
output("".join(message))
return needs_enterprise
def outputValue(self, name, value, message):
# Individual result values have a wrapper called
# `AspectPropertyValue`. This functions similarly to
# a null-able type.
# If the value has not been set then trying to access the
# `value` method will throw an exception.
# `AspectPropertyValue` also includes the `no_value_message`
# method, which describes why the value has not been set.
message.append(
f"\t{name}: {value.value()}\n" if value.has_value()
else f"\t{name}: {value.no_value_message()}\n")
def main(argv):
# In this example, by default, the 51degrees "Lite" file needs to be
# somewhere in the project space, or you may specify another file as
# a command line parameter.
#
# Note that the Lite data file is only used for illustration, and has
# a small number of Properties. The example shows the properties only
# the Enterprise data file has as needing it. Contact us for a copy
# of the Enterprise data file:
# /contact-us?utm_source=code&utm_medium=example&utm_campaign=51degrees-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-onpremise-gettingstarted_console.py&utm_term=main
data_file = argv[0] if len(argv) > 0 else ExampleUtils.find_file(LITE_DATAFILE_NAME)
# Configure a logger to output to the console.
logger = Logger(min_level="info")
if (data_file != None):
GettingStartedConsole().run(data_file, logger, print)
else:
logger.log("error",
"Failed to find a device detection " +
"data file. Make sure the data submodule is there, " +
"by running `git submodule update --init --recursive`, " +
"and run this example again to download the file.")
if __name__ == "__main__":
main(sys.argv[1:])
Definition onpremise/build/__init__.py:1
Definition devicedetection_pipelinebuilder.py:1
Definition example_constants.py:1