IPy are a Python class and tools for handling of IPv4 and IPv6 addresses and networks. It is similar to Net::IP Perl module.
Pull request Compare This branch is 4 commits ahead, 71 commits behind autocracy:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


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

Website: https://github.com/haypo/python-ipy/

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)

Compatibility and links

IPy 0.73 works on Python version 2.5-2.7.

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

Further Information might be available at:


 * better comparison (__cmp__ and friends)
 * tests for __cmp__
 * always write hex values lowercase
 * interpret 2001🔢5678:1234/64 as 2001🔢5678🔢:/64
 * move size in bits into class variables to get rid of
   some "if self._ipversion ..."
 * support for base85 encoding
 * support for output of IPv6 encoded IPv4 Addresses
 * update address type tables
 * first-last notation should be allowed for IPv6
 * add IPv6 docstring examples
 * check better for negative parameters
 * add addition / aggregation
 * move reverse name stuff out of the classes and refactor it
 * support for aggregation of more than two nets at once
 * support for aggregation with "holes"
 * support for finding common prefix
 * '>>' and '<<' for prefix manipulation
 * add our own exceptions instead ValueError all the time
 * rename checkPrefix to checkPrefixOk
 * add more documentation and doctests
 * refactor