Python Glossary Русский

Python 3.3+ipaddressexception

ipaddress.AddressValueError

exceptions

The exception for a malformed address; a ValueError subclass raised by the IPv4Address, IPv6Address and network constructors.

Syntax

except ipaddress.AddressValueError:

The ip_address() and ip_network() factories do not let it out: after trying both versions they raise a plain ValueError, so except AddressValueError around them catches nothing. In code that accepts either version, catch ValueError, which covers both cases. The message names the exact cause: an octet above 255, leading zeros, stray characters.

Examples

import ipaddress
try:
    ipaddress.IPv4Address('1.2.3.4 ')
except ipaddress.AddressValueError as e:
    print(e)  # → Only decimal digits permitted in '4 ' in '1.2.3.4 '
try:
    ipaddress.ip_address('1.2.3.4 ')
except ipaddress.AddressValueError:
    print('не сюда')
except ValueError as e:
    print(type(e).__name__)  # → ValueError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy