01 / ADDRESSING AND INVENTORY · LESSON 01

Read an IP address precisely

Validate exported IPv4 and IPv6 addresses without guessing from their spelling.

15 min · learn at your pace

An address is more than a pattern

An IPv4 address has four decimal octets, each with a permitted range. A regular expression that merely recognises digits and dots can accept impossible values. Python's ipaddress.ip_address parses the value and returns an IPv4Address or IPv6Address object. Invalid input raises ValueError.

The function also provides a consistent text representation. An IPv6 address written with expanded zero groups can represent the same address as its compressed form. Use the parsed object to normalize that spelling rather than inventing string replacement rules.

Keep rejected records traceable

This export format accepts address strings only. Strip surrounding whitespace, reject IPv4 leading zeros, and reject zone identifiers such as %eth0. Scoped IPv6 addresses have legitimate uses elsewhere, but this course's exported addresses do not carry an interface zone. A subnet prefix such as /24 belongs to a network field, not this address field.

Return accepted records and errors separately, retaining the original one-based position. Do not deduplicate: two inventory records may intentionally contain the same address, and a later lesson will consider routing context when deciding whether overlap is a problem.

All addresses are supplied data. Parsing an address does not resolve a name, send a packet, or prove that a device exists at that address. The browser needs no network access for this task.

Try an example.py
from ipaddress import ip_address
for text in ["192.0.2.10", "2001:0DB8:0:0::10"]:
    address = ip_address(text)
    print(str(address), address.version)
YOUR TURN

Put it into practice.

Define normalize_addresses(values), returning (accepted, errors). Accept string IPv4/IPv6 addresses only, stripping surrounding whitespace; reject zone IDs (%), prefixes, invalid octets, leading-zero IPv4 and non-string values. Accepted rows contain record (one-based input position), address (canonical text) and version (4 or 6). Errors contain record and reason "invalid address". Preserve input order and duplicates; do not change the input.

See an explained solution
One way to solve it.py
from ipaddress import ip_address

def normalize_addresses(values):
    accepted, errors = [], []
    for number, value in enumerate(values, 1):
        try:
            if not isinstance(value, str) or "%" in value:
                raise ValueError("Use an unscoped address string")
            address = ip_address(value.strip())
        except ValueError:
            errors.append({"record": number, "reason": "invalid address"})
        else:
            accepted.append({"record": number, "address": str(address), "version": address.version})
    return accepted, errors

if __name__ == "__main__":
    print(normalize_addresses(["192.0.2.10", "2001:0db8::10", "192.0.2.999"]))

The parser validates address structure and normalizes IPv6 spelling. Record numbers preserve the link to the original export, while rejected values remain visible.