Skip to main content

Python bindings for libpcapng — read/write pcapng files and live packet capture

Project description

pycapng

Python library for reading, writing, and capturing network traffic in the pcapng file format.

pip install pycapng
  • Read pcapng files packet by packet
  • Write pcapng files with hand-crafted Ethernet/IP/TCP/UDP/ICMP/DNS/DHCP/NTP/TLS packets
  • Capture live traffic with a Wireshark-style display filter (Linux / macOS)
  • Reassemble fragmented IP packets
  • Script packet flows with the built-in pcapsh engine

Reading a pcapng file

import pycapng

p = pycapng.PcapNG()
p.OpenFile("capture.pcapng", "r")

def on_packet(data: bytes, ts: int) -> None:
    print(f"packet {len(data)} bytes  ts={ts}")

p.ForeachPacket(on_packet)
p.CloseFile()

Writing packets

Raw bytes

import pycapng

p = pycapng.PcapNG()
p.OpenFile("out.pcapng", "w")

raw = bytes([0x45, 0x00, 0x00, 0x28])   # any bytes
p.WritePacket(raw)

p.CloseFile()

TCP

p.WriteTcpPacket(
    src_ip="192.168.1.10", dst_ip="93.184.216.34",
    src_port=54321,        dst_port=80,
    payload=b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n",
    flags=0x02,   # SYN
)

Build a packet without writing it

syn = p.BuildTcpPacket(
    src_ip="10.0.0.1", dst_ip="10.0.0.2",
    src_port=1234, dst_port=443,
    payload=b"", flags=0x02,
)
# syn is bytes — inspect it, modify it, write it later
p.WritePacket(syn)

UDP / ICMP

pkt = p.BuildUdpPacket(
    src_ip="10.0.0.1", dst_ip="8.8.8.8",
    src_port=12345, dst_port=53,
    payload=b"\x00\x01...",   # your DNS query bytes
)

icmp = p.BuildIcmpPacket(
    src_ip="10.0.0.1", dst_ip="10.0.0.2",
    icmp_type=8, icmp_code=0,   # Echo Request
    payload=b"hello",
)

Application-layer helpers

# DNS
query    = p.BuildDnsQuery("example.com")
response = p.BuildDNSResponse("example.com", "93.184.216.34")

# DHCP
discover = p.BuildDhcpDiscover()
offer    = p.BuildDhcpOffer(client_mac="aa:bb:cc:dd:ee:ff",
                             offered_ip="192.168.1.100")

# NTP
req  = p.BuildNtpRequest()
resp = p.BuildNtpReply()

# TLS handshake (produces realistic-looking bytes for testing)
ch = p.BuildTlsClientHello()
sh = p.BuildTlsServerHello()
ct = p.BuildTlsCertificate()
fin = p.BuildTlsFinished()
app = p.BuildTlsApplicationData(b"GET / HTTP/1.1\r\n")

Write with a timestamp

import time

p.WritePacketTime(raw_bytes, int(time.time() * 1e9))   # nanoseconds

Live capture

Requires root or CAP_NET_RAW on Linux, root or the network entitlement on macOS.

Simple callback loop

import pycapng

with pycapng.Capture("eth0") as cap:
    cap.set_filter("tcp.dstport == 443")
    cap.loop(0, lambda pkt: print(f"{pkt.captured_len} bytes"))

Full example

import pycapng, signal

cap = pycapng.Capture(pycapng.capture_default_device())
cap.set_snaplen(65535)
cap.set_promisc(True)
cap.set_timeout(100)                        # ms between ring deliveries
cap.set_filter("ip and not arp")

signal.signal(signal.SIGINT, lambda *_: cap.break_loop())

def on_packet(pkt: pycapng.PacketInfo) -> None:
    ts = pkt.timestamp_ns // 1_000_000_000
    print(f"[{ts}] {pkt.direction}  {pkt.captured_len}/{pkt.original_len} bytes")

cap.loop(0, on_packet)

stats = cap.get_stats()
print(f"received={stats.received}  dropped={stats.dropped}")
cap.close()

Display filter syntax

The filter language is a subset of Wireshark's display filter syntax.

