IPy is a very useful Python library for operations and maintenance. This library provides many powerful functions for handling IP addresses.
The most important IP class resolves and handles a variety of IPv4 and IPv6 addresses
For example, parse CIDR urls
>>> from IPy import IP
>>> ip = IP('127.0.0.0/30')
>>> for x in ip:
. print(x)
...
127.0. 0. 0
127.0. 01.
127.0. 02.
127.0. 03.
Copy the code
Supports various IPv4 and IPv6 address formats
>>> IP('10.0.0.0/8').version()
4
>>> IP(': : 1').version()
6
>>> print(IP(0x7f000001))
127.0. 01.
>>> print(IP('0x7f000001'))
127.0. 01.
>>> print(IP('127.0.0.1'))
127.0. 01.
>>> print(IP('10'))
10.0. 0. 0
>>> print(IP('1080:0:0:0:8:800:200C:417A'))
1080: :8:800:200c:417a
>>> print(IP('1080::8:800:200C:417A'))
1080: :8:800:200c:417a
>>> print(IP(': : 1':)) :1
>>> print(IP(': : 13.1.68.3'))
::d01:4403
Copy the code
Supports various types of subnet masks
>>> 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
Copy the code
Converts IP addresses to strings in various formats
>>> IP('10.0.0.0/32').strNormal()
'10.0.0.0'
>>> IP('10.0.0.0/24').strNormal()
'10.0.0.0/24'
>>> IP('10.0.0.0/24').strNormal(0)
'10.0.0.0'
>>> IP('10.0.0.0/24').strNormal(1)
'10.0.0.0/24'
>>> IP('10.0.0.0/24').strNormal(2)
'10.0.0.0/255.255.255.0'
>>> IP('10.0.0.0/24').strNormal(3)
'10.0.0.0-10.0.0.255'
>>> ip = IP('10.0.0.0')
>>> print(ip)
10.0. 0. 0
>>> ip.NoPrefixForSingleIp = None
>>> print(ip)
10.0. 0. 0/32
>>> ip.WantPrefixLen = 3
>>> print(ip)
10.0. 0. 0-10.0. 0. 0
Copy the code
More information can be found at github.com/autocracy/p…