1. 程式人生 > >IP地址處理模組IPy

IP地址處理模組IPy

一、IPy介紹

IPy-用於處理IPv4和IPv6地址和網路的類和工具。

API介紹:

IP類允許一個舒適的解析並處理大多數
用於IPv4和IPv6地址和網路的符號。
受到RIPE的Perl模組NET :: IP介面的極大啟發,但
不共享實現。它不共享非CIDR網路掩碼,
所以像這樣的時髦網路掩碼0xffffff0f無法在這裡完成。

The IP class allows a comfortable parsing and handling for most
notations in use for IPv4 and IPv6 addresses and networks. It was
greatly inspired by RIPE
's Perl module NET::IP's interface but doesn't share the implementation. It doesn't share non-CIDR netmasks, so funky stuff like a netmask of 0xffffff0f can't be done here.

二、IPy模組安裝

Centos 

[[email protected] ~]# pip3 install IPy
Collecting IPy
  Downloading https://files.pythonhosted.org/packages/88/28/79162bfc351a3f1ab44d663ab3f03fb495806fdb592170990a1568ffbf63/IPy-0.83.tar.gz
Installing collected packages: IPy Running setup.py install for IPy ... done Successfully installed IPy-0.83

Mac OS

MacBook-Pro:~ h$ pip3 install IPy
Collecting IPy
  Downloading https://files.pythonhosted.org/packages/88/28/79162bfc351a3f1ab44d663ab3f03fb495806fdb592170990a1568ffbf63/IPy-0.83.tar.gz
Installing collected packages: IPy Running setup.py install for IPy ... done Successfully installed IPy-0.83 MacBook-Pro:~ h$ python3 Python 3.6.3 (v3.6.3:2c5fed86e0, Oct 3 2017, 00:32:08) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import IPy >>> exit()

三、IP地址、網段的基本處理

IPy模組包含IP類,使用它可以方便處理絕大部分格式為IPv6及IPv4的網路和地址。比如通過version方法可以區分IPv4與IPv6,如:

>>> IP('127.0.0.0/30').version()
4
>>> IP('::1').version()
6

通過指定的網段輸出該網段的IP個數及所有IP地址清單,程式碼如下:

>>> from IPy import IP
>>> ip=IP('192.168.1.0/24')
>>> ip.len()        #輸出192.168.1.0/24網段的IP個數
256
>>> for x in ip:    #輸出192.168.1.0/24網段的所有IP清單
...     print(x)
...
192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
.........

下面介紹IP類的幾個常見的方法,包括反向解析名稱、IP型別、IP轉換等。

>>> from IPy import IP
>>> ip = IP('192.168.121.1')
>>> ip.reverseNames()                  #反向解析地址格式
['1.121.168.192.in-addr.arpa.']
>>> ip.iptype()        #192.168.121.1為私網型別'PRIVATE'
'PRIVATE'
>>> IP('8.8.8.8').iptype()        #8.8.8.8為公網型別
'PUBLIC'
>>> IP('8.8.8.8').int()              #轉換成整型格式
134744072
>>> IP('8.8.8.8').strHex()           #轉換成十六進位制格式
'0x8080808'
>>> IP('8.8.8.8').strBin()            #轉換成二進位制格式
'00001000000010000000100000001000'
>>> print(IP(0x8080808))            #十六進位制轉成IP格式
8.8.8.8

 IP方法也支援網路地址的轉換,例如根據IP與掩碼生成網段格式,示例:

>>> from IPy import IP
>>> IP('192.168.1.0').make_net('255.255.255.0')
IP('192.168.1.0/24')
>>> IP('192.168.1.0/255.255.255.0',make_net=True)
IP('192.168.1.0/24')
>>> IP('192.168.1.0-192.168.1.255',make_net=True)
IP('192.168.1.0/24')

也可以通過strNormal方法指定不同wantprefixlen引數值以定製不同輸出型別的網段。輸出型別為字串,如下:

wantprefixlen的取值及含義:

wantprefixlen=0,五返回,如192.168.1.0;

wantprefixlen=1,prefix格式,如192.168.1.0/24;

wantprefixlen=2,decimalnetmask格式,如192.168.1.0/255.255.255.0;

wantprefixlen=3,lastIP格式,如192.168.1.0-192.168.1.255。

>>> from IPy import IP
>>> IP('192.168.1.0/24').strNormal(0)
'192.168.1.0'
>>> IP('192.168.1.0/24').strNormal(1)
'192.168.1.0/24'
>>> IP('192.168.1.0/24').strNormal(2)
'192.168.1.0/255.255.255.0'
>>> IP('192.168.1.0/24').strNormal(3)
'192.168.1.0-192.168.1.255'