Skip to content
This repository was archived by the owner on Apr 23, 2026. It is now read-only.

1995parham-learning/ipdemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

ipdemo

A minimal DHCP-style IP address allocator written in Python. It assigns host IPs from a given IPv4/IPv6 subnet, tracks which addresses are in use, and releases them on request.

Requirements

  • Python 3.10+ (uses PEP 604 X | None unions and built-in set[...] generics)
  • No third-party dependencies — only the standard-library ipaddress module

Usage

from main import DHCP

dhcp = DHCP("192.168.1.0/29")

# Allocate a single address
ip = dhcp.allocate()

# Allocate several at once (stops early if the pool is exhausted)
ips = dhcp.allocate_multiple(5)

# Release an address back to the pool
dhcp.release("192.168.1.1")

# Validate a string as an IP address
DHCP.is_valid_ip("192.168.1.10")  # True

Run the built-in demo:

python main.py

Expected output (for the 192.168.1.0/29 subnet, which has 6 usable hosts):

['192.168.1.1', '192.168.1.2', '192.168.1.3', '192.168.1.4', '192.168.1.5']
True

API

DHCP(subnet: str)

Create an allocator bound to the given subnet (e.g. "192.168.1.0/29" or an IPv6 network). The network object is stored on self.network and the set of allocated addresses on self.allocated.

allocate() -> IPAddress | None

Return the first unused host address in the subnet, marking it as allocated. Returns None if the pool is exhausted.

allocate_multiple(count: int) -> list[str]

Allocate up to count addresses and return them as strings. The returned list is shorter than count when the pool runs out mid-way.

release(ip: str | IPAddress) -> bool

Release a previously allocated address. Returns True if the address was allocated and has now been freed, False if it was not in the allocated set.

DHCP.is_valid_ip(ip: str) -> bool

Static helper that returns True if ip parses as a valid IPv4 or IPv6 address.

Notes & limitations

  • Allocation scans network.hosts() from the start on every call, so it is O(n) per allocation. Fine for small subnets, not ideal for large ones.
  • State is held entirely in memory — nothing is persisted between runs.
  • The class does not implement the DHCP wire protocol; it only models the address-pool bookkeeping a DHCP server would do.

About

Network automation interview

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages