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 Distribution

ipasnmatcher-0.2.1.tar.gz (4.6 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.2.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ipasnmatcher-0.2.1.tar.gz
Algorithm Hash digest
SHA256 55793fc2bb5719c40dfcd4f5718cde9336ea5888410ab2002668d925753ad243
MD5 8ede22d4c82ef9ff6f5516aa50f111f7
BLAKE2b-256 4d13c3d1cc29b3d9bd61a0d9a2f54336c810bbda2966864f6ee8abcd33d259f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ipasnmatcher-0.2.1-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.7

File hashes

Hashes for ipasnmatcher-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2ddf9e41b6171574d355988fe41655e313519842b328560919db0ba0c1f9e635
MD5 69df145e7dc4c8ed8e1b336b64ee02b8
BLAKE2b-256 791f97d9f4c86c5be1c1857d5f1f78258880a6fcd2308dd70a60773e17a0451f

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