(self, data, ipversion=0)
Create an instance of an IP object.

        Data can be a network specification or a single IP. IP
        Addresses can be specified in all forms understood by
        parseAddress.() the size of a network can be specified as

        /prefixlen        a.b.c.0/24               2001:658:22a:cafe::/64
        -lastIP           a.b.c.0-a.b.c.255        2001:658:22a:cafe::-2001:658:22a:cafe:ffff:ffff:ffff:ffff
        /decimal netmask  a.b.c.d/255.255.255.0    not supported for IPv6

        If no size specification is given a size of 1 address (/32 for
        IPv4 and /128 for IPv6) is assumed.

        >>> print IP('127.0.0.0/8')
        127.0.0.0/8
        >>> print IP('127.0.0.0/255.0.0.0')
        127.0.0.0/8
        >>> print IP('127.0.0.0-127.255.255.255')
        127.0.0.0/8

        See module documentation for more examples.
Create an instance of an IP object.
Create an instance of an IP object .
def __init__(self, data, ipversion=0):
        """Create an instance of an IP object.

        Data can be a network specification or a single IP. IP
        Addresses can be specified in all forms understood by
        parseAddress.() the size of a network can be specified as

        /prefixlen        a.b.c.0/24               2001:658:22a:cafe::/64
        -lastIP           a.b.c.0-a.b.c.255        2001:658:22a:cafe::-2001:658:22a:cafe:ffff:ffff:ffff:ffff
        /decimal netmask  a.b.c.d/255.255.255.0    not supported for IPv6

        If no size specification is given a size of 1 address (/32 for
        IPv4 and /128 for IPv6) is assumed.

        >>> print IP('127.0.0.0/8')
        127.0.0.0/8
        >>> print IP('127.0.0.0/255.0.0.0')
        127.0.0.0/8
        >>> print IP('127.0.0.0-127.255.255.255')
        127.0.0.0/8

        See module documentation for more examples.
        """

        self.NoPrefixForSingleIp = 1  # Print no Prefixlen for /32 and /128
        # Do we want prefix printed by default? see _printPrefix()
        self.WantPrefixLen = None

        netbits = 0
        prefixlen = -1

        # handling of non string values in constructor
        if isinstance(data, types.IntType) or isinstance(data, types.LongType):
            self.ip = long(data)
            if ipversion == 0:
                if self.ip < 0x100000000:
                    ipversion = 4
                else:
                    ipversion = 6
            if ipversion == 4:
                prefixlen = 32
            elif ipversion == 6:
                prefixlen = 128
            else:
                raise ValueError("only IPv4 and IPv6 supported")
            self._ipversion = ipversion
            self._prefixlen = prefixlen
        # handle IP instance as an parameter
        elif isinstance(data, IPint):
            self._ipversion = data._ipversion
            self._prefixlen = data._prefixlen
            self.ip = data.ip
        else:
            # TODO: refactor me!
            # splitting of a string into IP and prefixlen et. al.
            x = data.split('-')
            if len(x) == 2:
                # a.b.c.0-a.b.c.255 specification ?
                (ip, last) = x
                (self.ip, parsedVersion) = parseAddress(ip)
                if parsedVersion != 4:
                    raise ValueError(
                        "first-last notation only allowed for IPv4")
                (last, lastversion) = parseAddress(last)
                if lastversion != 4:
                    raise ValueError("last address should be IPv4, too")
                if last < self.ip:
                    raise ValueError(
                        "last address should be larger than first")
                size = last - self.ip
                netbits = _count1Bits(size)
            elif len(x) == 1:
                x = data.split('/')
                # if no prefix is given use defaults
                if len(x) == 1:
                    ip = x[0]
                    prefixlen = -1
                elif len(x) > 2:
                    raise ValueError("only one '/' allowed in IP Address")
                else:
                    (ip, prefixlen) = x
                    if prefixlen.find('.') != -1:
                        # check if the user might have used a netmask like
                        # a.b.c.d/255.255.255.0
                        (netmask, vers) = parseAddress(prefixlen)
                        if vers != 4:
                            raise ValueError("netmask must be IPv4")
                        prefixlen = _netmaskToPrefixlen(netmask)
            elif len(x) > 2:
                raise ValueError("only one '-' allowed in IP Address")
            else:
                raise ValueError("can't parse")

            (self.ip, parsedVersion) = parseAddress(ip)
            if ipversion == 0:
                ipversion = parsedVersion
            if prefixlen == -1:
                if ipversion == 4:
                    prefixlen = 32 - netbits
                elif ipversion == 6:
                    prefixlen = 128 - netbits
                else:
                    raise ValueError("only IPv4 and IPv6 supported")
            self._ipversion = ipversion
            self._prefixlen = int(prefixlen)

            if not _checkNetaddrWorksWithPrefixlen(self.ip, self._prefixlen, self._ipversion):
                raise ValueError(
                    "%s goes not well with prefixlen %d" %
                    (hex(self.ip), self._prefixlen))
def __init__ ( self , data , ipversion = 0 ) : self . NoPrefixForSingleIp = 1 # Print no Prefixlen for /32 and /128 # Do we want prefix printed by default? see _printPrefix() self . WantPrefixLen = None netbits = 0 prefixlen = - 1 # handling of non string values in constructor if isinstance ( data , types . IntType ) or isinstance ( data , types . LongType ) : self . ip = long ( data ) if ipversion == 0 : if self . ip < 0x100000000 : ipversion = 4 else : ipversion = 6 if ipversion == 4 : prefixlen = 32 elif ipversion == 6 : prefixlen = 128 else : raise ValueError ( "only IPv4 and IPv6 supported" ) self . _ipversion = ipversion self . _prefixlen = prefixlen # handle IP instance as an parameter elif isinstance ( data , IPint ) : self . _ipversion = data . _ipversion self . _prefixlen = data . _prefixlen self . ip = data . ip else : # TODO: refactor me! # splitting of a string into IP and prefixlen et. al. x = data . split ( '-' ) if len ( x ) == 2 : # a.b.c.0-a.b.c.255 specification ? ( ip , last ) = x ( self . ip , parsedVersion ) = parseAddress ( ip ) if parsedVersion != 4 : raise ValueError ( "first-last notation only allowed for IPv4" ) ( last , lastversion ) = parseAddress ( last ) if lastversion != 4 : raise ValueError ( "last address should be IPv4, too" ) if last < self . ip : raise ValueError ( "last address should be larger than first" ) size = last - self . ip netbits = _count1Bits ( size ) elif len ( x ) == 1 : x = data . split ( '/' ) # if no prefix is given use defaults if len ( x ) == 1 : ip = x [ 0 ] prefixlen = - 1 elif len ( x ) > 2 : raise ValueError ( "only one '/' allowed in IP Address" ) else : ( ip , prefixlen ) = x if prefixlen . find ( '.' ) != - 1 : # check if the user might have used a netmask like # a.b.c.d/255.255.255.0 ( netmask , vers ) = parseAddress ( prefixlen ) if vers != 4 : raise ValueError ( "netmask must be IPv4" ) prefixlen = _netmaskToPrefixlen ( netmask ) elif len ( x ) > 2 : raise ValueError ( "only one '-' allowed in IP Address" ) else : raise ValueError ( "can't parse" ) ( self . ip , parsedVersion ) = parseAddress ( ip ) if ipversion == 0 : ipversion = parsedVersion if prefixlen == - 1 : if ipversion == 4 : prefixlen = 32 - netbits elif ipversion == 6 : prefixlen = 128 - netbits else : raise ValueError ( "only IPv4 and IPv6 supported" ) self . _ipversion = ipversion self . _prefixlen = int ( prefixlen ) if not _checkNetaddrWorksWithPrefixlen ( self . ip , self . _prefixlen , self . _ipversion ) : raise ValueError ( "%s goes not well with prefixlen %d" % ( hex ( self . ip ) , self . _prefixlen ) )