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.8.tar.gz (271.5 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.8-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (300.9 kB view details)

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

pycapng-0.8-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (280.1 kB view details)

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

pycapng-0.8-cp313-cp313-macosx_11_0_arm64.whl (245.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pycapng-0.8-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (300.7 kB view details)

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

pycapng-0.8-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (280.1 kB view details)

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

pycapng-0.8-cp312-cp312-macosx_11_0_arm64.whl (245.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pycapng-0.8-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.8-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.8-cp311-cp311-macosx_11_0_arm64.whl (243.8 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pycapng-0.8-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.8-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (278.0 kB view details)

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

pycapng-0.8-cp310-cp310-macosx_11_0_arm64.whl (242.7 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pycapng-0.8-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (296.1 kB view details)

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

pycapng-0.8-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl (278.0 kB view details)

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

pycapng-0.8-cp39-cp39-macosx_11_0_arm64.whl (242.7 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

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

File hashes

Hashes for pycapng-0.8.tar.gz
Algorithm Hash digest
SHA256 728139ebc275559545b15cb62cb0bd72741c2f89ccd0e6ce34c5eecb9db0250b
MD5 1c2295e72733fccf647e951b996832e0
BLAKE2b-256 9ec8bc9928d5fef1801155db2f9b9c28d3e46d45a281466f71dff493de85cefe

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dd8771a02afa5497ee9e90ddda3a36f8f95668f0cc873a1e7b7cf85614925d83
MD5 7308e5f4eaea506973419f00bd364bff
BLAKE2b-256 fb101783a1ba6968dc41dd6fd500d16237942926090bd62bfdf7692e122a5032

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 7360b8275cd098546c2fb9b2e5f9b259e40ac2a031dd44f83e307a909f911d0a
MD5 de5500f8bbbd6f9c5200c940c4e12163
BLAKE2b-256 e67f70e04d6a084e5a1b6adf7a5a8bb5c6bea5ba1448ec3aebb3c5022f9b60d4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33c148019ca18ccefbf0bce8497e7bbcffeb4521e4b7e82b42677a9ae5131ae2
MD5 7f1fd7af8d7e8a83d1f4beadeb1dfbb9
BLAKE2b-256 b3dc7cef43d393a021aef24dc2fc701ae03d7682dd36230cb3747b39547d3b20

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bc418a860628f155f0eecd69f245e45d4d175040eab2efea75893073bceb92b0
MD5 b2c3fc574c9ae22a3f1f1b382b80d16a
BLAKE2b-256 11afe27bda05a2ffd45dbdec2040b8f47d31ac5a952f541713344fc6976b4197

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6862562483585bbd82a9dc4261de6ea9669dbf2238b0feca159ceab9caf8b636
MD5 1b4cd9801465f52fc513ac01a4573050
BLAKE2b-256 3c8e44b248d8faefa1ca5f5411115111460cc1e5dbcb24f90a84179889c946f3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 34c3e3ddee3a382873e644e652c7b094e462625b54ad73c744347153b3b7b1c3
MD5 4288348470d8038c689cfc0109f652a2
BLAKE2b-256 08e2a8a79c3de7024cddafbc7e7638c658d8b2cc5a84449c0c49bb5e25cef7c4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0c62ab70931f966c88bb4e3f26593b15e79ed11c3407d69aa1a632940f980778
MD5 d9265bd58d64199ba7d4bbab2856e32a
BLAKE2b-256 61c470ff7ca5b513aad03cf14671fcd9225b03af1d6473abf9132cbd421be991

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 36ff89a1643d5ab45ad0e544fc3becb0ca95a7504d84e6ea119cf1d0f5dd3d6e
MD5 f8ce12f9b4c8b1f922001adf521141ca
BLAKE2b-256 07fa913852e17da71a05b05281d74d6c39b0d69032aae835d32eb0822dad1c29

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 10e2e64a4dc7c8fe2c4b647bbd93c08b1697a05c65df4f6123a802dd8a2a111e
MD5 dbea2c9a51d8af43d95ca0942a49b5bd
BLAKE2b-256 0af9c0d1b37a3de4ee4d8ea5fb23bce64911fd1f0f3d730de38b67094681f6f4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3144e94162c087745baf1d310e28bc486a5de710ad2376ca631c9f0822caadad
MD5 e4a602e7adfb54b7dbd4c9375e2e781c
BLAKE2b-256 fa9413589572d932584221ebed55acc4a444906a23f13fb1cb8f5a1da2d69063

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8934af2d71b3bcf782ce84cde42db36e99eb663b1f283577b49f6615338e5d03
MD5 715854cac971b16ddaca4ae59840e080
BLAKE2b-256 643bc602a445ce0d89b4ec1476db38157ef763b1da89665933c442e9953d86f5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1518401eb76394d9c5875b51e3e2774e491ebbd0d9c779e8550d9f249da26588
MD5 1ad8361ddd1432e8351c51da0581f6cd
BLAKE2b-256 7a995d395e69686e80cfaebf7a8c81952a3e5ca628a317c9f131fce88d1e941d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 09f4b58b188e4b3600483feb1e1902c0c7f64776057f0ff06f9702248cdee6ad
MD5 b4a070c504838539907fc10057866395
BLAKE2b-256 6f3d7245ab7bd98bfcbe0056899cf4dce917521485c2f9b95f9ec8fa83a3d29b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.8-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 16183d3da4e9c298743cbc9ff308616576d353df056a9cc1ccaf16c2dabf5023
MD5 057b5a60fc77a45ae070e9ac1e0c146a
BLAKE2b-256 9a6c6426cd79a7c2085dbbaba8a88bec292018b357c8675345cf1d8e53154d66

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pycapng-0.8-cp39-cp39-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 242.7 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.8-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2602034974869cd03aebf3e841423da3ddadd70e0f003ee5c0f6fd0300dd8679
MD5 b2e6682a74ee0c0f24f63411ba525406
BLAKE2b-256 e443590e0daec3f3ba3deaed975cbab1eea8ac62f5107314b9c8c64a6276a631

See more details on using hashes here.

Provenance

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