Skip to main content

A Python library for verifying whether an IP address belongs to a specific ASN's announced prefixes. Includes local caching and asynchronous API support using RIPEstat data.

Project description

ipasnmatcher

A Python package to verify if an IP address belongs to a specific ASN's network ranges using RIPEstat data.

Features

  • Lazy loads prefix data on first match for faster initialization
  • Fast IP-to-ASN matching with optimized network range checks
  • Built-in caching to minimize API requests
  • Optional strict mode to consider only active prefixes
  • Uses accurate data from RIPE NCC
  • Supports both synchronous and asynchronous usage

Installation

pip install ipasnmatcher

Usage

Synchronous Example

from ipasnmatcher import ASN

# Create an ASN object (prefix data will be lazy-loaded on first match)
asn = ASN("AS151981")

# The first match triggers data loading from RIPEstat API (and caches it)
print(asn.match("153.53.148.45"))  # True or False

Asynchronous Example

import asyncio
from ipasnmatcher import AsyncASN

async def main():
    # Create an async ASN object (lazy-loads on first async_match)
    async_asn = AsyncASN("AS15169")

    # The first async_match call triggers async data loading
    result = await async_asn.async_match("8.8.8.8")
    print(result)  # True or False

asyncio.run(main())

Advanced Usage

asn = ASN(
    asn="AS15169",      # ASN (e.g., Google)
    strict=True,        # Only consider active prefixes
    cache_max_age=7200  # Cache duration in seconds (2 hours)
)

Combining ASN Objects

You can combine multiple ASNs using the + operator. When combined:

  • If any of the ASNs has strict=True, the resulting combined ASN will also be strict.
  • The combined ASN’s max_cache_age will be the minimum of the values from the ASNs being merged.
from ipasnmatcher import ASN

google = ASN("AS15169", strict=False, cache_max_age=7200)
cloudflare = ASN("AS13335", strict=True, cache_max_age=3600)

combined = google + cloudflare

# Combined inherits strict=True and cache_max_age=3600
print(combined.match("8.8.8.8"))   # True (Google)
print(combined.match("1.1.1.1"))   # True (Cloudflare)

repr() shows the full combination:

ASN(asn='AS15169', strict=False, cache_max_age=7200) + ASN(asn='AS13335', strict=True, cache_max_age=3600)

Parameters

ASN(asn: str, strict: bool = False, cache_max_age: int = 3600)
  • asn: ASN identifier in format "AS12345"
  • strict: If True, only prefixes currently active are considered (default: False)
  • cache_max_age: Cache lifetime in seconds (default: 3600)

How it works

  • Data is lazy-loaded — the first match() or async_match() triggers prefix loading from the RIPEstat API.
  • Prefix data is cached locally in .ipasnmatcher_cache/{asn}.json.
  • Subsequent matches use cached data if it’s fresh (not older than cache_max_age).
  • Matching is done efficiently using Python’s ipaddress module.

Use Cases

  • Network security and traffic validation
  • CDN traffic routing based on ASN ownership
  • IP classification by network operators
  • Compliance monitoring of network connections

GitHub

Star or fork this project on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipasnmatcher-1.0.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ipasnmatcher-1.0.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file ipasnmatcher-1.0.1.tar.gz.

File metadata

  • Download URL: ipasnmatcher-1.0.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ipasnmatcher-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b4c6749240b29c6b064921f4f4cf0929864d3f827b3c9b38781fd87b28e29f50
MD5 04be35e3790f7b0bfbfbaf2c31f13860
BLAKE2b-256 23da3d2b7f9afc0e38d4516a0d7e68b65fa25ae0a50a40330b71eeb49ffed3e8

See more details on using hashes here.

File details

Details for the file ipasnmatcher-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: ipasnmatcher-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ipasnmatcher-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eab2599863e497febe26b92fc8913635e31294d374f8f0f1cec02017f8a378be
MD5 d83ae221b75a3b74b6c35797b8e2c681
BLAKE2b-256 4c3281bdfc7214ab2b3579bd17182f4d071fc3741c404644b4e5a0a156a552da

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page