Python wrapper module for the Pcap++ library using nanobind
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py-pcapplusplus-1.0.11.tar.gz.
File metadata
- Download URL: py-pcapplusplus-1.0.11.tar.gz
- Upload date:
- Size: 7.9 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0a911cb087e5ee34c5c60aa5ee81bd3a007fb6d5ec6f83003a65606aa13ac99
|
|
| MD5 |
2e54d6eeb76ff6dd67482e7b277075cd
|
|
| BLAKE2b-256 |
addf61d0d63b389632d7269f02161de41e8a78b11cdff9d3a718d8b382887c49
|
File details
Details for the file py_pcapplusplus-1.0.11-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.
File metadata
- Download URL: py_pcapplusplus-1.0.11-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 867.4 kB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70bc7ee56ad5dc32255796726f1ec9413789a5a841c8dd018afcf366698009ce
|
|
| MD5 |
cbb42117b734333985e80439cd4e4b11
|
|
| BLAKE2b-256 |
82161aa01e709b2f6c64a5c9a8c4facd75ba909f386b790c84b5b2c255a5116c
|
File details
Details for the file py_pcapplusplus-1.0.11-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl.
File metadata
- Download URL: py_pcapplusplus-1.0.11-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
- Upload date:
- Size: 792.4 kB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4bafc1bddb417087dbd4c6db33b4d9a5123483316ebe7de7bc7c2d65ddcc991
|
|
| MD5 |
7c6ce1b7afcc1c2f348be1d094b221f2
|
|
| BLAKE2b-256 |
b8a6d529660ac885aa89656cec5b16c1bd288e0380ed3a25fc428329adfa146c
|
File details
Details for the file py_pcapplusplus-1.0.11-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: py_pcapplusplus-1.0.11-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 561.9 kB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24bf053b753acf98274bc0eb7603c7990b0aedaa780dd51a6231d0b7fe63ea32
|
|
| MD5 |
9bd9b49c857762c2eb70f38deec1fecd
|
|
| BLAKE2b-256 |
ad3513a6e9cbf9736d21455751efa79a2e86d5027dcbb19d510c2ce7c1e69e35
|