Skip to main content

Python wrapper module for the Pcap++ library using nanobind

Project description

py-pcapplusplus

pypi downloads downloads_monthly Tests

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 packet
  • add_layer(layer): Add a new layer to the packet
  • insert_layer(prev_layer, new_layer): Insert a layer after an existing one

Layer Types

  • EthLayer: Ethernet layer
  • IPv4Layer: IPv4 layer
  • IPv6Layer: IPv6 layer
  • TcpLayer: TCP layer
  • UdpLayer: UDP layer
  • ArpLayer: ARP layer
  • VlanLayer: VLAN layer
  • DhcpLayer: DHCP layer
  • SomeIpLayer: SOME/IP layer
  • SomeIpSdLayer: SOME/IP-SD layer
  • PayloadLayer: 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


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.11a0.tar.gz (7.9 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

py_pcapplusplus-1.0.11a0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (867.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

py_pcapplusplus-1.0.11a0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (792.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

py_pcapplusplus-1.0.11a0-cp310-abi3-macosx_11_0_arm64.whl (561.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file py-pcapplusplus-1.0.11a0.tar.gz.

File metadata

  • Download URL: py-pcapplusplus-1.0.11a0.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

Hashes for py-pcapplusplus-1.0.11a0.tar.gz
Algorithm Hash digest
SHA256 35b80508a8da41e226fcdb73e9c3811e655971926364a6fde2e824257689d1cb
MD5 688507aacb1d383c610728388f3546c5
BLAKE2b-256 0f8e17549f19d9437089cc372700c8b53e1f98d68c756071e41229a8aea5d74f

See more details on using hashes here.

File details

Details for the file py_pcapplusplus-1.0.11a0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py_pcapplusplus-1.0.11a0-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 ba49d93c637640b87ea5f796991454c4cb9717d0d3b3454e6643ed5e1996baf5
MD5 1855e0dd93715311f514f64fd7374f12
BLAKE2b-256 9eeb0189d9ac5ed291494de59a72c366db9818ec68e5b0d89d2af880bc65e3bd

See more details on using hashes here.

File details

Details for the file py_pcapplusplus-1.0.11a0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for py_pcapplusplus-1.0.11a0-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 ac23c92aa1768682611d7420527dda601649b830c1216ef3298545d3264683f0
MD5 20c08d3c63634911f7c99dad9045638d
BLAKE2b-256 33df13954b9ae64ceed813f974cb7715c455d01cc67245eb89c788a5e955f8d6

See more details on using hashes here.

File details

Details for the file py_pcapplusplus-1.0.11a0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for py_pcapplusplus-1.0.11a0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0efdd2e38c345252b0638da1fb1d6e96e156dd7a4cb99439b84cef9e754062c8
MD5 1d3150c881665ca2e0395b5f48bd0958
BLAKE2b-256 760d7218e3646aab7be61a56cb63d5a54207c199835979e69ed2154d2d5baa7a

See more details on using hashes here.

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