TheAlgorithms/Python · error · ValueError
Invalid IPv4 address format
Error message
Invalid IPv4 address format
What it means
Raised by ipv4_to_decimal(ipv4_address) in conversions/ipv4_conversion.py:30 when splitting the address on '.' does not yield exactly 4 octets. The function is strict about dotted-quad shape: too few parts ('10.0.255'), too many ('10.0.0.1.5'), or a missing/wrong separator all produce ValueError('Invalid IPv4 address format'). Note each part must also parse with int(), so non-numeric parts raise int's own ValueError first.
Source
Thrown at conversions/ipv4_conversion.py:30
int: The decimal representation of the IP address.
>>> ipv4_to_decimal("192.168.0.1")
3232235521
>>> ipv4_to_decimal("10.0.0.255")
167772415
>>> ipv4_to_decimal("10.0.255")
Traceback (most recent call last):
...
ValueError: Invalid IPv4 address format
>>> ipv4_to_decimal("10.0.0.256")
Traceback (most recent call last):
...
ValueError: Invalid IPv4 octet 256
"""
octets = [int(octet) for octet in ipv4_address.split(".")]
if len(octets) != 4:
raise ValueError("Invalid IPv4 address format")
decimal_ipv4 = 0
for octet in octets:
if not 0 <= octet <= 255:
raise ValueError(f"Invalid IPv4 octet {octet}") # noqa: EM102
decimal_ipv4 = (decimal_ipv4 << 8) + int(octet)
return decimal_ipv4
def alt_ipv4_to_decimal(ipv4_address: str) -> int:
"""
>>> alt_ipv4_to_decimal("192.168.0.1")
3232235521
>>> alt_ipv4_to_decimal("10.0.0.255")
167772415
"""
return int("0x" + "".join(f"{int(i):02x}" for i in ipv4_address.split(".")), 16)View on GitHub (pinned to f5988cc097)
Solutions
- Strip CIDR/port decorations first: ipv4 = addr.split('/')[0].rsplit(':', 1)[0]
- Validate shape before calling: len(addr.split('.')) == 4 and each part is decimal 0-255 (or use ipaddress.IPv4Address)
- Route IPv6 through ipaddress.IPv6Address instead of this function
Example fix
# before
ipv4_to_decimal('192.168.0.1/24') # ValueError
# after
import ipaddress
ipv4_to_decimal(str(ipaddress.ip_interface('192.168.0.1/24').ip)) Defensive patterns
Strategy: validation
Validate before calling
import ipaddress
try:
ipaddress.IPv4Address(ipv4_address)
except ipaddress.AddressValueError as e:
raise ValueError(f'not a plain IPv4 address: {ipv4_address!r}') from e Try / catch
try:
ipv4_to_decimal(ipv4_address)
except ValueError as e:
raise ValueError(f'bad address {ipv4_address!r}: {e}') from e Prevention
- Split CIDR (addr/mask) and host:port before conversion
- Prefer ipaddress.IPv4Address as the canonical validator
- When building addresses by join('.'), assert len(parts) == 4
When it happens
Trigger: ipv4_to_decimal('10.0.255'), ipv4_to_decimal('10.0.0.1.5'), ipv4_to_decimal('192.168.0.1/24') (CIDR suffix becomes a 5th part), or addresses built by joining the wrong list length.
Common situations: Passing CIDR notation or addresses with port ('1.2.3.4:80'); constructing IPs from split() output without rechecking part count; trailing dots; IPv6 strings reaching an IPv4-only function.
Related errors
- Invalid IPv4 octet {octet}
- Invalid decimal IPv4 address
- base must be >= 2
- Input value is not an integer
- Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/2f51a6423feb9c9b.
Report an issue: GitHub.