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.6.tar.gz (266.1 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.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (297.8 kB view details)

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

pycapng-0.6-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (277.9 kB view details)

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

pycapng-0.6-cp313-cp313-macosx_11_0_arm64.whl (243.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pycapng-0.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (297.9 kB view details)

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

pycapng-0.6-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (278.0 kB view details)

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

pycapng-0.6-cp312-cp312-macosx_11_0_arm64.whl (243.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pycapng-0.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (296.3 kB view details)

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

pycapng-0.6-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (277.8 kB view details)

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

pycapng-0.6-cp311-cp311-macosx_11_0_arm64.whl (242.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pycapng-0.6-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (293.4 kB view details)

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

pycapng-0.6-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (276.4 kB view details)

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

pycapng-0.6-cp310-cp310-macosx_11_0_arm64.whl (241.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pycapng-0.6-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (293.6 kB view details)

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

pycapng-0.6-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (276.6 kB view details)

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

pycapng-0.6-cp39-cp39-macosx_11_0_arm64.whl (241.5 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for pycapng-0.6.tar.gz
Algorithm Hash digest
SHA256 5ad27236dad484be7fc27f4a053fb7a0c0f1ba4cf3b076dde3dfde1d24336468
MD5 35105b635a5ca8a05049a299f0ab6c13
BLAKE2b-256 080010becfe13e9e13b5343beb3b0bbdc206598061316d351d4084a17379859d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6.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.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 736fbbe598edf87fe06c5af499af561deb5e74b7a326b16a05aac73d786df732
MD5 f0461eeea6317fc0c265c509b8db16f9
BLAKE2b-256 b16a495d0aead0c01ef9c89ed0d83ba27594bdac9c70929879bbc2f353feb4f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2a1d8b85c8ad8958e8cf41f6be81c222fbf38bed8fdee12b1150a2c757806c00
MD5 1fe0dd344d17f49c046c641014e24908
BLAKE2b-256 a7862990cbe9e15f22a3675ddaa058909d07c80c40f15669463d10fdf2b6285b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b16d06fda2732b5f9a3611cc8d0d8ec5d5241f239858780c87469a4c080a170f
MD5 df53cc4a8d56556236e854abeacdce9a
BLAKE2b-256 52854e6f49794865467c05307eee1f877edd647f1218e55d83b6e5191431ea08

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c2eb81e0fcc7f00e54525e07bb931cb1af9276bad215eab0a5591867cf508e25
MD5 c8c9457f3dfe642b95ac679f553771e6
BLAKE2b-256 f14705dd1d466c962cbcca936ba63b9623226424fe1109f8fa47baf6aa0e794c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 137df02dbec98a2666bafe9950dcfa5465b8d9946de8cbc31e9f1c4aaf1f1a58
MD5 b4e7ef2d03bdc7bec20715c6c7e38837
BLAKE2b-256 53cb35cdc9bc6480a87abd51b14a467b75fb10be85789c7d3df4fd0b8a2fd3b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2040396ebe4a3766dc785f2b0a2a88930ccdbf59a37727cd45809b95ce1ec1db
MD5 fd79b5c9c10d16516d9d3c73bdadcb26
BLAKE2b-256 9b78801f96c92917baa3ce4c007a9e097f9f287cf2ed92b68c5a9b5bcb343abc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a52eff926a8138166c2a0854eaadd22073856130821a78bf9afb136add098af5
MD5 cfc77048b16965f8454dd2ef07a6f2e4
BLAKE2b-256 02a75d535396defa08595f23c91b63cf83e42f61adfba74745384c486c11e710

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6155909c1acfadbe00ef2facb84f19ebc2a1d34fd000d7d153aa595adb84f1ca
MD5 f8851524d9ce8d8873f9099e41db23ce
BLAKE2b-256 a9990d6179b317becdd5b417867c4fed3d8fe39af1e4927406989e6f6759650f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 789f4cce82ec06318413c3c8f1560347a5afe7810db490932a353f54b80a1c16
MD5 bc8168f157db838ed3c83b9dda2abddb
BLAKE2b-256 df774b2e2fdeb3830859caf2cc3e6016056a1481101cad4351116648282e4f0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b1ccb268ab148ab421f2280baae5fbb593f923b40a6b77f658b090e95380081c
MD5 8fed58bb4f33f536266a95e73408078e
BLAKE2b-256 23d0f2d5172871d004758fa9c7830a54ca9903d899a3c47838a5a7c51da68270

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 37cc2d0197d957f7475d7ea06936954e2f27011ca3e0b37ce2dba5e469d32feb
MD5 367a6c99b3633ea525566999f6833f77
BLAKE2b-256 7667d514fd36b060834609c903d48a8f0534edf5e768139c961a93dd9f527076

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 23894ac425f2f43e54caa03e542efdb91c78a666af53ef30c70ae1494d74c0a4
MD5 a61cf8596a72851d397a172086d0f55a
BLAKE2b-256 178f30c1ac285c599e2f5577297ac0e74b23d531aefe4c3aff68ed9be16b04be

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a4b5e5ac33b3079af50ac2d588301e1d7f17c188e452eaa45d092709510dd4a3
MD5 44b8e6af8058a60a4f3dc8b539a9fc8a
BLAKE2b-256 8ad8c9f6ad0346a219d62280e0cf5b90d48ce648b298aa5403afd352153708a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pycapng-0.6-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8e302e2fb4881b818f1c5b57bb415a6d036a05e2678d1129ee5be872a84ef3ff
MD5 116a5723cba181f776770a0977415038
BLAKE2b-256 94727d7dfd42f83f8ceb6f363ebe7474ee785fa4e0e0d5245b7ff4c31beaca42

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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.6-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

  • Download URL: pycapng-0.6-cp39-cp39-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 241.5 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.6-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8c0acfc2ea912e352c22b52c278c0beb44cb71a07a6b29643517b7cf9ddefac6
MD5 d1c3618358d3d8b0d0bf61c7b7d0d792
BLAKE2b-256 e24a726dda9e58d14caf179c44a07a97dc709edff4c8cc63c175246200c8ea82

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycapng-0.6-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