Skip to main content

A Python DHCP library and server implementation

Project description

pydhcp

Version Python versions License: MIT Docs

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.

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())

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 --stdin --json

# Decode a hex-encoded DHCP packet from stdin as a compact text summary
pydhcp packet --decode --stdin --summary

# Encode structured packet text back to hex
pydhcp packet --encode --input-file packet.json --json --output-file packet.hex

# 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydhcp-0.3.0.tar.gz (75.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydhcp-0.3.0-py3-none-any.whl (55.9 kB view details)

Uploaded Python 3

File details

Details for the file pydhcp-0.3.0.tar.gz.

File metadata

  • Download URL: pydhcp-0.3.0.tar.gz
  • Upload date:
  • Size: 75.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pydhcp-0.3.0.tar.gz
Algorithm Hash digest
SHA256 13be6cc8bffd34702e3152870195ba8742da53b7bb7e5e960ca45e28815a3e27
MD5 0fc8fdcfc5f90248ab293ca51e30167b
BLAKE2b-256 1696d9dadae16ec3cb68b923a8d512fbe0fe6674c802f29624173ba4eb700e4f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydhcp-0.3.0.tar.gz:

Publisher: release.yml on jose-pr/pydhcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydhcp-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pydhcp-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 55.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pydhcp-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf6af2d7fd45a8212b2c3d90a39f3af01f68906476c724d8e839e3ecb9842e05
MD5 137e928b295dfc2c3c0dafae70d05661
BLAKE2b-256 05be0b78f1037a38c061a20931500548960f497d635bda0eae4edb207ab25248

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydhcp-0.3.0-py3-none-any.whl:

Publisher: release.yml on jose-pr/pydhcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page