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 currenhttps://github.com/Itsmmdoha/ipasnmatcher/blob/main/LICENSEtly 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

Find the github repository here

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.1.0.tar.gz (4.4 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.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ipasnmatcher-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e8819ffdeb562de54ef4efede500a917a24d493d3b1f09817b682f5c9e7cde9d
MD5 4881de3b7385c663b35c77d2244cca4a
BLAKE2b-256 7f90792edd37b5d5dd57633ee60b7432326c6eb31757f4eb1ba6deb9a3c7b107

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ipasnmatcher-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ef7cded5979900d6048efd9ff4b0e63f934b5aaa82408a85a3dc991df316166
MD5 824907839e3bda5bb3715384b61078d0
BLAKE2b-256 97a4283c5f1e22894e6fe5cbd50502b27521f0bb5a6f1e496da230d89c9e4685

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