Registry / http-networking / pypcap

pypcap

JSON →
library1.3.0pypypiunverified

pypcap is a Python interface to the `libpcap` C library, enabling programmatic packet capture and analysis. It provides access to network devices and allows reading packets from live captures or pcap dump files. The current version is 1.3.0. Releases are infrequent but address Python compatibility and underlying `libpcap` features.

pip install pypcap
INSTALL
IMPORT
SIG · PYPCAP
P
pypcap
http-networkingpythonv1.3.0
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v? · pip install
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
build_error
glibc
py 3.103.910 runs
build_error
Code
Verified usage

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

pcap
import pcap

This quickstart captures packets on a default or specified network interface. It includes error handling for common issues like permissions and device availability. Run with `sudo` on Linux/macOS for best results.

import pcap import os import sys try: # Try to find a default network device dev = pcap.lookupdev() if dev is None: print("No default network device found. Attempting to list all devices...") all_devs = pcap.findalldevs() if not all_devs: print("No network devices available on this system.") sys.exit(1) dev = all_devs[0] # Use the first available device as a fallback print(f"Using first available device: {dev}") # Allow overriding the device from an environment variable for easier testing capture_device = os.environ.get('PCAP_DEV', dev) print(f"Attempting to capture on device: {capture_device}") pc = pcap.pcap(capture_device) # Optional: Set a BPF filter (e.g., 'tcp port 80' or 'udp') # pc.setfilter('tcp') print("Starting packet capture (press Ctrl+C to stop)...") for ts, pkt in pc: # ts is the timestamp (float), pkt is the raw packet bytes print(f"[{ts}] Captured packet of length: {len(pkt)} bytes") # For detailed parsing, consider libraries like scapy or dpkt except pcap.PcapError as e: print(f"Error initializing pcap: {e}") print("\nCommon issues:\n1. Lack of permissions: Try running with `sudo` (e.g., `sudo python your_script.py`).\n2. Device not found: Check if '{capture_device}' is the correct interface name (use `pcap.findalldevs()` to list).\ 3. `libpcap` not installed: Ensure `libpcap-dev` (Linux) or `libpcap` (macOS) is installed.") sys.exit(1) except KeyboardInterrupt: print("\nCapture stopped by user.") except Exception as e: print(f"An unexpected error occurred: {e}") sys.exit(1)
Debug
Known issues
gotchapypcap is a wrapper around the C library `libpcap`. You must have `libpcap` development headers installed on your system for `pypcap` to build and run correctly.
fix
Install `libpcap-dev` on Debian/Ubuntu (`sudo apt-get install libpcap-dev`), `libpcap-devel` on RHEL/CentOS (`sudo yum install libpcap-devel`), or `libpcap` on macOS (`brew install libpcap`).
affects: All versions
gotchaCapturing raw network packets typically requires elevated privileges (e.g., root/administrator) due to security restrictions on accessing network interfaces directly.
fix
Run your Python script with `sudo` (e.g., `sudo python your_script.py`) or configure `setcap` on Linux to grant `cap_net_raw,cap_net_admin` capabilities to your Python executable.
affects: All versions
breakingPython 3 support was introduced in version 1.2.0. Versions prior to 1.2.0 are Python 2 exclusive and will not work with Python 3 interpreters.
fix
For Python 3 projects, ensure `pypcap` is version 1.2.0 or higher. If you must use Python 2, ensure `pypcap` is below 1.2.0.
affects: <1.2.0
gotchaAn iteration bug affecting `for ts, pkt in pc:` loops was fixed in version 1.2.0, potentially leading to incorrect or incomplete packet sequences in older versions.
fix
Upgrade `pypcap` to version 1.2.0 or higher to ensure correct and reliable packet iteration behavior.
affects: <1.2.0
Upgrade
Version history
1.3.0latest on PyPI · released Sep 12, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
7
Resources
pypcap — pip install pypcap · libregistry