Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Class and tools for handling of IPv4 and IPv6 addresses and networks

Project Description
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:

What's new

Version 0.83 (2015-04-04)
* Add carrier grade NAT ranges
* Unbreak lots of packing systems by not having a letter in the release version

Version 0.82a (2014-10-07)
* Fix version numbers in files
* Correct -> next(x) python3 compatability

Version 0.82 (2014-10-06)

* Add support for array slices
* Add __and__ and isdisjoint for IPSet
* Fix a bug in IPSet where contains may incorrectly return false
* Added some fuzz testing

Version 0.81 (2013-04-08)

* Correct reverseName() for IPv6 addresses, so IP('::1').reverseName() returns correct.
* Add network mask awareness to v46map()
* Fix Python 3 errors in IPSet class
* Make IPSet base class be object when MutableSet isn't available, fixing
errors in Python 2.5

Version 0.80 (2013-03-26)

* Drop support of Python older than 2.4
* Python 3 does not need 2to3 conversion anymore (same code base)
* Fix adding of non-adjacent networks: + made
* Fix adding networks that don't create a valid subnet: + made
* Fix adding with an IPv6 address where .int() was < 32 bits made IPy believe it
was an IPv4 address:
::ffff:0/112 + ::1:0:0/112 made
* Add support of IPSets
* Add support for subtracting a network range
* Prevent IPv4 and IPv6 ranges from saying they contain each other
* Add a .v46map() method to convert mapped address ranges
such as IP('::ffff:'); RFC 4291
* Change sort order to more natural:
IPv4 before IPv6; less-specific prefixes first (/0 before /32)

Version 0.76 (2013-03-19)

* ip == other and ip != other doesn't fail with an exception anymore if other
is not a IP object
* Add IP.get_mac() method: get the 802.3 MAC address from IPv6 RFC 2464
* Fix IP('::/0')[0]: return an IPv6 instead of an IPv4 address

Version 0.75 (2011-04-12)

* IP('::/0').netmask() gives IP('::') instead of IP('')

Version 0.74 (2011-02-16)

* Fix tests for Python 3.1 and 3.2
* ip.__nonzero__() and (ipa in ipb) return a bool instead of 0 or 1
* IP('') + IP('') raises an error, fix written by Arfrever

Version 0.73 (2011-02-15)

* Support Python 3: runs 2to3
* Update the ranges for IPv6 IPs
* Fix reverseName() and reverseNames() for IPv4 in IPv6 addresses
* Drop support of Python < 2.5

Version 0.72 (2010-11-23)

* Include examples and in source build (add them to
* Remove __rcsid__ constant from IPy module

Version 0.71 (2010-10-01)

* Use xrange() instead of range()
* Use isinstance(x, int) instead of type(x) == types.IntType
* Prepare support of Python3 (use integer division: x // y)
* Fix IP(long) constructor: ensure that the address is not too large
* Constructor raise a TypeError if the type is not int, long,
str or unicode
* is now public (belongs to APNIC)

Version 0.70 (2009-10-29)

* New "major" version because it may break compatibility
* Fix __cmp__(): IP('') and IP('') are not equal
* Fix of the network "::/0": "::" instead of "".
IPy 0.63 should fix this bug, but it wasn't.

Version 0.64 (2009-08-19)

* Create to fix bdist_rpm, fix by Robert Nickel

Version 0.63 (2009-06-23)

* Fix formatting of "IPv4 in IPv6" network, eg. IP('::ffff:'),
the netmask ("/120" in the example) was missing!

Version 0.62 (2008-07-15)

* Fix reverse DNS of IPv6 address: use "" suffix instead of
deprecated "" suffix

Version 0.61 (2008-06-12)

* Patch from Aras Vaichas allowing the [-1] operator
to work with an IP object of size 1.

Version 0.60 (2008-05-16)

* strCompressed() formats '::ffff:a.b.c.d' correctly
* Use strCompressed() instead of strFullsize() to format IP addresses,
ouput is smarter with IPv6 address
* Remove check_addr_prefixlen because it generates invalid IP address
Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
IPy-0.83.tar.gz (31.8 kB) Copy SHA256 Checksum SHA256 Source Apr 8, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting