{"library":"pure-pcapy3","title":"Pure-pcapy3","description":"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.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install pure-pcapy3"],"cli":null},"imports":["import pcapy"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import pcapy\nimport sys\n\ndef packet_handler(header, data):\n    # This is a very basic handler. Real-world usage would parse 'data'.\n    print(f\"Captured packet! Length: {header.getlen()} bytes\")\n\n# Try to open 'eth0' or 'en0' (common interface names)\n# Fallback to an example pcap file if live capture fails or is not desired.\ninterface = 'eth0' # Change to your network interface, e.g., 'wlan0', 'en0'\ntry:\n    # Open live capture on a network interface\n    # interface, snaplen, promiscuous_mode, timeout_ms\n    pc = pcapy.open_live(interface, 1500, True, 100)\n    print(f\"Listening on interface: {interface}...\")\n    print(\"Press Ctrl+C to stop.\")\n    pc.loop(0, packet_handler) # 0 for infinite loop\nexcept pcapy.PcapError as e:\n    print(f\"Error opening live capture on {interface}: {e}\")\n    print(\"Trying to open a dummy pcap file instead.\")\n    # Fallback to a dummy pcap file for demonstration if live capture fails\n    # (e.g., no permissions, interface not found)\n    try:\n        # For a runnable example without requiring live interface access, you'd usually have a .pcap file.\n        # This example assumes you might have 'test.pcap' or similar.\n        # If not, this part will also fail.\n        # Create a dummy pcap file if it doesn't exist for the example:\n        import os\n        dummy_pcap_path = 'dummy.pcap'\n        if not os.path.exists(dummy_pcap_path):\n            print(f\"Warning: '{dummy_pcap_path}' not found. Cannot demonstrate file reading.\")\n        else:\n            print(f\"Opening pcap file: {dummy_pcap_path}\")\n            pc_file = pcapy.open_offline(dummy_pcap_path)\n            pc_file.loop(0, packet_handler)\n    except pcapy.PcapError as e_file:\n        print(f\"Error opening pcap file '{dummy_pcap_path}': {e_file}\")\n        print(\"Please ensure you have necessary permissions or a valid pcap file.\")\nexcept KeyboardInterrupt:\n    print(\"Capture stopped by user.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"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).","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}