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/ropoctl/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.2.tar.gz (25.1 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.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (302.2 kB view details)

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

libpnet_pyo3-0.1.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (292.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

libpnet_pyo3-0.1.2-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (548.7 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.2.tar.gz.

File metadata

  • Download URL: libpnet_pyo3-0.1.2.tar.gz
  • Upload date:
  • Size: 25.1 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.2.tar.gz
Algorithm Hash digest
SHA256 bd4ebe0da7a2d4515a999d37799e18e9abe66691ccceb264df533466a7ff8958
MD5 b8ef7676213d1fb346610bd91f68602a
BLAKE2b-256 3c7f2a4eb81e1d078b7df7a9d601d3eabb93172a2a13cc3328381121b798d62d

See more details on using hashes here.

Provenance

The following attestation bundles were made for libpnet_pyo3-0.1.2.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.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for libpnet_pyo3-0.1.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 07f3285c4827a18c134c305e50786a96a9cbd52e9d2e46e1141a0b606227f38d
MD5 f08c563bf13b73120b1348a9d391a18f
BLAKE2b-256 041bfc3ef9cb13db00c1b590f5b672bc970744a569b13ecc4531d003b3de7039

See more details on using hashes here.

Provenance

The following attestation bundles were made for libpnet_pyo3-0.1.2-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.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for libpnet_pyo3-0.1.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 01de212e99308a1a42cb7f8e3066133779df953ac9e77c743026bb7f276f2983
MD5 fbde9398892bce2dc9bbe8f0534164f5
BLAKE2b-256 0e1d41e1da383d846da60fb307ae81f7a31e1507d4fbe1177e2449fd5390787f

See more details on using hashes here.

Provenance

The following attestation bundles were made for libpnet_pyo3-0.1.2-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.2-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.2-cp310-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 456f92a2eb7bb7146381ebf4a649d09542bf5a172b5adac5448f4de8f06ca6f3
MD5 35d2c8a2fb97d04577dfd2a3199c67e8
BLAKE2b-256 01fe1a4033ce6aebee00f861cc8480bca560e3d6355fadc42a25c1970e14a5bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for libpnet_pyo3-0.1.2-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