Python binding for bgpkit-parser
frompybgpkit_parserimportParserimportjsonparser=Parser(
url="https://spaces.bgpkit.org/parser/update-example",
filters={"peer_ips": "185.1.8.65, 2001:7f8:73:0:3:fa4:0:1"},
)
foreleminparser:
print(elem.origin_asns)
print(json.dumps(elem.to_dict(), indent=4))
breakYou can also add cache_dir to Parser to cache the downloaded files to a specified directory.
Here is an example:
frompybgpkit_parserimportParserimportjsonparser=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="./"
)
foreleminparser:
print(elem.origin_asns)
print(json.dumps(elem.to_dict(), indent=4))
breakThe 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:
frompybgpkit_parserimportFilter, Parserfilters= [
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(...)
#[pyclass]#[derive(Clone,PartialEq)]pubstructElem{#[pyo3(get, set)]pubtimestamp:f64,#[pyo3(get, set)]pubelem_type:String,#[pyo3(get, set)]pubpeer_ip:String,#[pyo3(get, set)]pubpeer_asn:u32,#[pyo3(get, set)]pubpeer_bgp_id:Option<String>,#[pyo3(get, set)]pubprefix:String,#[pyo3(get, set)]pubnext_hop:Option<String>,#[pyo3(get, set)]pubas_path:Option<String>,#[pyo3(get, set)]puborigin_asns:Option<Vec<u32>>,#[pyo3(get, set)]puborigin:Option<String>,#[pyo3(get, set)]publocal_pref:Option<u32>,#[pyo3(get, set)]pubmed:Option<u32>,#[pyo3(get, set)]pubcommunities:Option<Vec<String>>,#[pyo3(get, set)]pubatomic:Option<String>,#[pyo3(get, set)]pubaggr_asn:Option<u32>,#[pyo3(get, set)]pubaggr_ip:Option<String>,#[pyo3(get, set)]pubonly_to_customer:Option<u32>,}- Python3.9
- Python3.10
- Python3.11
- Python3.12
- Python3.13
python3 -m pip install pybgpkit-parsermaturin develop builds local python module and add to the venv.
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.
frompybgpkit_parserimportParser, ROUTE_FIELDS# Fast: only converts requested fieldsfortimestamp, prefix, as_pathinParser(url).iter_tuples(["timestamp", "prefix", "as_path"]):
pass# Faster for large files: batch Python boundary crossingsfields= ["timestamp", "prefix", "as_path"]
forbatchinParser(url).iter_tuple_batches(fields, batch_size=10_000):
fortimestamp, prefix, as_pathinbatch:
passAvailable field presets:
BASIC_FIELDS:timestamp,elem_type,peer_ip,peer_asn,prefixROUTE_FIELDS:BASIC_FIELDS+as_pathNEXT_HOP_FIELDS:BASIC_FIELDS+next_hop
You can also pass your own field list, e.g. Parser(url).iter_tuples(["peer_asn", "prefix"]).
Elem exposes Rust-like helper methods:
is_announcement()is_withdrawal()get_origin_asn()get_origin_asns()has_as_path()to_dict()/as_dict()origin_asnpropertyto_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 scansiter_tuple_batches(fields, batch_size)— recommended for large-file scans
RouteParser exposes upstream BgpRouteElem iteration for faster scans when you only need route identity fields:
frompybgpkit_parserimportRouteParserforrouteinRouteParser(url):
print(route.timestamp, route.peer_ip, route.peer_asn, route.prefix, route.as_path)RouteElem fields:
timestampelem_typepeer_ippeer_asnprefixas_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).
See BUILD.md for automated GitHub Actions release details.