Skip to content

Python

Prerequisites

Before you get started, you need to:

Installation

Honeypot works with Python 3.7 and above. Use your preferred package manager to install Honeypot:

sh
$ pip install honeypot-py==0.2.13

Basic Usage

In your backend (for example, in Django middleware or in a view), instantiate a honeypot instance with the URL of your honeypot:

python
from honeypot import honeypot

hp = honeypot(HONEYPOT_URL)

Sending events

There are two ways to send events to the honeypot:

  • Synchronously using track. This will return the payload from Honeypot
  • Asynchronously using track_async. This will track the event in a non-blocking manner.

Then, call the track method with the IP address and the message you want to send to the honeypot.

python
response = hp.set_ip("66.175.44.61").track("Hello, Python!")

# get all of the signals, including geo information, tags, etc.
response.signals()

# get specific signals
response.signals('current_tags', 'country_code', 'country_name')
python
hp.with_request(request).track_async("Hello, Python!")

Identifying users

You can pass a user ID or other identifiers to the identify method.

python
from honeypot import honeypot

hp = honeypot(HONEYPOT_URL)

hp.identify("user123")

honeypot.set_ip("66.175.44.61").track("Hello, Python Async!")

Django integration

If you're using Django, you can create a middleware object that automatically tracks events to certain endpoints. You'll need to pass a django.http.HttpRequest object to the with_request method and create event -> endpoint mappings, like so:

python

from honeypot import honeypot

hp = honeypot(HONEYPOT_URL)

honeypot(url)
    .event_paths(
        post={
            # Event name -> endpoint
            "Login": "/api/auth",
        },
    )
    .set_ip(ip)
    .with_request(request)
    .identify("user123")

Stripe Integration

If you're using Stripe, you can pass Honeypot signals directly into a Stripe checkout session via the subscription_data field.

python

stripe.checkout.Session.create(
    ...
    subscription_data={
        "metadata": **response.signals("connection_type", "email_domain_parked")
    },
)

See the Stripe documentation for more information on the subscription_data field.