IPy是Python支持IP的各种操作第三方模块,需要pip安装后才能使用,安装方法:
pip install IPy
IPy支持的方法和属性:
>>> dir(IPy)
['INT_TYPES', 'IP', 'IPSet', 'IPV6_MAP_MASK', 'IPV6_TEST_MAP', 'IPint', 'IPv4ranges', 'IPv6ranges', 'MAX_IPV4_ADDRESS', 'MAX_IPV6_ADDRESS',
'STR_TYPES', '_BitTable', '__builtins__', '__cached__', '__doc__', '__file__','__loader__', '__name__', '__package__', '__spec__',
'__version__', '_checkNetaddrWorksWithPrefixlen', '_checkNetmask', '_checkPrefix', '_count0Bits', '_count1Bits', '_countFollowingZeros', '_intToBin', '_ipVersionToLen',
'_netmaskToPrefixlen', '_parseAddressIPv6', '_prefixlenToNetmask', '_remove_subprefix', 'bisect', 'collections_abc', 'intToIp', 'parseAddress', 'types', 'xrange']
IPy的IP方法是处理IP地址和网络的常用方法,可以说使用IPy基本就是使用IP方法,所以本文主要介绍IP方法支持的一系列操作
>>> ip=IP('127.0.0.0/30')
>>> for x in ip:
... print(str(x))
...
127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
>>> print(str(ip[2]))
127.0.0.2
>>> print(str(ip[-1]))
127.0.0.3
>>> IP('10.0.0.0/8').broadcast()
IP('10.255.255.255')
>>> print(IP('127.0.0.1').make_net('255.0.0.0'))
127.0.0.0/8
>>> IP('10.0.0.0/8').net()
IP('10.0.0.0')
>>> IP('10.0.0.0/8').netmask()
IP('255.0.0.0')
>>> print(IP('195.185.1.1').reverseName())
1.1.185.195.in-addr.arpa.
>>> print(IP('195.185.1.0/28').reverseName())
0-15.1.185.195.in-addr.arpa.
>>> IP('::1:2').reverseName()
'2.0.0.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.'
>>> IP('ff02::/64').reverseName()
'0.0.0.0.0.0.0.0.0.0.0.0.2.0.f.f.ip6.arpa.'
>>> IP('213.221.113.87/32').reverseNames()
['87.113.221.213.in-addr.arpa.']
>>> IP('213.221.112.224/30').reverseNames()
['224.112.221.213.in-addr.arpa.', '225.112.221.213.in-addr.arpa.', '226.112.221.213.in-addr.arpa.', '227.112.221.213.in-addr.arpa.']
>>> IP('127.0.0.0/24').reverseNames()
['0.0.127.in-addr.arpa.']
>>> IP('127.0.0.0/23').reverseNames()
['0.0.127.in-addr.arpa.', '1.0.127.in-addr.arpa.']
>>> IP('127.0.0.0/16').reverseNames()
['0.127.in-addr.arpa.']
>>> IP('127.0.0.0/15').reverseNames()
['0.127.in-addr.arpa.', '1.127.in-addr.arpa.']
>>> IP('128.0.0.0/8').reverseNames()
['128.in-addr.arpa.']
>>> IP('128.0.0.0/7').reverseNames()
['128.in-addr.arpa.', '129.in-addr.arpa.']
>>> IP('::1:2').reverseNames()
['2.0.0.0.1.ip6.arpa.']
>>> IP('10.0.0.0') < IP('2001:db8::')
1
Then the first address is compared. Lower addresses are
always smaller:
>>> IP('10.0.0.0') > IP('10.0.0.1')
0
>>> IP('10.0.0.0/24') > IP('10.0.0.1')
0
>>> IP('10.0.1.0') > IP('10.0.0.0/24')
1
>>> IP('10.0.1.0/24') > IP('10.0.0.0/24')
1
>>> IP('10.0.1.0/24') > IP('10.0.0.0')
1
Then the prefix length is compared. Shorter prefixes are
considered smaller than longer prefixes:
>>> IP('10.0.0.0/24') > IP('10.0.0.0')
0
>>> IP('10.0.0.0/24') > IP('10.0.0.0/25')
0
>>> IP('10.0.0.0/24') > IP('10.0.0.0/23')
1
>>> IP('195.185.1.1').strHex()
'0xc3b90101'
>>> 0xC3B90101 in IP('195.185.1.0/24')
True
>>> '127.0.0.1' in IP('127.0.0.0/24')
True
>>> IP('127.0.0.0/24') in IP('127.0.0.0/25')
False
>>> IP('10.0.0.0/24').__hash__()
-167772185
>>> 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
>>> print(IP('127.0.0.1/255.0.0.0', make_net=True))
127.0.0.0/8
>>> "%X" % IP('10.10.10.10').int()
'A0A0A0A'
>>> print(IP('127.0.0.1').iptype())
LOOPBACK
>>> print(IP('192.168.1.1').iptype())
PRIVATE
>>> print(IP('195.185.1.2').iptype())
PUBLIC
>>> print(IP('::1').iptype())
LOOPBACK
>>> print(IP('2001:0658:022a:cafe:0200::1').iptype())
ALLOCATED RIPE NCC
>>> print(IP('195.185.1.0/28').len())
16
>>> print(IP('195.185.1.0/24').len())
256
>>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')
1
>>> IP('192.168.0.0/23').overlaps('192.168.1.255')
1
>>> IP('192.168.0.0/23').overlaps('192.168.2.0')
0
>>> IP('192.168.1.0/24').overlaps('192.168.0.0/23')
-1
>>> IP('10.0.0.0/8').prefixlen()
8
>>> print(IP('127.0.0.1').strBin())
01111111000000000000000000000001
>>> print(IP('2001:0658:022a:cafe:0200::1').strBin())
00100000000000010000011001011000000000100010101011001010111111100000001000000000000000000000000000000000000000000000000000000001
>>> print(IP('127.0.0.1').strDec())
2130706433
>>> print(IP('2001:0658:022a:cafe:0200::1').strDec())
42540616829182469433547762482097946625
>>> print(IP('127.0.0.1').strHex())
0x7f000001
>>> print(IP('2001:0658:022a:cafe:0200::1').strHex())
0x20010658022acafe0200000000000001
>>> print(IP('195.185.0.0/16').strNetmask())
255.255.0.0
>>> print(IP('2001:0658:022a:cafe::0/64').strNetmask())
/64
>>> print(IP('127.0.0.1').strNormal())
127.0.0.1
>>> print(IP('2001:0658:022a:cafe:0200::1').strNormal())
2001:658:22a:cafe:200:0:0:1
>>> IP('10.0.0.0/8').version()
4
>>> IP('::1').version()
6