IP Addresses

This module provides a field for IP addresses. Let’s first generate an IP field:

>>> from z3c.schema import ip
>>> myip = ip.IPAddress()

Now make sure the IP addresses validate:

>>> myip.validate(10)
Traceback (most recent call last):
...
WrongType: (10, <type 'str'>, '')
>>> myip.validate('12.123.231.wee')
Traceback (most recent call last):
...
NotValidIPAdress: 12.123.231.wee
>>> myip.validate('10.0.0.1')

Since the field uses a simple function to validate its IP addresses, it is easier to use it for the tests:

>>> from z3c.schema.ip import isValidIPAddress
>>> isValidIPAddress('0.0.0.0')
True
>>> isValidIPAddress('255.255.255.255')
True
  • Number of pieces failures

    >>> isValidIPAddress('12.3.1')
    False
    >>> isValidIPAddress('1.0.0.0.0')
    False
    >>> isValidIPAddress('1.0.0.0.')
    False
    
  • Not integers failures

    >>> isValidIPAddress('x.0.0.0')
    False
    >>> isValidIPAddress('0x8.0.0.0')
    False
    
  • Not in range failures

    >>> isValidIPAddress('-1.0.0.0')
    False
    >>> isValidIPAddress('256.0.0.0')
    False
    >>> isValidIPAddress('1.-1.256.0')
    False
    

Reference

IP Address Field Interfaces

interface z3c.schema.ip.interfaces.IIPAddress[source]

Extends: zope.schema.interfaces.IBytesLine

A valid IP address field.

exception z3c.schema.ip.interfaces.NotValidIPAdress[source]

Bases: zope.schema._bootstrapinterfaces.ValidationError

Not a valid IP address.

z3c.schema.ip.field.isValidIPAddress(addr)[source]

Returns True if the IP address is valid and False if not.

class z3c.schema.ip.field.IPAddress(min_length=0, max_length=None, **kw)[source]

Bases: zope.schema._field.BytesLine

A valid IP address.