Python Glossary Русский

Python 3.3+ipaddressexception

ipaddress.NetmaskValueError

exceptions

The exception for an invalid netmask or prefix; a ValueError subclass raised by the IPv4Network and IPv6Network constructors.

Syntax

except ipaddress.NetmaskValueError:

It only covers a bad mask itself, such as '/33' or '/255.0.255.0'. Host bits set ('10.0.0.1/24') is a plain ValueError, and the ip_network() factory turns a bad mask into a plain ValueError too. Catching ValueError is the safe default; use NetmaskValueError when calling a constructor directly and you need to single out a mask error.

Examples

import ipaddress
for text in ['10.0.0.0/33', '10.0.0.1/24']:
    try:
        ipaddress.IPv4Network(text)
    except ipaddress.NetmaskValueError as e:
        print('маска:', e)
    except ValueError as e:
        print('другое:', e)
# → маска: '33' is not a valid netmask / другое: 10.0.0.1/24 has host bits set

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy