Anura Docs

Anura SDK for Python

The Anura SDK for Python provides a simpler integration process for Anura Direct within their Python projects. Empowering developers to begin analyzing their traffic.

Getting Started

  1. Have an open active account with Anura - You can see more about Anura's offerings here.
  2. Minimum Requirements - To use the SDK, you will need Python >=3.10.
  3. Install the SDK
  4. View our Quick Examples to immediately begin using the SDK!

Installing the SDK

The easiest and recommended way to install the SDK is to use pip. You can install it with the following command:

Terminal
pip3 install anura

Or, install from source by using one of the following examples according to your operating system:

Linux/Mac:

Terminal
git clone https://github.com/anuraio/anura-sdk-python.git
cd anura-sdk-python
python3 -m pip3 install -r requirements.txt
python3 -m pip3 install -e .

Windows:

Terminal
git clone https://github.com/anuraio/anura-sdk-python.git
cd anura-sdk-python
py -m pip3 install -r requirements.txt
py -m pip3 install -e .

Quick Examples

Create the Anura Direct client

from anura.direct.client import AnuraDirect
import asyncio # used for asynchronous result fetching
import aiohttp # used for asynchronous result fetching

direct = AnuraDirect('your-instance-id-goes-here')

Create additional data for Anura Direct

For sending additional data, you can use a dict. We'll use the additional_data when we fetch a result:

additional_data = {}
index = 1

# adding an element
additional_data[index] = 'your-data-value'

# updating an element (adding the element again, but with a new value)
additional_data[index] = 'your-new-data-value'

# removing an element
del additional_data[index]

Get a result from Anura Direct

try:
    result = direct.get_result(
        ip_address='visitors-ip-address',    # required
        user_agent='visitors-user-agent',    # optional
        app='visitors-app-package-id',       # optional
        device='visitors-device-id',         # optional
        source='your-source-value',          # optional
        campaign='your-campaign-value',      # optional
        additional_data=additional_data      # optional
    )

    if (result.is_suspect()):
        # Perform some logic only for suspect visitors
        print('Visitor is suspect.')

    if (result.is_non_suspect()):
        # Perform some logic only for non-suspect visitors
        print('Visitor is non-suspect.')

    if (result.is_mobile()):
        # Perform some logic only for visitors from mobile devices
        print('Visitor is using a mobile device.')

    is_web_crawler = (result.rule_sets is not None and "WC" in result.rule_sets)
    if (is_web_crawler):
        # Perform some logic only for web crawlers
        print('Visitor is a web crawler.')

    print('result: ' +  str(result))
except Exception as e:
    print(e)

Get a result from Anura Direct asynchronously

async def main():
    direct = AnuraDirect('your-instance-id')

    async with aiohttp.ClientSession() as session:
        try:
            result = direct.get_result_async(
                session=session,                     # required
                ip_address='visitors-ip-address',    # required
                user_agent='visitors-user-agent',    # optional
                app='visitors-app-package-id',       # optional
                device='visitors-device-id',         # optional
                source='your-source-value',          # optional
                campaign='your-campaign-value',      # optional
                additional_data=additional_data      # optional
            )

            if (result.is_suspect()):
                # Perform some logic only for suspect visitors
                print('Visitor is suspect.')

            if (result.is_non_suspect()):
                # Perform some logic only for non-suspect visitors
                print('Visitor is non-suspect.')

            if (result.is_mobile()):
                # Perform some logic only for visitors from mobile devices
                print('Visitor is using a mobile device.')

            is_web_crawler = (result.rule_sets is not None and "WC" in result.rule_sets)
            if (is_web_crawler):
                # Perform some logic only for web crawlers
                print('Visitor is a web crawler.')

            print('result: ' +  str(result))
        except Exception as e:
            print(e)

 # Use asyncio to run the coroutine (asynchronous function).
asyncio.run(main()) 

API Reference

AnuraDirect

Can get results from Anura Direct. These results are fetched using Direct's /direct.json API endpoint.

Methods

get_result() -> DirectResult

Parameters:

Name Type Description Required
ip_address str The IP address of your visitor. Both IPv4 & IPv6 addresses are supported. Yes
user_agent str The user agent string of your visitor.
app str The application package identifier of your visitor (when available.)
device str The device identifier of your visitor (when available.)
source str A variable, declared by you, to identify "source" traffic within Anura's dashboard interface.
campaign str A subset variable of "source", declared by you, to identify "campaign" traffic within Anura's dashboard interface.
additional_data dict Additional Data gives you the ability to pass in select points of data with your direct requests, essentially turning Anura into "your database for transactional data."
get_result_async() -> Awaitable[DirectResult]

Parameters:

Name Type Description Required
session aiohttp.ClientSession The aiohttp client session object. Yes
ip_address str The IP address of your visitor. Both IPv4 & IPv6 addresses are supported. Yes
user_agent str The user agent string of your visitor.
app str The application package identifier of your visitor (when available.)
device str The device identifier of your visitor (when available.)
source str A variable, declared by you, to identify "source" traffic within Anura's dashboard interface.
campaign str A subset variable of "source", declared by you, to identify "campaign" traffic within Anura's dashboard interface.
additional_data dict Additional Data gives you the ability to pass in select points of data with your direct requests, essentially turning Anura into "your database for transactional data."
@property instance(self) -> str @instance.setter instance(self, instance: str) -> None @property use_https(self) -> bool @use_https.setter use_https(self, use_https: bool) -> None

DirectResult

The result upon a successful call to get_result() or get_result_async() from the AnuraDirect client. It contains not only the result from the Anura Direct API, but some other methods to help you use the result as well.

Methods

is_suspect() -> bool is_non_suspect() -> bool is_mobile() -> bool

Properties

result: str mobile: int | None rule_sets: list[str] | None invalid_traffic_type: str | None