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.5.tar.gz (266.0 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.5-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.5-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.5-cp313-cp313-macosx_11_0_arm64.whl (243.8 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pycapng-0.5-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.5-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.5-cp312-cp312-macosx_11_0_arm64.whl (243.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pycapng-0.5-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.5-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.5-cp311-cp311-macosx_11_0_arm64.whl (242.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pycapng-0.5-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.5-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.5-cp310-cp310-macosx_11_0_arm64.whl (241.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pycapng-0.5-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.5-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.5-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.5.tar.gz.

File metadata

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

File hashes

Hashes for pycapng-0.5.tar.gz
Algorithm Hash digest
SHA256 3d8c3d2e0e6ba1e29e788e727a4684ceabc1d6043e567d241f33adf241478535
MD5 cbd4ab543f97c184205a877fa4c7e3c0
BLAKE2b-256 ff57eb971691d50402c4f99544a1be064063797e6c3d02729e6007edbb571805

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d17a0e53f9fc506403b40d76532da0b87cedbb58339bb035869c57491be97930
MD5 4cd11ea0403a69c9e3df8d5382574f8a
BLAKE2b-256 6abee4edc2123a8e58c47a8960d5c9139a55436d6140f91b269eb2c2f4133f89

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4fd556c69e68ecac54cf1e7b5ae522b1fbba0ba2311dec146e3b6bd8250b63fd
MD5 9960e7889892830151f67fc484130408
BLAKE2b-256 87f50d0e01a6bd02537c29a41535e1065af2bf1366478e43e0e00d4859f10d91

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b249824641cd4628041a0b8872d05adf577bde54029843e4ec63c5a0dd1afb4f
MD5 b78e9a59e22045eac30a931835684e72
BLAKE2b-256 82f9b40fc5e6d4746ded10b0690e814571d2a4bfe467f109aa1e9640cff581b8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f1a51cac1366a9220cd8c3e03cc9af85f379dd97d7a6728c0350620f18aa760f
MD5 be104c1e6b2ec77a6a9313706bc605ec
BLAKE2b-256 a28b64ae32e8ae07d2ca1905b05fb3e7641ceece3c0a727f0eaf0a0f4cb9a153

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1bac0c18b4c1c7783a8f9412f1edcc7f379ef638e2ce4b03d1f1cd5bfa479099
MD5 e54007802eec8318ecfb0e5f4006cf90
BLAKE2b-256 bed0a19bf9750847b6ef4e5995342b935ae028a7639471af2289e25e31a78ee2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bec8c7514f555222085acd205d1b0e8f503530dc033577e1a5fb499b03817e3e
MD5 aee50c2f14845e559ea075e1a25d9f50
BLAKE2b-256 70dce4b261a3c3c67c64eee37de97df2318aa340307b76a968bbfe2c32531d20

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 24c06c5ba0f8558a8c2189e61c7673eba5b6136c01ce01a4de70cfcbf010c94b
MD5 b510fc96938dc8dd53bcdc431182374e
BLAKE2b-256 522800a0e6cf4ac8a7d7a41ec530f6d6bd4bdee6b190240822c5449faa59e579

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 97750952dbfc2aff0eac9f1dd2bd560412ce8d5a0ca512c518ef2ef7629c9afa
MD5 edfeadc1bb645d03fcde190c859b34fb
BLAKE2b-256 df5eab1072f67ad5a6bd550ee8d369c6e597d9e306cbdcd4199376ab1a227d32

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3abaab2848afaeb4ce8a2cbb6320e8caad1a5be0f4e143890a04498727bfb467
MD5 9705c38784f5db800fc80dede85c90fd
BLAKE2b-256 446f509a48b213c3632560f4449441bb744d131981c700dac959b0ed7f02010e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 efdd96ab74ba24122846c4dd98dce60e1e8d6e0c6480e5fde0f2bd359d99df80
MD5 b2e905a324149bf4faa892742cb8bf32
BLAKE2b-256 b6bf27361d591c361449b596264fa9c8ea3fc79d634dc32c0242ee0a5597b4e3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 79f9befb4dd6d8adc0fb7a7aaae935fba676e9ba0fd0bb2dc6a9e0ded988c592
MD5 05f87913ae536d06041f808b907cad58
BLAKE2b-256 28382bf38bb7e62372598fa8ba4c3aac5cd0399ce3ed810a52334bab45741bf2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cfb83de2dde75d3fa5725cd4ab1bff1b54607de62201b8fffc7ba4798dfe81bc
MD5 e8fc15385acc07d284b596a7ee1714a7
BLAKE2b-256 6c5f526dc666e131cbe477671fec409e564444bb11f05874459410929419d244

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d8ee415cf718d2660c033a1788d014d9dee5091a7307a211afe6562968aebae1
MD5 6f73ac1fbf8fb0593de3460a9fdadf42
BLAKE2b-256 cdaf9f9f10ca768729c4a28ed50a7ef49c3e5617571fa60d13b54e0f0b4151e8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.5-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 12ac75607f4f3a142850d774269ef4793285f0795fb40566b22fcdea3a06e3cf
MD5 430bbecb41733e3f8a009d2031a75522
BLAKE2b-256 5b7f03fca22bc108d2d719eec87211cd81e74cc6e82d1b2996ed9f6be6b787e2

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pycapng-0.5-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.5-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3f64aa4d6da37a5077df6063586e497bad3c362f1a33c90dd47a9bf765a7db45
MD5 fde2c8c24cd981c5016cba40e8a904c4
BLAKE2b-256 43f5eb23ef059de19bb054cb2c98e31b62dc77f881fadef845d367dc01d66272

See more details on using hashes here.

Provenance

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