Example Meaning
tcp any TCP packet
tcp.dstport == 443 TCP to port 443
ip.src == 192.168.0.0/16 source in subnet
tcp.flags.syn and not tcp.flags.ack SYN-only
udp.port == 53 DNS (src or dst)
ip.addr == 10.0.0.1 any direction to/from IP
tcp and not (tcp.dstport == 80 or tcp.dstport == 443) non-web TCP
eth.addr == aa:bb:cc:dd:ee:ff by MAC (src or dst)

Built-in fields: eth.{src,dst,type,addr} · ip.{src,dst,proto,ttl,len,addr} · ip6.{src,dst} · tcp.{srcport,dstport,flags,flags.syn/ack/rst/fin,port} · udp.{srcport,dstport,port} · icmp.{type,code}

Capture directly to a pcapng file

# one-liner: capture 500 TLS packets and save to a file
pycapng.capture_to_file("eth0", "tls.pcapng",
                         filter="tcp.dstport == 443", count=500)

List available interfaces

for dev in pycapng.capture_list_devices():
    print(dev["name"], "(loopback)" if dev["loopback"] else "")

iface = pycapng.capture_default_device()

IP fragment reassembly

import pycapng

r = pycapng.Reassembler()
p = pycapng.PcapNG()
p.OpenFile("fragments.pcapng", "r")

def on_packet(data: bytes, ts: int) -> None:
    complete = r.add(data)
    if complete:
        print(f"reassembled datagram: {len(complete)} bytes")

p.ForeachPacket(on_packet)

pcapsh script engine

The pycapng.pcapsh submodule exposes a scripting engine for building and replaying packet flows declaratively.

from pycapng import pcapsh

engine = pcapsh.PcapSH()
engine.RunScript("my_flow.pcapsh", "out.pcapng")

TLS record builders are also available as standalone functions:

from pycapng.pcapsh import (
    tls_client_hello, tls_server_hello,
    tls_certificate, tls_finished,
    tls_application_data,
)

C library

pycapng is the Python interface to libpcapng, a C library that also ships a standalone command-line tool (pcapsh) and a full C API. See the repository for C usage, build instructions, and the capture API reference.

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

pycapng-0.7.tar.gz (268.3 kB view details)

Uploaded Source

Built Distributions

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

