IP地址處理模塊IPy


一、IPy介紹

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

網址: https://github.com/autocracy/python-ipy/

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 

[root@localhost ~]# 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'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM