Package web2py :: Package gluon :: Module validators :: Class IS_IPV4
[hide private]
[frames] | no frames]

Class IS_IPV4

source code

object --+    
         |    
 Validator --+
             |
            IS_IPV4


Checks if field's value is an IP version 4 address in decimal form. Can
be set to force addresses from certain range.

IPv4 regex taken from: http://regexlib.com/REDetails.aspx?regexp_id=1411

Arguments:

minip: lowest allowed address; accepts:
       str, eg. 192.168.0.1
       list or tuple of octets, eg. [192, 168, 0, 1]
maxip: highest allowed address; same as above
invert: True to allow addresses only from outside of given range; note
        that range boundaries are not matched this way
is_localhost: localhost address treatment:
              None (default): indifferent
              True (enforce): query address must match localhost address
                              (127.0.0.1)
              False (forbid): query address must not match localhost
                              address
is_private: same as above, except that query address is checked against
            two address ranges: 172.16.0.0 - 172.31.255.255 and
            192.168.0.0 - 192.168.255.255
is_automatic: same as above, except that query address is checked against
              one address range: 169.254.0.0 - 169.254.255.255

Minip and maxip may also be lists or tuples of addresses in all above
forms (str, int, list / tuple), allowing setup of multiple address ranges:

    minip = (minip1, minip2, ... minipN)
               |       |           |
               |       |           |
    maxip = (maxip1, maxip2, ... maxipN)

Longer iterable will be truncated to match length of shorter one.

Examples::

    #Check for valid IPv4 address:
    INPUT(_type='text', _name='name', requires=IS_IPV4())

    #Check for valid IPv4 address belonging to specific range:
    INPUT(_type='text', _name='name',
        requires=IS_IPV4(minip='100.200.0.0', maxip='100.200.255.255'))

    #Check for valid IPv4 address belonging to either 100.110.0.0 -
    #100.110.255.255 or 200.50.0.0 - 200.50.0.255 address range:
    INPUT(_type='text', _name='name',
        requires=IS_IPV4(minip=('100.110.0.0', '200.50.0.0'),
                         maxip=('100.110.255.255', '200.50.0.255')))

    #Check for valid IPv4 address belonging to private address space:
    INPUT(_type='text', _name='name', requires=IS_IPV4(is_private=True))

    #Check for valid IPv4 address that is not a localhost address:
    INPUT(_type='text', _name='name', requires=IS_IPV4(is_localhost=False))

>>> IS_IPV4()('1.2.3.4')
('1.2.3.4', None)
>>> IS_IPV4()('255.255.255.255')
('255.255.255.255', None)
>>> IS_IPV4()('1.2.3.4 ')
('1.2.3.4 ', 'enter valid IPv4 address')
>>> IS_IPV4()('1.2.3.4.5')
('1.2.3.4.5', 'enter valid IPv4 address')
>>> IS_IPV4()('123.123')
('123.123', 'enter valid IPv4 address')
>>> IS_IPV4()('1111.2.3.4')
('1111.2.3.4', 'enter valid IPv4 address')
>>> IS_IPV4()('0111.2.3.4')
('0111.2.3.4', 'enter valid IPv4 address')
>>> IS_IPV4()('256.2.3.4')
('256.2.3.4', 'enter valid IPv4 address')
>>> IS_IPV4()('300.2.3.4')
('300.2.3.4', 'enter valid IPv4 address')
>>> IS_IPV4(minip='1.2.3.4', maxip='1.2.3.4')('1.2.3.4')
('1.2.3.4', None)
>>> IS_IPV4(minip='1.2.3.5', maxip='1.2.3.9', error_message='bad ip')('1.2.3.4')
('1.2.3.4', 'bad ip')
>>> IS_IPV4(maxip='1.2.3.4', invert=True)('127.0.0.1')
('127.0.0.1', None)
>>> IS_IPV4(maxip='1.2.3.4', invert=True)('1.2.3.4')
('1.2.3.4', 'enter valid IPv4 address')
>>> IS_IPV4(is_localhost=True)('127.0.0.1')
('127.0.0.1', None)
>>> IS_IPV4(is_localhost=True)('1.2.3.4')
('1.2.3.4', 'enter valid IPv4 address')
>>> IS_IPV4(is_localhost=False)('127.0.0.1')
('127.0.0.1', 'enter valid IPv4 address')
>>> IS_IPV4(maxip='100.0.0.0', is_localhost=True)('127.0.0.1')
('127.0.0.1', 'enter valid IPv4 address')



Instance Methods [hide private]
 
__init__(self, minip='0.0.0.0', maxip='255.255.255.255', invert=True, is_localhost=1, is_private=1, is_automatic=1, error_message='enter valid IPv4 address')
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__call__(self, value) source code

Inherited from Validator: formatter

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Class Variables [hide private]
  regex = re.compile(r'^(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.){3}...
  numbers = (16777216, 65536, 256, 1)
  localhost = 2130706433
  private = ((2886729728, 2886795263), (3232235520, 3232301055))
  automatic = (2851995648, 2852061183)
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, minip='0.0.0.0', maxip='255.255.255.255', invert=True, is_localhost=1, is_private=1, is_automatic=1, error_message='enter valid IPv4 address')
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

Class Variable Details [hide private]

regex

Value:
re.compile(r'^(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d\d|\
2[0-4]\d|25[0-5])$')