ipasnmatcher - Python Package for Fast IP-to-ASN Matching

Monazir Muhammad Doha | Sep 5, 2025 min read

Recently, an idea popped into my head. It’s about a website, and this site will have some features that are exclusive to BRACU students. Upon looking for ways to restrict the rest of the world from accessing those features, I figured I could limit them to people connected to the University WiFi. But then again, how do I do that? I can’t host things locally on the University network.

But what if I could distinguish IP addresses from BRACU’s internet? Then I could allow just those IPs and block the rest of the world. After digging a little deeper into networking, I found out that every organization connected to the internet has something called an ASN number—think of it like an ID. This ID is then used to distinguish organizations by whoever runs the internet, and every organization is given a range of IP addresses.

For example:

Organization A with ASN 1234 has the IP range 192.168.0.9–27. That means this organization is allowed to have 192.168.0.9, 192.168.0.10, 192.168.0.11 up to 192.168.0.27, but not 192.168.0.28.

In reality, this is a little more complex, but you get the idea. ASN numbers and their IP ranges for every organization are public information—you can look them up easily. For instance, just Google “BRACU ASN” and you’ll find it!

Based on this logic, I made a simple Python package that takes the ASN number of an organization and lets you check if any given IP address falls within its IP range.

Hope you like this! I’d really appreciate a fork or a star on GitHub—the.

Features

  • Fast IP-to-ASN matching with optimized network ranges
  • Built-in caching to minimize API requests
  • Optional strict mode to consider only active prefixes
  • Uses accurate data from RIPE NCC

Installation

pip install ipasnmatcher

Usage

from ipasnmatcher import ASN

# Creating an ASN object fetches prefix data from the RIPEstat API and caches it locally
asn = ASN(asn="AS151981")

# Check if an IP belongs to this ASN
print(asn.match("153.53.148.45"))  # True or False

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)
)

Combine ASN objects

Merge multiple ASNs with + to check IPs against all their prefixes:

from ipasnmatcher import ASN

google = ASN("AS15169")      # Google
cloudflare = ASN("AS13335")  # Cloudflare

combined = google + cloudflare
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=3600) + ASN(asn='AS13335', strict=False, 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

  • On initialization, the ASN object fetches announced prefixes from the RIPEstat API and caches them locally in .ipasnmatcher_cache/{asn}.json.
  • Subsequent uses load data from cache if it is fresh (not older than cache_max_age).
  • Matching IPs against ASN prefixes 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.