py-pcapplusplus
A Python wrapper for the Pcap++ library using nanobind, providing a high-level interface for network packet manipulation, capture, and analysis.
Features
- Read and parse PCAP files
- Create and modify network packets
- Support for multiple protocol layers:
- Ethernet
- IPv4/IPv6
- TCP/UDP
- ARP
- VLAN
- DHCP
- SOME/IP and SOME/IP-SD
- Raw socket operations for packet capture and injection
- High-performance packet processing
Installation
pip install py-pcapplusplus
Quick Start
Reading PCAP Files
from py_pcapplusplus import Reader, Packet
# Open a PCAP file
reader = Reader("capture.pcap")
# Iterate through packets
for packet in reader:
# Access different layers
eth_layer = packet[LayerType.EthLayer]
ip_layer = packet[LayerType.IPv4Layer]
# Print packet information
print(packet)
Creating Packets
from py_pcapplusplus import Packet, EthLayer, IPv4Layer, TcpLayer
# Create a new packet
packet = Packet()
# Add layers
eth = EthLayer(src_mac_addr="00:11:22:33:44:55", dst_mac_addr="66:77:88:99:aa:bb")
ip = IPv4Layer(src_addr="192.168.1.1", dst_addr="192.168.1.2")
tcp = TcpLayer(src_port=12345, dst_port=80)
# Add layers to packet
packet / eth / ip / tcp
Packet Capture
from py_pcapplusplus import RawSocket
# Create a raw socket on a specific interface
socket = RawSocket("eth0")
# Capture packets
packets = socket.sniff(timeout=5.0) # Capture for 5 seconds
# Process captured packets
for packet in packets:
print(packet)
API Reference
Core Classes
Packet
get_layer(layer_type): Get a specific layer from the packetadd_layer(layer): Add a new layer to the packetinsert_layer(prev_layer, new_layer): Insert a layer after an existing one
Layer Types
EthLayer: Ethernet layerIPv4Layer: IPv4 layerIPv6Layer: IPv6 layerTcpLayer: TCP layerUdpLayer: UDP layerArpLayer: ARP layerVlanLayer: VLAN layerDhcpLayer: DHCP layerSomeIpLayer: SOME/IP layerSomeIpSdLayer: SOME/IP-SD layerPayloadLayer: Raw payload layer
Protocol-Specific Features
Ethernet Layer
eth = EthLayer(src_mac_addr="00:11:22:33:44:55", dst_mac_addr="66:77:88:99:aa:bb")
eth.src_mac_addr = "00:11:22:33:44:55" # Set source MAC
eth.dst_mac_addr = "66:77:88:99:aa:bb" # Set destination MAC
eth.ether_type = True # Set to IPv4 (False for IPv6)
IPv4 Layer
ip = IPv4Layer(src_addr="192.168.1.1", dst_addr="192.168.1.2")
ip.src_ip = "192.168.1.1" # Set source IP
ip.dst_ip = "192.168.1.2" # Set destination IP
ip.ttl = 64 # Set TTL
ip.clear_chksum() # Clear checksum for recalculation
TCP Layer
tcp = TcpLayer(src_port=12345, dst_port=80)
tcp.syn_flag = True # Set SYN flag
tcp.ack_flag = True # Set ACK flag
tcp.rst_flag = False # Set RST flag
Raw Socket Operations
socket = RawSocket("eth0")
# Send a single packet
socket.send_packet(packet)
# Send multiple packets
socket.send_packets([packet1, packet2, packet3])
# Receive a packet
packet = socket.receive_packet(blocking=True, timeout=1.0)
# Sniff packets
packets = socket.sniff(timeout=5.0)
Dependencies
- Pcap++ library
- nanobind
- Python 3.10
Acknowledgments
- Pcap++ library for the underlying packet processing capabilities
- nanobind for the Python binding framework
Metadata
Release files for py-pcapplusplus 1.0.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| py_pcapplusplus-1.0.12.tar.gz | 7.9 MB | Details |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_pcapplusplus-1.0.12-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ x86-64 | Details |
| py_pcapplusplus-1.0.12-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl | CPython 3.10 | abi3 | Linux glibc 2.17+ ARM64 | Details |
| py_pcapplusplus-1.0.12-cp310-abi3-macosx_11_0_arm64.whl | CPython 3.10 | abi3 | macOS 11.0+ ARM64 | Details |
Total release size: 10.1 MB
Release files / py_pcapplusplus-1.0.12.tar.gz
| Download URL | py_pcapplusplus-1.0.12.tar.gz |
|---|---|
| Size | 7.9 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
33e950b3e953e7aea56bb23ce82b7d43f07c836bce17e17b4717680d14472dae
|
|
BLAKE2b-256 checksum How to use checksums |
13b782474f71231ad8e9c80c566a2b20276317427221026e4e101aa1bb99c59e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / py_pcapplusplus-1.0.12-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
| Download URL | py_pcapplusplus-1.0.12-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl |
|---|---|
| Size | 873.7 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
03b23ad00b567abdc36410b367f3caa910197ab3950c15c937d420c1dbb2ea88
|
|
BLAKE2b-256 checksum How to use checksums |
09e802868ab0acb1e0947d42ca7f989bf46fcce6bac655b1011869da59a23f51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / py_pcapplusplus-1.0.12-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
| Download URL | py_pcapplusplus-1.0.12-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl |
|---|---|
| Size | 798.6 kB |
| Tags | CPython 3.10 Linux glibc 2.17+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
e03d9b3f2d0b0f8a25302f8f9ff75cf3a9e3cb3f851b1167eeb650c8d791f084
|
|
BLAKE2b-256 checksum How to use checksums |
c32d78dc2bb0eb08a2267e8c558911a468d86942c30c08acafcad3fdfa094e6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / py_pcapplusplus-1.0.12-cp310-abi3-macosx_11_0_arm64.whl
| Download URL | py_pcapplusplus-1.0.12-cp310-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 563.3 kB |
| Tags | CPython 3.10 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
9f9cdef9ae4135901377387f92d933d9ac650110348ea9a5f8e7da308fb14fd5
|
|
BLAKE2b-256 checksum How to use checksums |
81a5b849406fdc8d40d020451f7583cbe62970c376416590794a2645aee026ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|