Skip to content

Itsmmdoha/ipasnmatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
  • Efficient IP-to-ASN matching using radix trees with O(k) lookup time
  • 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 (Context Managed)

import asyncio
from ipasnmatcher import AsyncASN

async def main():
    async with AsyncASN("AS15169") as async_asn:
        result = await async_asn.async_match("8.8.8.8")
        print(result)

asyncio.run(main())

Asynchronous Example (Manual Lifecycle)

import asyncio
from ipasnmatcher import AsyncASN

asn = AsyncASN("AS15169")

async def main():
    # The first async_match call triggers async data loading
    match = await asn.async_match("8.8.8.8")
    print(match)

    # Manually close the async client to free resources
    await asn.aclose()

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=True, cache_max_age=3600) + 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 radix trees.

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.

About

A Python package to check if an IP address belongs to a specific ASN's network ranges with research-grade data from RIPEstat.

Resources

License

Stars

Watchers

Forks

Contributors

Languages