Python Setup
Why uv
Section titled “Why uv”uv is a Python package manager written in Rust. It installs packages 10 to 100 times faster than pip and creates isolated virtual environments automatically. All code examples in this book run inside a single uv-managed environment.
Install uv
Section titled “Install uv”curl -LsSf https://astral.sh/uv/install.sh | shsource $HOME/.local/bin/envcurl -LsSf https://astral.sh/uv/install.sh | shsource $HOME/.local/bin/env# or with Homebrew:brew install uvpowershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"Create the Environment
Section titled “Create the Environment”Run this once. It creates a virtual environment and installs every package used in the book:
uv init networking-book-envcd networking-book-envuv add scapy netmiko pysnmp dnspython ntplib pymodbus asyncuaActivate the Environment
Section titled “Activate the Environment”source .venv/bin/activatesource .venv/bin/activate.venv\Scripts\activateScapy Raw Socket Permissions
Section titled “Scapy Raw Socket Permissions”Scapy needs raw socket access to capture and send frames.
Grant cap_net_raw to the Python interpreter so you do not need to run as root:
sudo setcap cap_net_raw+eip $(which python3)Run capture scripts with sudo:
sudo python3 your_script.pyInstall Npcap before running any Scapy capture scripts. Run scripts as Administrator.
Packages Used
Section titled “Packages Used”| Package | Used in | Purpose |
|---|---|---|
scapy | Ch 1, 2, 3, 4, 13 | Frame capture, crafting, and protocol analysis |
netmiko | Ch 1, 2, 4 | SSH automation for Hirschmann HiOS switches |
pysnmp | Ch 5 | SNMP polling and trap reception |
dnspython | Ch 5 | DNS queries and diagnostics |
ntplib | Ch 5 | NTP offset measurement |
pymodbus | Ch 9 | Modbus TCP client (sync and async) |
asyncua | Ch 9 | OPC UA client with security support |
Verify the Installation
Section titled “Verify the Installation”import scapy, netmiko, pysnmp, dns, ntplib, pymodbus, asyncuaprint("All packages installed.")References
Section titled “References”- uv documentation: https://docs.astral.sh/uv/
- Scapy documentation: https://scapy.readthedocs.io/
- Netmiko documentation: https://github.com/ktbyers/netmiko