Skip to content


Subversion checkout URL

You can clone with
Download ZIP
IPy are a Python class and tools for handling of IPv4 and IPv6 addresses and networks. It is similar to Net::IP Perl module.
Python Makefile
Latest commit 5d002a8 @autocracy Merge pull request #38 from chrismeyersfsu/fix-127_localhost


IPy - class and tools for handling of IPv4 and IPv6 addresses and networks.


Presentation of the API

The IP class allows a comfortable parsing and handling for most
notations in use for IPv4 and IPv6 addresses and networks. It was
greatly inspired by RIPE's Perl module NET::IP's interface but
doesn't share the implementation. It doesn't share non-CIDR netmasks,
so funky stuff like a netmask of 0xffffff0f can't be done here.

    >>> from IPy import IP
    >>> ip = IP('')
    >>> for x in ip:
    ...  print(x)
    >>> ip2 = IP('0x7f000000/30')
    >>> ip == ip2
    >>> ip.reverseNames()
    ['', '', '', '']
    >>> ip.reverseName()
    >>> ip.iptype()

Supports most IP address formats

It can detect about a dozen different ways of expressing IP addresses
and networks, parse them and distinguish between IPv4 and IPv6 addresses:

    >>> IP('').version()
    >>> IP('::1').version()

IPv4 addresses

    >>> print(IP(0x7f000001))
    >>> print(IP('0x7f000001'))
    >>> print(IP(''))
    >>> print(IP('10'))

IPv6 addresses

    >>> print(IP('1080:0:0:0:8:800:200C:417A'))
    >>> print(IP('1080::8:800:200C:417A'))
    >>> print(IP('::1'))
    >>> print(IP('::'))

Network mask and prefixes

    >>> print(IP(''))
    >>> print(IP(''))
    >>> print(IP(''))

Derive network address

IPy can transform an IP address into a network address by applying the given
>>> print(IP('', make_net=True))

This can also be done for existing IP instances:
>>> print(IP('').make_net(''))

Convert address to string

Nearly all class methods which return a string have an optional
parameter 'wantprefixlen' which controls if the prefixlen or netmask
is printed. Per default the prefilen is always shown if the network
contains more than one address::

    wantprefixlen == 0 / None     don't return anything
    wantprefixlen == 1            /prefix       
    wantprefixlen == 2            /netmask      
    wantprefixlen == 3            -lastip       

You can also change the defaults on an per-object basis by fiddling with
the class members:

 * NoPrefixForSingleIp
 * WantPrefixLen

Examples of string conversions:

    >>> IP('').strNormal()
    >>> IP('').strNormal()
    >>> IP('').strNormal(0)
    >>> IP('').strNormal(1)
    >>> IP('').strNormal(2)
    >>> IP('').strNormal(3)
    >>> ip = IP('')
    >>> print(ip)
    >>> ip.NoPrefixForSingleIp = None
    >>> print(ip)
    >>> ip.WantPrefixLen = 3
    >>> print(ip)

Work with multiple networks

Simple addition of neighboring netblocks that can be aggregated will yield
a parent network of both, but more complex range mapping and aggregation
requires is available with the IPSet class which will hold any number of
unique address ranges and will aggregate overlapping ranges.

    >>> from IPy import IP, IPSet
    >>> IP('') - IP('')
    IPSet([IP(''), IP('')])
    >>> IPSet([IP(''), IP(''), IP('')])
    >>> s = IPSet([IP('')])
    >>> s.add(IP(''))
    >>> s
    IPSet([IP(''), IP('')])
    >>> s.discard(IP(''))
    >>> s
    IPSet([IP(''), IP(''), IP(''), IP('')])

IPSet supports the `set` method `isdisjoint`:

    >>> s.isdisjoint(IPSet([IP('')]))
    >>> s.isdisjoint(IPSet([IP('')]))

IPSet supports intersection:

    >>> s & IPSet([IP('')])

Compatibility and links

IPy 0.83 works on Python version 2.6 - 3.4.

The IP module should work in Python 2.5 as long as the subtraction operation
is not used. IPSet requires features of the collecitons class which appear
in Python 2.6, though they can be backported.


When using IPv6 addresses, it is best to compare using IP().len() instead of
len(IP). Addresses with an integer value > 64 bits can break the 2nd method.
See for more info.

Fuzz testing for IPSet will throw spurious errors when the IPSet module
combines two smaller prefixes into a larger prefix that matches the random
prefix tested against.

This Python module is under BSD license: see COPYING file.

Further Information might be available at:
Something went wrong with that request. Please try again.