Skip to main content

A Python package that allows you to verify if an IP address belongs to a specific ASN's network ranges.

Project description

ipasnmatcher

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

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.

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

ipasnmatcher-0.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ipasnmatcher-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35e5a7f23ccfb2db273191c03b623eb8cb1ec1be73e627ced04f33108bbf4989
MD5 52beeb3c307e17a90af6af0584814466
BLAKE2b-256 7bc13e08fc37efae8102a65470ebc42a675772763db1e3d6377e323b1ba5f656

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