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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

pycapng-0.4-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.4-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.4-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.4.tar.gz.

File metadata

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

File hashes

Hashes for pycapng-0.4.tar.gz
Algorithm Hash digest
SHA256 e3baa4cc663acb78b11ebc40d948b830a4e579c41660748b6733f6dd89ebf7a7
MD5 06b6df551fef47bf9d8077cccebab229
BLAKE2b-256 34b39c66a1239089e77523d043b06256353b27b625bddccaa744272a5d54b7fd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5538c25523e22a782d18d3e956a9eca64e31c01f2ed5b8fe578750f2bc2bef1e
MD5 1236bde2c421bccc0dca662d51f99d28
BLAKE2b-256 d32a59172698cedb49353eeefc8476318a1b66a1cd464ea9e696462a59df4d63

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 90d1305fb343663b4564f4fa63331649c99ef73e008c78d71612dcc3b32f6e92
MD5 5de3fdbdaff4a616f3837f0d0b02f3fd
BLAKE2b-256 3b872d829818499a548375345742d47117b71e3db3e6b52ee4a04d9f2dd80717

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eb863c007798d5d4c92e7fa36c15ff035f76f69372d46f43892121e599c24623
MD5 e2b8d3ae1b8479c1b167891f4eccc976
BLAKE2b-256 b678925f5f9c9d3e0ea740f24589247114cbff6e72e6294717f7937c6dc36cf1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8d1617fe60d97472c476928b44581528da9879e6c0ec47f692d37828d57963d5
MD5 fd2f58a2c78b57cf4e9541475914c6f5
BLAKE2b-256 3ed69a272d6db2dc706694b1f6c586c7b55794f3ae4fb10c120c3676fd4b65a9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6949bceba00f648b52df9b1da65c0dba309810470e455304d5bbf8ca02e15d8d
MD5 a3e33d38ea9d8807d5e170be78b75799
BLAKE2b-256 c7523db6524f8bacdba7f387d2ffdb1c65cafd8c1bbdbe3df45dcd1dcf5a24f2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2c91ddac65d0fb6de09082b3988aa879ef69e47dcf68ffaf04b9cfdab79a624b
MD5 1e11d84fd3579d44b1d79fe8b7f3bb39
BLAKE2b-256 bfebc1bcc80691259d1314ba31ee5ac2c9759388053ee343e9dc2210b9334976

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1c7d958a0b33cad9bc71e36cefc9fe7c99ccad05655f581999d80f7d9019f462
MD5 b45f2a3045ef6c796e0f7af49a3fc66d
BLAKE2b-256 9f82356f4ed296fb609f30f584c224c4a04e5101663a6e96c9ff9f9a746c9a70

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 2ba2d36ff92f4d8c92e1485987597ce99fe31289a7f64f96f3e61ffa30d81323
MD5 41f4f8d2087fe1a56bd56dd642c1f8ba
BLAKE2b-256 a303fcdb89afeac1ca607dee5833ec7e73a5081c0fa14da0f44315a1e31632c9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b5a1db5a84cac5fbd240662f0ef80f4eea71c416ba3b1e19f953d9888215bfeb
MD5 7cf306c6d5f3438fba96a862dc08e785
BLAKE2b-256 6db092939b73c33aee0d82b3faae97846f2af539ed9ccee3802ba7c5289c7bde

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 77c10a43a2071c1c72bd835ccdb8a8cd114cd1a4bfe29ee702d69f36f3a4b80c
MD5 7346ba742bbb073accc9c11254f4f5e4
BLAKE2b-256 1e1f3fccbd2a76a844951195ab6a942aad11737b375d5c36ab718110d10f2e27

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 bf63e18716b2aec106e7db9557756ea51c0fd39e2eb15c66ddea17f067830368
MD5 0770e3b0edfe75f0da30e71d056fc0cb
BLAKE2b-256 831d9892e4c567bbcace3071bf781d06cd01f162ce34f40a1ae12b9e53d5fbe4

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f08870b7ee2c2426f38e42d83e6b407c7c821cda59d3c286a60e46042ded16db
MD5 ae0b2f67956fe8c7c4792a8aa1fa7d21
BLAKE2b-256 c1af976596e42b75ec6aa2b5863fbb63f96d40d2adbbe97ade64588094e6aa73

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ff5cd88c7836f96cb53d1371dec3e57ed7bbb7da698bd7f7e27175210761da9e
MD5 018896e70840aa248153f0b1b67bfe6d
BLAKE2b-256 b60f0c0202d408e236786979f6364cff80a33d71180924418acd5a1e5d1ae205

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pycapng-0.4-cp39-cp39-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 5faaba2d50a6c3eb6086e62ae34e4b39642fe84bc1d4154ca5d6bbb00ede45e0
MD5 063cb07ea7a696cddd4a9e13b3ca639e
BLAKE2b-256 6d12293cbcfd67b68469e6f8dabeda733925a61f3acc2489d9f17f7c8f9cabe5

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: pycapng-0.4-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.4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1b5531d4bd103bdbbfd8118b86d4097965dd7ce26c9d1f8e09bc6af3d6d69b7a
MD5 3d2c059777b026519846bec1da6fa6ae
BLAKE2b-256 c67627666a4635db54dd7a7e3e872c34b35f9ab43a7b243d855ba8661d07b5d8

See more details on using hashes here.

Provenance

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