IPy
IPy is a Python library providing classes and tools for handling IPv4 and IPv6 addresses and networks. It aims to offer a comfortable parsing and manipulation interface for various IP address notations, similar to Perl's Net::IP module. The current version is 1.01, and it appears to be in maintenance mode with no recent updates.
Warnings
- breaking IPy version 1.01 officially supports Python versions 2.6 through 3.7. Using it with newer Python versions (3.8+) may lead to compatibility issues or unexpected behavior as it has not been updated since December 2020.
- gotcha When comparing IPv6 addresses, using `IP().len()` for comparison is recommended over `len(IP)`. Addresses with an integer value greater than 64 bits can cause incorrect results with `len(IP)`.
- gotcha Fuzz testing with `IPSet` may produce spurious errors when the `IPSet` module aggregates two smaller prefixes into a larger one that matches the random prefix being tested against. This is noted as an 'Errata' in the package description.
- deprecated The `ipaddress` module, providing similar functionality for IPv4 and IPv6 address manipulation, has been part of the Python standard library since Python 3.3. For new projects on Python 3.3 and above, `ipaddress` is generally the preferred choice due to its native integration and ongoing maintenance.
Install
-
pip install ipy
Imports
- IP
from IPy import IP
- IPSet
from IPy import IP, IPSet
Quickstart
from IPy import IP, IPSet
# Working with individual IP addresses and networks
ip_network = IP('192.168.1.0/24')
print(f"Network: {ip_network}")
print(f"Version: {ip_network.version()}")
for ip_addr in ip_network:
print(f" Host: {ip_addr}")
ip_host = IP('::1')
print(f"IPv6 Host: {ip_host}")
print(f"Is loopback: {ip_host.iptype() == 'LOOPBACK'}")
# Working with IPSet for multiple ranges
s = IPSet([IP('10.0.0.0/22')])
s.add(IP('192.168.1.0/29'))
print(f"IPSet: {s}")
print(f"Is disjoint from 172.16.0.0/12: {s.isdisjoint(IPSet([IP('172.16.0.0/12')]))}")