Skip to main content

Python binding for bgpkit-parser

Project description

bgpkit-parser-py

Python binding for bgpkit-parser

Example

from pybgpkit_parser import Parser
import json

parser = Parser(
    url="https://spaces.bgpkit.org/parser/update-example",
    filters={"peer_ips": "185.1.8.65, 2001:7f8:73:0:3:fa4:0:1"},
)

for elem in parser:
    print(elem.origin_asns)
    print(json.dumps(elem.to_dict(), indent=4))
    break

You can also add cache_dir to Parser to cache the downloaded files to a specified directory.

Here is an example:

from pybgpkit_parser import Parser
import json

parser = Parser(
    url="https://spaces.bgpkit.org/parser/update-example",
    filters={"peer_ips": "185.1.8.65, 2001:7f8:73:0:3:fa4:0:1"},
    cache_dir="./"
)

for elem in parser:
    print(elem.origin_asns)
    print(json.dumps(elem.to_dict(), indent=4))
    break

Filters

The original dictionary-based filter API is still supported:

parser = Parser(url, filters={"peer_ips": "185.1.8.65,2001:7f8:73:0:3:fa4:0:1"})

Reusable Rust-backed filters are also available:

from pybgpkit_parser import Filter, Parser

filters = [
    Filter.peer_ips(["185.1.8.65", "2001:7f8:73:0:3:fa4:0:1"]),
    Filter.elem_type("a"),
]
parser = Parser.from_filters(url, filters)

Available helper constructors:

  • Filter.peer_ip(...)
  • Filter.peer_ips([...])
  • Filter.origin_asn(...)
  • Filter.prefix(...)
  • Filter.elem_type(...)

Available fields for Elem

    #[pyclass]
    #[derive(Clone, PartialEq)]
    pub struct Elem {
        #[pyo3(get, set)]
        pub timestamp: f64,
        #[pyo3(get, set)]
        pub elem_type: String,
        #[pyo3(get, set)]
        pub peer_ip: String,
        #[pyo3(get, set)]
        pub peer_asn: u32,
        #[pyo3(get, set)]
        pub peer_bgp_id: Option<String>,
        #[pyo3(get, set)]
        pub prefix: String,
        #[pyo3(get, set)]
        pub next_hop: Option<String>,
        #[pyo3(get, set)]
        pub as_path: Option<String>,
        #[pyo3(get, set)]
        pub origin_asns: Option<Vec<u32>>,
        #[pyo3(get, set)]
        pub origin: Option<String>,
        #[pyo3(get, set)]
        pub local_pref: Option<u32>,
        #[pyo3(get, set)]
        pub med: Option<u32>,
        #[pyo3(get, set)]
        pub communities: Option<Vec<String>>,
        #[pyo3(get, set)]
        pub atomic: Option<String>,
        #[pyo3(get, set)]
        pub aggr_asn: Option<u32>,
        #[pyo3(get, set)]
        pub aggr_ip: Option<String>,
        #[pyo3(get, set)]
        pub only_to_customer: Option<u32>,
    }

Supported Python Version

  • Python3.9
  • Python3.10
  • Python3.11
  • Python3.12
  • Python3.13

Installation

python3 -m pip install pybgpkit-parser

Develop

maturin develop builds local python module and add to the venv.

High-performance projected iteration

For best performance, prefer projected tuple iteration when you only need a subset of fields. This avoids creating full Elem objects and skips conversion for unused fields.

from pybgpkit_parser import Parser, ROUTE_FIELDS

# Fast: only converts requested fields
for timestamp, prefix, as_path in Parser(url).iter_tuples(["timestamp", "prefix", "as_path"]):
    pass

# Faster for large files: batch Python boundary crossings
fields = ["timestamp", "prefix", "as_path"]
for batch in Parser(url).iter_tuple_batches(fields, batch_size=10_000):
    for timestamp, prefix, as_path in batch:
        pass

Available field presets:

  • BASIC_FIELDS: timestamp, elem_type, peer_ip, peer_asn, prefix
  • ROUTE_FIELDS: BASIC_FIELDS + as_path
  • NEXT_HOP_FIELDS: BASIC_FIELDS + next_hop

You can also pass your own field list, e.g. Parser(url).iter_tuples(["peer_asn", "prefix"]).

Utility methods

Elem exposes Rust-like helper methods:

  • is_announcement()
  • is_withdrawal()
  • get_origin_asn()
  • get_origin_asns()
  • has_as_path()
  • to_dict() / as_dict()
  • origin_asn property
  • to_json()
  • to_psv()
  • Elem.get_psv_header()
  • module constants: ELEM_TYPE_ANNOUNCE, ELEM_TYPE_WITHDRAW, PSV_HEADER

Parser also provides stream-consuming helpers:

  • count()
  • iter_batches(batch_size)
  • iter_tuples(fields) — recommended for high-performance subset-field scans
  • iter_tuple_batches(fields, batch_size) — recommended for large-file scans

Route-level parsing

RouteParser exposes upstream BgpRouteElem iteration for faster scans when you only need route identity fields:

from pybgpkit_parser import RouteParser

for route in RouteParser(url):
    print(route.timestamp, route.peer_ip, route.peer_asn, route.prefix, route.as_path)

RouteElem fields:

  • timestamp
  • elem_type
  • peer_ip
  • peer_asn
  • prefix
  • as_path

RouteParser supports the same constructor style, from_filters(...), parse_all(), parse_next(), count(), iter_batches(batch_size), iter_tuples(fields), and iter_tuple_batches(fields, batch_size).

For route scans, this is the fastest object-based API; for maximum throughput use RouteParser.iter_tuples(ROUTE_FIELDS) or RouteParser.iter_tuple_batches(ROUTE_FIELDS, batch_size).

Build and publish

See BUILD.md for automated GitHub Actions release 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

pybgpkit_parser-0.7.0.tar.gz (31.4 kB view details)

Uploaded Source

Built Distributions

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

pybgpkit_parser-0.7.0-cp39-abi3-win_amd64.whl (3.6 MB view details)

Uploaded CPython 3.9+Windows x86-64

pybgpkit_parser-0.7.0-cp39-abi3-manylinux_2_38_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.38+ x86-64

pybgpkit_parser-0.7.0-cp39-abi3-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

pybgpkit_parser-0.7.0-cp39-abi3-macosx_10_12_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file pybgpkit_parser-0.7.0.tar.gz.

File metadata

  • Download URL: pybgpkit_parser-0.7.0.tar.gz
  • Upload date:
  • Size: 31.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pybgpkit_parser-0.7.0.tar.gz
Algorithm Hash digest
SHA256 051f9f2e8b01f732c939c34ab01c8f728d72d7e1c0f18d25645a7f26a836c0da
MD5 f77551f44a73e1282ab4756a2987c6fe
BLAKE2b-256 e28230fde72e202566c04beb3d9f4827806b46f62e812c4f90521b7190682a08

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybgpkit_parser-0.7.0.tar.gz:

Publisher: release.yml on bgpkit/bgpkit-parser-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybgpkit_parser-0.7.0-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for pybgpkit_parser-0.7.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 42e60fd5dc7021a21a2c3f3366ac6f687cc00f13a8b6d5ea7e18886d591a1fba
MD5 4f13ac4887654abaa0bee731dbecc0d1
BLAKE2b-256 5ed255f3421af8b81288ff8719eebd7ade79406cb95adb0f6d32765f365a9a4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybgpkit_parser-0.7.0-cp39-abi3-win_amd64.whl:

Publisher: release.yml on bgpkit/bgpkit-parser-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybgpkit_parser-0.7.0-cp39-abi3-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for pybgpkit_parser-0.7.0-cp39-abi3-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 e71541fb2c51acd56d2439d50693f4db0448007b5b480f2d8b93702bb0e324ec
MD5 0a4aff91bd80af76e94164c565aefcee
BLAKE2b-256 72faf9df790363c142c2c4c38ab0dbcd3e73f017e55ad4a188f9f708643d94bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybgpkit_parser-0.7.0-cp39-abi3-manylinux_2_38_x86_64.whl:

Publisher: release.yml on bgpkit/bgpkit-parser-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybgpkit_parser-0.7.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pybgpkit_parser-0.7.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a37eb609e72db857ddbc3971b56b249fbfe0e9e5df667947ac43f7fedff512e0
MD5 d238658e497d80df62ab77804e2484d8
BLAKE2b-256 b90e39830a708e16d362c0bd4a3dd6339c947eb55e586f6a9dd96c96f3c79a97

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybgpkit_parser-0.7.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on bgpkit/bgpkit-parser-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybgpkit_parser-0.7.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pybgpkit_parser-0.7.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a4cc3b945d49eb8292a75ae5c7dcb67824a85b9481e06c9600966cd887393ca0
MD5 892dfb96e276976e5c9add481afda9d0
BLAKE2b-256 dc3298fb85b015dee712148c5c133b1934dc8034446f3dd6c9a3e821a929f3a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybgpkit_parser-0.7.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on bgpkit/bgpkit-parser-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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