pycapng-0.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (300.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pycapng-0.7-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (279.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

pycapng-0.7-cp313-cp313-macosx_11_0_arm64.whl (245.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pycapng-0.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (300.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pycapng-0.7-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (280.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

pycapng-0.7-cp312-cp312-macosx_11_0_arm64.whl (245.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pycapng-0.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (298.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pycapng-0.7-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (279.6 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

pycapng-0.7-cp311-cp311-macosx_11_0_arm64.whl (243.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pycapng-0.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (296.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pycapng-0.7-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (277.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

pycapng-0.7-cp310-cp310-macosx_11_0_arm64.whl (242.6 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pycapng-0.7-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (296.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pycapng-0.7-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (278.1 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ ARM64manylinux: glibc 2.28+ ARM64

pycapng-0.7-cp39-cp39-macosx_11_0_arm64.whl (242.6 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file pycapng-0.7.tar.gz.

File metadata

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

File hashes

Hashes for pycapng-0.7.tar.gz
Algorithm Hash digest
SHA256 b5d8503d7f604111e9f0f2be7551192ca6898f877c3412080ce661c944aca383
MD5 1429ee6d746297e470f46d7535a80bd4
BLAKE2b-256 e583b8b94927fdc09917669fcaf1bf123ba6ebdb8af7726b62a27bf9eecf55e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7.tar.gz:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2e6c4a37f4976a451d572b3a992a2476b68ac8efa625ac03e43dbfdd994579d5
MD5 82541e5dfa9b7953f40764f962dc7537
BLAKE2b-256 2e7e7abc1a5a59ecf33996280d7935672b92c3e3b9485f59c058520d53dafaa8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 363b38f8a6cc1cec4a4f4b1796384bdccfa2af9ad74f79544aac718af13cb0d3
MD5 1eeb46bba5c63fc164c63f3151d80163
BLAKE2b-256 4a7221380c98efca2e4c4d7bd177bc207778c2eff897e7162d0426c9b220ca3e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b0d7b53faaf30c5b7f9e333dd971c3448ae66ca41bddacb4d3084295a792c0b4
MD5 3936454b8175dec9ebe1dc9be034f4a4
BLAKE2b-256 29061ec315856db9bb6f25a9f40a9a0103970e3c89323d2036b4b07f0bd2f515

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7af71795da81a7033851bed1baeac48f452188c1cc090b5c2a346baeeecd6181
MD5 e832813f7b9b0a66c0894e0da1ff28db
BLAKE2b-256 1d91f20eba156b2f4fe36f4e2f62adc0424387b6849f67997654c13d7fb47757

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8b6cbd9c2decf016b570044439b4f51b57bd3f00beff247f1a121b139ba67b34
MD5 3842b398c3aac11fa27a4b9280a89a73
BLAKE2b-256 8512034bd31af6b5ec4c9f1919ec4bf19302c063116311fb4350a1ad70bf4da3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b4110960b4bdb5ebc141c27a330ba19ab3cf2971ba85d36994800727526eef6
MD5 c56ea9005afa816323a0fe3321eddfd1
BLAKE2b-256 81c8e024b20ced69b79fa40dd14aee636e0e444c03621273ea773ec27481ae4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8594aa1e182207e22e772569b7f2293a1e17ce972cdf08e90d478b277e9ecbd3
MD5 b9159a3b094aed12b52d20573beff9e4
BLAKE2b-256 08807a6e26164e6136fe9bcf76f214bab196b2e05f82b4c3e35b779ea5bdc76b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1f313719aa6f3ecbb79da53cf2e0617d91f0f7a2220a5d0431c714d2820a94ad
MD5 34f64aa6387d44b5f187d09aab545be9
BLAKE2b-256 c73c19c860f13a35ae6cbc2c9347843c98cc18e33e8f8eccd7dcb0311feccaf1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8cb53ece36cf1bf045592dcb05011cac6b8b5471e97f16bd06cda4676c5d0720
MD5 379246e08ed98a77231ec22c5ded51a4
BLAKE2b-256 014896e2ab763970ea640c751ddb90a58752b7e162d3cfcbf74360bc5dfbbf03

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 78ed2645be5e2c7ff51bd85a4aa92d14780eedd5702defbb8872e5f7e16bf2e2
MD5 5a7952e0cf38bb4496450803a79c7f61
BLAKE2b-256 19d9c4dc6ad5a6b49deb83ecb9dea757d0905fa32882283cb574a3858e4bf659

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7700297abcb20e7b880f707a7fa284c37eaec4ece498bcd52368c05887e2d271
MD5 80085605587bfef1d158377f2aedc9d2
BLAKE2b-256 410ca0cf3c0fc4e207dcfc740b3aa375cda8b5156f1d01db368e895c01c2173e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d2f06ff9dcc8eded05f1bde6ed33416365142f75d5c587a12e723e6d4e9243a4
MD5 14bf7421f94cf0828c06bc3a8f902972
BLAKE2b-256 010b1d6245c31a61b8bc4bbaf54059e33f9332f108eb2431cef49242280a357f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cd47a649465bf896e68bdbfbc3033cf603383ae20f4a19ba26607edcdf58db39
MD5 003c29505e9cf81b0a1102b02bcb6420
BLAKE2b-256 494141f74b42a9009f863768b1639464d15e355c0dc3a00fa0cdac5716e56726

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.7-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2b2066290704014967efc49016881ffed841b1d0d1fefc083851b3815cecf88d
MD5 5a8abbc76442624ddfc68eeac268a3e4
BLAKE2b-256 81535b6f83d1f17c871ac6bbbaedc21e4f9bbc035de3dd5f0c9ea697cfcb826a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl:

Publisher: release.yml on stricaud/libpcapng

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

File details

Details for the file pycapng-0.7-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

  • Download URL: pycapng-0.7-cp39-cp39-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 242.6 kB
  • Tags: CPython 3.9, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pycapng-0.7-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 02ec5cf296a6b705022e65825fdb9ec60d81df7e4c5b6715404a069354c111bc
MD5 5b452495fe1f1bdb2f4876bc07463a4e
BLAKE2b-256 4ce97b3eebe11aadc0358771569890ded357045a5410652f294e92c5816d51e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.7-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: release.yml on stricaud/libpcapng

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