Registry / http-networking / pure-pcapy3

pure-pcapy3

JSON →
library1.0.1pypypi✓ verified 85d ago

pure-pcapy3, currently at version 1.0.1, is a pure Python reimplementation of the `pcapy` library, designed to be API compatible and a drop-in replacement. It allows Python programs to capture live network traffic and read/write pcap files without requiring C extensions. While aiming for full compatibility, its release cadence is infrequent, focusing on stability.

pip install pure-pcapy3
INSTALL
IMPORT
SIG · PURE-PCAPY3
P
pure-pcapy3
http-networkingpythonv1.0.1
Install
2.6s avg
Import
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.6s · import 0.000s · 20MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Bpf
from pure_pcapy import Bpf
import pcapy
Dumper
from pure_pcapy import Dumper
import pcapy
PcapError
from pure_pcapy import PcapError
import pcapy

This quickstart demonstrates how to initialize `pure-pcapy3` for live packet capture on a specified network interface. It includes a basic packet handler and a fallback mechanism to illustrate opening a pcap file. Remember to replace `'eth0'` with an actual network interface on your system (e.g., `'wlan0'`, `'en0'`) and ensure your user has permissions to capture packets (e.g., by running with `sudo` or being in the appropriate user group).

import pcapy import sys def packet_handler(header, data): # This is a very basic handler. Real-world usage would parse 'data'. print(f"Captured packet! Length: {header.getlen()} bytes") # Try to open 'eth0' or 'en0' (common interface names) # Fallback to an example pcap file if live capture fails or is not desired. interface = 'eth0' # Change to your network interface, e.g., 'wlan0', 'en0' try: # Open live capture on a network interface # interface, snaplen, promiscuous_mode, timeout_ms pc = pcapy.open_live(interface, 1500, True, 100) print(f"Listening on interface: {interface}...") print("Press Ctrl+C to stop.") pc.loop(0, packet_handler) # 0 for infinite loop except pcapy.PcapError as e: print(f"Error opening live capture on {interface}: {e}") print("Trying to open a dummy pcap file instead.") # Fallback to a dummy pcap file for demonstration if live capture fails # (e.g., no permissions, interface not found) try: # For a runnable example without requiring live interface access, you'd usually have a .pcap file. # This example assumes you might have 'test.pcap' or similar. # If not, this part will also fail. # Create a dummy pcap file if it doesn't exist for the example: import os dummy_pcap_path = 'dummy.pcap' if not os.path.exists(dummy_pcap_path): print(f"Warning: '{dummy_pcap_path}' not found. Cannot demonstrate file reading.") else: print(f"Opening pcap file: {dummy_pcap_path}") pc_file = pcapy.open_offline(dummy_pcap_path) pc_file.loop(0, packet_handler) except pcapy.PcapError as e_file: print(f"Error opening pcap file '{dummy_pcap_path}': {e_file}") print("Please ensure you have necessary permissions or a valid pcap file.") except KeyboardInterrupt: print("Capture stopped by user.") except Exception as e: print(f"An unexpected error occurred: {e}")
Debug
Known issues
gotchaThe package `pure-pcapy3` must be imported as `import pcapy`. This is by design, as it aims to be a drop-in replacement for the original `pcapy` library. Importing `pure_pcapy3` directly will not expose the expected API.
fix
Always use `import pcapy` after installing `pure-pcapy3`. Do not attempt `import pure_pcapy3` or `from pure_pcapy3 import pcapy`.
affects: All versions
gotchaDespite being a 'pure Python' implementation, `pure-pcapy3` still relies on an underlying packet capture library (like `libpcap` on Linux/macOS or `WinPcap`/`Npcap` on Windows) to open network interfaces for live capture. These system-level libraries must be installed on your operating system for `pcapy.open_live()` to function correctly.
fix
Ensure `libpcap` development headers are installed on Linux/macOS (e.g., `apt-get install libpcap-dev`) or `Npcap` (with 'WinPcap API-compatible mode') on Windows. You may also need appropriate user permissions (e.g., run as root/administrator or add user to `wireshark`/`pcap` group) to capture traffic.
affects: All versions
gotchaPerformance of `pure-pcapy3` might be lower than the original C-extension-based `pcapy` or other optimized packet capture libraries for high-throughput scenarios, due to its pure Python nature. If performance is critical, consider the original `pcapy` (if compatible) or alternatives like `scapy` or `dpkt` combined with `pylibpcap`.
fix
Benchmark your application with `pure-pcapy3` under expected load. If performance bottlenecks are identified, evaluate whether the pure Python requirement is strict or if a faster, potentially C-dependent, alternative can be used.
affects: All versions
Upgrade
Version history
1.0.1latest on PyPI · released Feb 27, 2017
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
pure-pcapy3 — pip install pure-pcapy3 · libregistry