PyDivert

3.1.0 · active · verified Sun Apr 12

PyDivert is a powerful Python binding for the WinDivert driver, enabling user-mode applications to capture, modify, and drop network packets on Windows. It offers features like advanced filtering, on-the-fly packet manipulation, and re-injection into the network stack. Version 3.1.0 is the current release, and the library demonstrates an active release cadence, with major updates supporting modern Python features (like asyncio) and WinDivert 2.2+ capabilities, including bundled driver binaries.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to capture outbound TCP packets destined for port 80 and then re-inject them back into the network stack. It highlights the basic usage of `pydivert.WinDivert` as a context manager and iterating over captured packets. Remember to run this with administrator privileges.

import pydivert
import os

# NOTE: This script requires administrator privileges to run.
# On Windows, you might need to run your terminal/IDE as Administrator.

# Example: Capture and re-inject all outbound TCP packets to port 80 (HTTP).
# Packets captured are removed from the network stack; they must be re-injected to proceed.
with pydivert.WinDivert("tcp.DstPort == 80 and outbound") as w:
    print("Capturing outbound TCP packets to port 80. Press Ctrl+C to stop.")
    try:
        for packet in w:
            print(f"Captured: {packet.src_addr}:{packet.src_port} -> {packet.dst_addr}:{packet.dst_port}")
            w.send(packet) # Re-inject the packet back into the stack
    except KeyboardInterrupt:
        print("\nStopped capturing.")
    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →