Skip to main content

pyo3 bindings for libpnet

Project description

libpnet-pyo3

pyo3 bindings for libpnet.

Status: alpha. Wheels published for Linux + macOS in v0.1. libpnet itself supports Windows (via Npcap or winpkfilter); the Windows wheel build for this wrapper is planned for a later release.

Install

pip install libpnet-pyo3

Wheels published for Linux (x86_64, aarch64) and macOS (x86_64, aarch64), Python 3.10+ (abi3).

Raw sockets need elevated privilege:

  • Linux: sudo or grant CAP_NET_RAW to the Python interpreter
  • macOS: sudo (BPF and raw sockets are root-only)

Quick start

from libpnet_pyo3 import tcp_sr1, icmp_ping, sniff, arp_who_has

# TCP SYN probe
resp = tcp_sr1(dst="1.2.3.4", dport=80, flags="S", timeout=1.0)
if resp and resp.is_synack():
    print(f"open, ttl={resp.ttl}, window={resp.window}")

# Ping
echo = icmp_ping("1.1.1.1", timeout=1.0)
if echo and echo.is_echo_reply():
    print(f"reply from {echo.src} ttl={echo.ttl}")

# ARP
reply = arp_who_has("192.168.1.1")
if reply:
    print(f"{reply.ip} is at {reply.mac}")

# Sniff
for pkt in sniff(count=10, timeout=5.0):
    print(pkt)

API

Send-and-receive

Function Returns
tcp_sr1(dst, dport, flags="S", *, sport=None, src=None, seq=None, window=64240, ttl=64, payload=None, timeout=1.0) TcpResponse | None
udp_sr1(dst, dport, payload, *, sport=None, src=None, ttl=64, timeout=1.0) UdpResponse | None
icmp_ping(dst, *, src=None, ident=None, seq=1, ttl=64, payload=None, timeout=1.0) IcmpResponse | None
arp_who_has(target_ip, *, iface=None, timeout=1.0) ArpReply | None

Send-only

Function
tcp_send(dst, dport, flags="S", ...) fire-and-forget TCP
udp_send(dst, dport, payload, ...) fire-and-forget UDP
send_ipv4_bytes(dst, packet, *, protocol="tcp") send pre-built IPv4 bytes
send_l2_bytes(packet, *, iface=None) send pre-built Ethernet frame

Capture

sniff(*, iface=None, count=None, timeout=None) -> list[SniffedPacket] — at least one of count/timeout is required.

Packet builders (return raw bytes)

build_tcp_packet, build_udp_packet, build_icmp_echo, build_arp_request — same kwargs as the send-receive equivalents.

Helpers

Function
list_interfaces() list[Interface] — name/mac/ipv4/ipv6/is_up/is_loopback/index
default_interface() first up, non-loopback iface with an IPv4
interface_for(name) look up by name
source_ipv4_for(dst) source IPv4 the kernel would pick for dst

Response objects

  • TcpResponse: src dst sport dport flags seq ack window ttl payload, methods has_flag(f), is_synack(), is_rst()
  • UdpResponse: src dst sport dport ttl payload
  • IcmpResponse: src icmp_type icmp_code ttl ident seq payload, method is_echo_reply()
  • ArpReply: ip mac iface
  • SniffedPacket: iface bytes ts_secs, method ethertype()

TCP flag constants

SYN, ACK, RST, FIN, PSH, URG, ECE, CWR — combine with | or pass a string like "SA" to flags=.

Not in v0.1 (planned)

  • IPv6 (IPv4 only today)
  • BPF filter expressions on sniff
  • pcap read/write
  • Multi-reply send (sr returning a list)
  • Windows wheels (libpnet supports Windows via Npcap / winpkfilter — this wrapper just hasn't wired it up yet)

Build from source

Requires Rust (stable, 1.74+) and Python 3.10+.

git clone https://github.com/Lazarus-AI/libpnet-pyo3
cd libpnet-pyo3
python -m venv .venv && source .venv/bin/activate
pip install maturin
maturin develop --release

License

Dual-licensed under Apache-2.0 or MIT at your option.

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

libpnet_pyo3-0.1.1.tar.gz (25.0 kB view details)

Uploaded Source

Built Distributions

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

libpnet_pyo3-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (302.1 kB view details)

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

libpnet_pyo3-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (292.2 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

libpnet_pyo3-0.1.1-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (547.8 kB view details)

Uploaded CPython 3.10+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file libpnet_pyo3-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for libpnet_pyo3-0.1.1.tar.gz
Algorithm Hash digest
SHA256 542e5ed09dd486805b962e11efded76986aed883eba63dfd475fa13b00193884
MD5 33f47b8b5b9ac1922ac9648c817d7852
BLAKE2b-256 325a260cde113baee1b52ec4cde539f75d7592fd128c443da07d82ac3e27a3df

See more details on using hashes here.

Provenance

The following attestation bundles were made for libpnet_pyo3-0.1.1.tar.gz:

Publisher: release.yml on ropoctl/libpnet-pyo3

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libpnet_pyo3-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libpnet_pyo3-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9a408be0bc0e2691ac49e05af82543e0e9d0718272e80463d9d8e3ffba0e4eb2
MD5 6221f6548dba520bce9511c305a7e786
BLAKE2b-256 574030f058c75f30284d0adf68159f0f579287d03c9bde63f999839ded3686bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for libpnet_pyo3-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on ropoctl/libpnet-pyo3

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libpnet_pyo3-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for libpnet_pyo3-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fad361ff86082ebb2453f71a9e3c97b8d6392957473cd075de70e6f8ef453c1d
MD5 1407dda162a514136108af76282bd564
BLAKE2b-256 ac15ce127d03995374ab98b77ec218b9a15fb7b3fa08866bdb9bcc9f6fe99121

See more details on using hashes here.

Provenance

The following attestation bundles were made for libpnet_pyo3-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on ropoctl/libpnet-pyo3

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libpnet_pyo3-0.1.1-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for libpnet_pyo3-0.1.1-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 5c2a7cbc6efdcd858ded69c896402972ebea760d00de1a06a73edcd74be886c5
MD5 8970cd8af38edeacb27135a47f481588
BLAKE2b-256 6eaef539ec0b9e3daf39855f8752f0a316ee464d16042245911a29a52dc4a548

See more details on using hashes here.

Provenance

The following attestation bundles were made for libpnet_pyo3-0.1.1-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on ropoctl/libpnet-pyo3

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