Skip to main content

Read pcapml-labeled network traces (pcapng) into pandas DataFrames. Pure Python.

Project description

pcapml (Python)

Read pcapml-labeled network traces into pandas DataFrames. Pure Python — no native extensions, no libpcap, no Go binary required. The only dependency is pandas.

pcapml stores ground-truth labels as per-packet comments inside standard pcapng files. This package parses those files directly so you can go from a labeled capture to a tidy DataFrame in one line.

Install

pip install pcapml

Or from this repository:

pip install ./python

Quick start

import pcapml

df = pcapml.read_pcapml("dataset.pcapng")
print(df.head())
                          timestamp sample_id label direction         dst        src_ip        dst_ip proto  src_port  dst_port  length
2026-03-19 22:25:24.189764096+00:00         0  curl   lan2wan example.com 192.168.1.188 104.18.26.120   TCP     47954        80      60
2026-03-19 22:25:24.195076864+00:00         0  curl   wan2lan example.com 104.18.26.120 192.168.1.188   TCP        80     47954      60
...

Columns

Column Dtype Description
timestamp datetime64[ns, UTC] Packet capture time
sample_id string pcapml sample ID (kept as string — IDs can exceed 2⁶⁴)
label string Sample label / process name
direction string Direction tag if present (lan2wan, wan2lan, e, i, …)
dst string Resolved destination domain, if any
src_ip, dst_ip string L3 addresses (IPv4 or IPv6)
proto string TCP, UDP, ICMP, … (or the numeric value)
src_port, dst_port Int64 L4 ports (nullable)
length Int64 Original on-wire packet length
raw bytes Full captured packet bytes

Arbitrary comment keys are promoted automatically. The columns above are the fixed core schema; any other key=value pair in a comment becomes its own column named after the key (sparse keys fill with <NA>). For example a comment s=0,proc=curl,dst=youtube.com,asn=15169 yields an extra asn column. If a key collides with a core column name it is prefixed (meta_<key>) instead of overwriting it.

Pass include_raw=False to drop the raw column and save memory:

df = pcapml.read_pcapml("dataset.pcapng", include_raw=False)

The header decoding (IPv4/IPv6 + TCP/UDP) is intentionally lightweight. The raw bytes are always available if you want a full dissector such as dpkt or scapy:

import dpkt
df["eth"] = df["raw"].apply(dpkt.ethernet.Ethernet)  # for Ethernet linktype

Iterating by sample

For ML workflows it's often handier to work one sample at a time. samples() groups consecutive packets by sample ID (pcapml writes each sample's packets contiguously; use pcapml sort first if yours aren't):

for sample in pcapml.samples("dataset.pcapng"):
    print(sample.sample_id, sample.label, len(sample))
    sample.df          # a DataFrame of just this sample's packets
    sample.metadata    # dict of every key=value pair from the comment

Sample also exposes .sid and .packets aliases.

Label formats

Both pcapml comment encodings are supported transparently:

  • Keyed (eBPF / gateway capture): s=0,proc=curl,dir=lan2wan,dst=example.com
  • Legacy positional: 18205618432581910911,windows-10

Lower-level access

If you don't want pandas in the loop, iterate raw packets directly:

from pcapml import iter_packets, parse_comment

for pkt in iter_packets("dataset.pcapng"):
    sid, label, direction, dst, meta = parse_comment(pkt.comment)
    pkt.timestamp   # POSIX seconds (float)
    pkt.data        # raw bytes
    pkt.link_type   # pcapng linktype (1 = Ethernet, 101 = RAW IPv4)

License

Apache-2.0.

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

pcapml-0.1.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

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

pcapml-0.1.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pcapml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 32e67476e6eab2de5ef781cee0bcb06c50ce4f880e92187463fb4e1b5088ebd3
MD5 879fef6f61bbaa54b3e9039b478b4aeb
BLAKE2b-256 07d3303ea44ac1745cf8eb924a623e74c16919373fa3f497b989644f8c6459f5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pcapml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pcapml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ece281c67de2f1e0cf62501912694380be387d1c3b972ecbbcbed04eb2f7ec5b
MD5 5e1fb028e3b7e0ebf60dcf7d79c856e4
BLAKE2b-256 f9076faf584ecb115f2eac5a7ee10ae97571128abb51e8f8f1e0c2743f0711af

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