isipsafe
Python logo

Python

Check addresses at sign-up or login, and report abuse, with requests.

Look up an address before you let it sign up, log in or check out, and report the ones that misbehave. Only requests is needed.

Check an address

import requests

def isipsafe_score(ip: str) -> int:
    r = requests.get("https://isipsafe.com/api/v1/check", params={"ip": ip}, timeout=3)
    r.raise_for_status()
    return r.json()["data"]["abuseConfidenceScore"]

if isipsafe_score(client_ip) >= 75:
    require_captcha()

Report an address

import os, requests

def isipsafe_report(ip: str, categories: list[str], comment: str = "") -> None:
    r = requests.post(
        "https://isipsafe.com/api/v1/report",
        params={"key": os.environ.get("ISIPSAFE_KEY", "")},
        data={"ip": ip, "categories": ",".join(categories), "comment": comment},
        timeout=5,
    )
    if r.status_code not in (200, 429):   # 429: already reported recently
        r.raise_for_status()

isipsafe_report("203.0.113.50", ["web-app-attack", "brute-force"], "40 failed logins in 5 minutes")

In a Django or Flask app

Cache results for an hour (for example with functools.lru_cache plus a timestamp, or your framework's cache) so a busy login page does not call the API on every request, and fail open: if the lookup times out, let the user through.

Need a hand?

If something here does not work with your version of Python, or you have improved it, tell us. We update these pages with what people send.

Python and its logo are trademarks of their respective owners. isipsafe is not affiliated with or endorsed by them.