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

ips = ["8.8.8.8", "1.1.1.1", "172.217.14.142"]
for ip in ips:
    if asn.match(ip):
        print(f"{ip} belongs to {asn.asn}")
    else:
        print(f"{ip} does not belong to {asn.asn}")

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

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-0.0.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

ipasnmatcher-0.0.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ipasnmatcher-0.0.1.tar.gz
Algorithm Hash digest
SHA256 795ae897a52678e6776e5a81f8ab6a7c599834964d303e7fcd3e5f6c729c0b3d
MD5 6f828f868078da9cdb0df941a8d567e7
BLAKE2b-256 43e0cd1a1318d5d04397f0463749e15bfd83c6c61e835c835ec75d1e2f357b92

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ipasnmatcher-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3d22d0d8d9ef4bc2ddaec3e8b65ea5942fe11fc4d3b4359f570f5bd1f004352c
MD5 4ddd31d12d24659f384951875a7f06a4
BLAKE2b-256 eafaef34a238d472ef9109afabe4f31f8aa108d41ba8684019f0029f99903234

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