A Python DHCP library and server implementation
Project description
pydhcp
A Python DHCP library and server implementation.
pydhcp is pure Python and targets Python 3.9 and newer. Packet parsing and
structured packet tooling are portable; actual DHCP serving still depends on OS
socket permissions and platform-specific UDP behavior.
Features
- DHCP Packet Parsing — Full support for parsing and constructing DHCP packets.
- DHCP Server Base — A simple, async-friendly server foundation with overrideable lease and option policy hooks.
- DHCP Client & Capture Tools — Basic packet-client builders and a tshark-like capture command for troubleshooting.
Installation
pip install pydhcp
TOML packet encode/decode support is optional. Install pydhcp[toml] if you want
pydhcp packet --toml; JSON, YAML, and INI support remain available with the base package.
Quick start
Synchronous Server
from pydhcp.server import DhcpServer
server = DhcpServer(listen="*")
server.listen()
The built-in server intentionally keeps allocation policy small. It renews existing leases
and responds to client-requested addresses, while applications can subclass DhcpServer
or provide a custom lease backend for pools, reservations, and site-specific options.
You can also bind explicit endpoints or multiple ports when you do not want wildcard behavior:
server = DhcpServer(listen=[("127.0.0.1", [6767, 6768])], per_interface=True)
server.listen()
Asynchronous Server
import asyncio
from pydhcp.server import AsyncDhcpServer
async def main():
server = AsyncDhcpServer()
await server.start()
# Keep running or handle other async tasks
# To stop: await server.stop()
asyncio.run(main())
Basic Packet Client
DhcpClient is a packet-level helper for tests and troubleshooting. It sends DHCP
messages and queues matching replies, but it does not configure host network interfaces.
from pydhcp.client import DhcpClient
client = DhcpClient(listen=("127.0.0.1", 6768))
discover = client.build_discover(b"\x00\x11\x22\x33\x44\x55")
client.send(discover, destination="127.0.0.1", port=6767)
Command Line Interface (CLI)
pydhcp includes a command line interface for listing network adapters, decoding packets, and starting servers.
# List all network interfaces
pydhcp interfaces
# Decode a hex-encoded DHCP packet from stdin as JSON
pydhcp packet --decode --input - --format json
# Decode a hex-encoded DHCP packet from stdin as a compact text summary
pydhcp packet --decode --input - --format summary
# Encode structured packet text back to hex
pydhcp packet --encode --input packet.json --format json --output packet.hex
# Capture DHCPDISCOVER packets as newline-delimited JSON on stdout
pydhcp capture --listen 127.0.0.1:6767 --filter msg_type=DHCPDISCOVER --output -
# Write one structured file per capture
pydhcp capture --listen 127.0.0.1:6767 --output "output/{client_id}/{timestamp}_{msg_type}.{format}" --output-mode per-capture --format json
# Invoke a trusted local command hook with each captured packet on stdin
pydhcp capture --listen 127.0.0.1:6767 --hook ./on-dhcp-capture
# Start the DHCP server from JSON or INI config
pydhcp server --config config.json
# Listen on multiple explicit endpoints while debugging
pydhcp server --listen 127.0.0.1:6767,127.0.0.1:6768
# Increase logging while debugging
pydhcp server --listen 127.0.0.1:6767 --log-level debug
Development
See development notes for environment setup, dependency install, and test commands.
For comprehensive validation in GitHub Actions, the test workflow also supports
manual workflow_dispatch runs and safe ci-* tags. Benchmarks stay repo-local and opt-in:
use the workflow's run_benchmarks input or a ci-bench-* tag when you want
the benchmark harness included. The repository wrapper and individual benchmark scripts can also
write structured JSON reports for local comparison or CI artifact upload:
.\.venv\3.12.10\Scripts\python.exe benchmarks\run.py --suite parse --iterations 10000 --json-output benchmark-results/bench_parse.json
.\.venv\3.12.10\Scripts\python.exe benchmarks\run.py --suite options --iterations 1000 --json-output benchmark-results/bench_options.json
.\.venv\3.12.10\Scripts\python.exe benchmarks\bench_parse.py --iterations 10000 --json-output benchmark-results/bench_parse.json
.\.venv\3.12.10\Scripts\python.exe benchmarks\bench_options.py --iterations 1000 --json-output benchmark-results/bench_options.json
Releasing
This project follows Semantic Versioning and keeps a
CHANGELOG.md. Pushing a tag matching v* triggers the release
workflow.
Documentation site
MkDocs builds the API reference from docs/, published on every release. The docs also include a "Common DHCP Options" page with typed examples.
License
MIT — see LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydhcp-0.4.1.tar.gz.
File metadata
- Download URL: pydhcp-0.4.1.tar.gz
- Upload date:
- Size: 104.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc1ed729315f56b51c8bf67087c9e03b9d7f7247fd20ed820b6b19eb00fc7726
|
|
| MD5 |
1d94959d8f237438da2323bbba23ec2f
|
|
| BLAKE2b-256 |
a7a4ffe1587f1df88f01752cea23b6359999777ac2146824713fe485aa1ca14c
|
Provenance
The following attestation bundles were made for pydhcp-0.4.1.tar.gz:
Publisher:
release.yml on jose-pr/pydhcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydhcp-0.4.1.tar.gz -
Subject digest:
bc1ed729315f56b51c8bf67087c9e03b9d7f7247fd20ed820b6b19eb00fc7726 - Sigstore transparency entry: 2218345488
- Sigstore integration time:
-
Permalink:
jose-pr/pydhcp@e9a517c079ac907d9d75f33f1a06f4dc4e192981 -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/jose-pr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e9a517c079ac907d9d75f33f1a06f4dc4e192981 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pydhcp-0.4.1-py3-none-any.whl.
File metadata
- Download URL: pydhcp-0.4.1-py3-none-any.whl
- Upload date:
- Size: 84.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb637794bddda69b16603ee557655174b7860d04d569e4694c03797708163392
|
|
| MD5 |
a54cdf955e4ce9f1e50d26e42ed6cfb5
|
|
| BLAKE2b-256 |
8ee2eb5895fe9945bd283dd857bd9915dbeb1a53fe211afea594cea49a228f58
|
Provenance
The following attestation bundles were made for pydhcp-0.4.1-py3-none-any.whl:
Publisher:
release.yml on jose-pr/pydhcp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydhcp-0.4.1-py3-none-any.whl -
Subject digest:
fb637794bddda69b16603ee557655174b7860d04d569e4694c03797708163392 - Sigstore transparency entry: 2218345850
- Sigstore integration time:
-
Permalink:
jose-pr/pydhcp@e9a517c079ac907d9d75f33f1a06f4dc4e192981 -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/jose-pr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@e9a517c079ac907d9d75f33f1a06f4dc4e192981 -
Trigger Event:
push
-
Statement type: