一文掌握使用Python的IP处理模块IPy

发布时间:2023-07-07 13:00

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方法支持的一系列操作

1,对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

2,对IP地址段转换显示形式

>>> 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

3,获取网络地址和掩码

>>> IP(\'10.0.0.0/8\').net()
IP(\'10.0.0.0\')

>>> IP(\'10.0.0.0/8\').netmask()
IP(\'255.0.0.0\')

4,反转IP地址以及带掩码的网络IP地址

>>> 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.\']

5,比较IP地址的大小,需要注意的是ipv4的地址是永远小于ipv6地址的

>>> 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

6,IP地址转换成十六进制以及具体ip是否属于地址段

>>> 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

7,用IP地址或地址段hash

>>> IP(\'10.0.0.0/24\').__hash__()
-167772185

8,IP地址或者地址段的输出print,支持丰富的显示形式

>>> 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

9,IP地址十进制显示

>>> \"%X\" % IP(\'10.10.10.10\').int()
\'A0A0A0A\'

10,自动判断IP地址类型,回环地址,私有地址,公网地址等

>>> 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

11,IP网段包含的IP地址个数

>>> print(IP(\'195.185.1.0/28\').len())
16
>>> print(IP(\'195.185.1.0/24\').len())
256

12,判断两个IP网段是否有重叠

>>> 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

13,获取掩码的长度

>>> IP(\'10.0.0.0/8\').prefixlen()
8

14,IP地址转换成二进制

>>> print(IP(\'127.0.0.1\').strBin())
01111111000000000000000000000001
>>> print(IP(\'2001:0658:022a:cafe:0200::1\').strBin())
00100000000000010000011001011000000000100010101011001010111111100000001000000000000000000000000000000000000000000000000000000001

15,获取IP地址十进制格式的字符串表示形式

>>> print(IP(\'127.0.0.1\').strDec())
2130706433
>>> print(IP(\'2001:0658:022a:cafe:0200::1\').strDec())
42540616829182469433547762482097946625

16,获取IP地址的16进制字符串表示形式

>>> print(IP(\'127.0.0.1\').strHex())
0x7f000001
>>> print(IP(\'2001:0658:022a:cafe:0200::1\').strHex())
0x20010658022acafe0200000000000001

17,获取IP地址段的掩码字符串,大部分用于IPv6

>>> print(IP(\'195.185.0.0/16\').strNetmask())
255.255.0.0
>>> print(IP(\'2001:0658:022a:cafe::0/64\').strNetmask())
/64

18,获取IP地址的字符串表示形式

>>> 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

19,获取IP地址类型,是ipv4还是ipv6

>>> IP(\'10.0.0.0/8\').version()
4
>>> IP(\'::1\').version()
6

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号