Counting available host IP addresses - a classful approach

Counting available host IP addresses - a classful approach

Breaking it down

These two functions take two IP addresses from a string and splits them into four octets, then it converts the octets to a packed IP/integer (octet value * 256^octet) and sums the values to return how many host addresses are available between the two.

def ips_between(start, end):
    end_octets = [ int(octet) for octet in end.split(".") ] # Making binary octets from the strings
    start_octets = [ int(octet) for octet in start.split(".") ]
    return ip_to_packed_int(end_octets) - ip_to_packed_int(start_octets)


def ip_to_packed_int(bin_octets: list)->int:
    packed_ip = []
    power = 3
    for i in bin_octets:
        packed_ip.append((int(i) * (256 ** power)))
        power -= 1
    return sum(packed_ip)
Show Comments