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
py-pcapplusplus-1.0.7.tar.gz
(17.5 kB
view details)
Built Distributions
File details
Details for the file py-pcapplusplus-1.0.7.tar.gz
.
File metadata
- Download URL: py-pcapplusplus-1.0.7.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
e0259199a69e32fc0dbac02bd663a7b7cf789e785bf846a13e4e3305df8fb366
|
|
MD5 |
6f185da2113ae73c53efbaf467ca548b
|
|
BLAKE2b-256 |
2e669bc7131c61bd6d0ee3e1434b46ffc6a16bdbf9667bed2b272b9dda3b90e3
|
File details
Details for the file py_pcapplusplus-1.0.7-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
.
File metadata
- Download URL: py_pcapplusplus-1.0.7-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
- Upload date:
- Size: 862.9 kB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
fadf997d44832d33ebe8dc720d933f16b97b4ec7516504e67b7898a34178424b
|
|
MD5 |
73b98654b599396d9577f64cbf0639be
|
|
BLAKE2b-256 |
47644283a1f40aae1ce292ad9615e4fe9dfeb9591445894ae684f83134d85b38
|
File details
Details for the file py_pcapplusplus-1.0.7-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
.
File metadata
- Download URL: py_pcapplusplus-1.0.7-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
- Upload date:
- Size: 789.6 kB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
480731d400f2cad953733b6eb8f0af57d2a725590a7bd33a1ca32f9419af627f
|
|
MD5 |
9bd222924e1c7e90360b1260c20a5e1a
|
|
BLAKE2b-256 |
61fc7724a6ff76b0b57ef2aa057f47c040cde9ff137d9e60b27676e43c422d93
|
File details
Details for the file py_pcapplusplus-1.0.7-cp310-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: py_pcapplusplus-1.0.7-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 565.7 kB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
da792b11476f393738c45402236657c1bf5d040388ed0d857b87475d894f1f2b
|
|
MD5 |
34b02b78a3838f94cb4692dac3229463
|
|
BLAKE2b-256 |
5d9ae3a53da12cead34c2352bf88a396d87621354da109cc77086b5eac298ffc
|