参考:http://pydoc.net/netaddr/0.7.10/netaddr.strategy/
from netaddr.strategy.ipv4 import int_to_words, bin_to_int
1. IPAddress
In [79]: ip = IPAddress('127.0.0.1')
In [80]: ip
Out[80]: IPAddress('127.0.0.1')
In [81]: ip.bin
Out[81]: '0b1111111000000000000000000000001'
In [82]: ip.value
Out[82]: 2130706433
In [83]: int(ip)
Out[83]: 2130706433
In [84]: ip.info
Out[84]:
{'IPv4': [{'date': '1981-09',
'designation': 'IANA - Loopback',
'prefix': '127/8',
'status': 'Reserved',
'whois': ''}]}
In [85]: ip..format
File "<ipython-input-85-14e9e84bea11>", line 1
ip..format
^
SyntaxError: invalid syntax
In [86]: ip.format
Out[86]: <bound method IPAddress.format of IPAddress('127.0.0.1')>
In [87]: ip.format()
Out[87]: '127.0.0.1'
In [88]: ip.ipv4
Out[88]: <bound method IPAddress.ipv4 of IPAddress('127.0.0.1')>
In [89]: ip.ipv4()
Out[89]: IPAddress('127.0.0.1')
In [90]: ip.ipv6()
Out[90]: IPAddress('::ffff:127.0.0.1')
In [91]: ip.is_private()
Out[91]: False
2. IPNetwork
In [42]: from netaddr import *
In [43]: ip = IPNetwork('192.168.0.1/29')
In [44]: ip_list = IPNetwork('192.168.0.1/29')
In [45]: len(ip_list)
Out[45]: 8
In [46]: ip_list
Out[46]: IPNetwork('192.168.0.1/29')
In [47]: list(ip_list)
Out[47]:
[IPAddress('192.168.0.0'),
IPAddress('192.168.0.1'),
IPAddress('192.168.0.2'),
IPAddress('192.168.0.3'),
IPAddress('192.168.0.4'),
IPAddress('192.168.0.5'),
IPAddress('192.168.0.6'),
IPAddress('192.168.0.7')]
In [96]: ipn = IPNetwork('127.0.0.1/24')
In [97]: ipn
Out[97]: IPNetwork('127.0.0.1/24')
In [98]: ipn.cidr
Out[98]: IPNetwork('127.0.0.0/24')
In [99]: ipn.first
Out[99]: 2130706432
In [100]: ipn.last
Out[100]: 2130706687
In [101]: ipn.is_private
Out[101]: <bound method BaseIP.is_private of IPNetwork('127.0.0.1/24')>
In [102]: ipn.is_private()
Out[102]: False
In [103]: ipn.next
Out[103]: <bound method IPNetwork.next of IPNetwork('127.0.0.1/24')>
In [104]: ipn.next()
Out[104]: IPNetwork('127.0.1.0/24')
In [105]: ipn.previous()
Out[105]: IPNetwork('126.255.255.0/24')
In [106]: ipn.is_loopback()
Out[106]: True
In [107]: ipn.ipv4
Out[107]: <bound method IPNetwork.ipv4 of IPNetwork('127.0.0.1/24')>
In [108]: ipn.ipv4()
Out[108]: IPNetwork('127.0.0.1/24')
In [109]: ipn.ipv6()
Out[109]: IPNetwork('::ffff:127.0.0.1/120')
In [110]: ipn.size()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-110-21878477cf57> in <module>
----> 1 ipn.size()
TypeError: 'int' object is not callable
In [111]: ipn.size
Out[111]: 256
In [112]: ipn.sort_key
Out[112]: <bound method IPNetwork.sort_key of IPNetwork('127.0.0.1/24')>
In [113]: ipn.sort_key()
Out[113]: (4, 2130706432, 23, 1)
In [114]: ipn.value
Out[114]: 2130706433
In [115]: ipn.version
Out[115]: 4
In [116]: ipn.subnet
Out[116]: <bound method IPNetwork.subnet of IPNetwork('127.0.0.1/24')>
In [118]: ipn.network
Out[118]: IPAddress('127.0.0.0')
In [119]: ipn.broadcast
Out[119]: IPAddress('127.0.0.255')
In [120]: ipn.ip
Out[120]: IPAddress('127.0.0.1')
In [122]: ipn.hostmask
Out[122]: IPAddress('0.0.0.255')
In [124]: ipn.prefixlen
Out[124]: 24
In [125]: ipn.previous
Out[125]: <bound method IPNetwork.previous of IPNetwork('127.0.0.1/24')>
In [126]: ipn
Out[126]: IPNetwork('127.0.0.1/24')
In [127]: ipn.next
Out[127]: <bound method IPNetwork.next of IPNetwork('127.0.0.1/24')>
3. cidrs
In [49]: import netaddr
In [50]: cidrs = netaddr.iprange_to_cidrs('192.168.0.1', '192.168.0.9')
In [51]: cidrs
Out[51]:
[IPNetwork('192.168.0.1/32'),
IPNetwork('192.168.0.2/31'),
IPNetwork('192.168.0.4/30'),
IPNetwork('192.168.0.8/31')]
In [52]: cidrs[0]
Out[52]: IPNetwork('192.168.0.1/32')
4. 相关计算
In [197]: ip_test = '192.168.0.1'
In [199]: IPAddress(ip_test)
Out[199]: IPAddress('192.168.0.1')
In [200]: ipa = IPAddress(ip_test)
In [201]: ipa
Out[201]: IPAddress('192.168.0.1')
In [203]: ipa.value
Out[203]: 3232235521
In [204]: int_to_words(ipa.value)
Out[204]: (192, 168, 0, 1)
In [205]: IPNetwork(ipa)
Out[205]: IPNetwork('192.168.0.1/32')
In [206]: ipn = IPNetwork(ipa)
In [207]: ipn.last
Out[207]: 3232235521
In [208]: ipn.first
Out[208]: 3232235521
In [209]: int_to_words(ipn.first)
Out[209]: (192, 168, 0, 1)
In [215]: ipn[-1]
Out[215]: IPAddress('192.168.0.1')
In [216]: ipn[0]
Out[216]: IPAddress('192.168.0.1')
In [217]: ipn[0].bin
Out[217]: '0b11000000101010000000000000000001'
In [218]: ipn[0].value
Out[218]: 3232235521