Python学习笔记-IP地址处理模块Ipy

来源:互联网 发布:法语网络教学视频 编辑:程序博客网 时间:2024/06/05 05:03

IP地址处理模块Ipy

    IPy模块可以很好的辅助我们高效完成IP的规划工作。

    参考:https://github.com/autocracy/python-ipy

        安装IPy模块

1
[root@kurol ~]python36 -m easy_install -i http://pypi.douban.com/simple/ IPy  

        导入模块

1
In [12]from IPy import IP  

    1、IP地址、网段的基本处理

        通过version区分IPv4和IPv6:

1
2
3
4
5
In [13]testip = ('10.0.0.0/8','::1')
In [14]IP(testip[0]).version()
Out[14]4
In [15]IP(testip[1]).version()
Out[15]6  

        通过指定的网段输出该网段的IP个数及所有IP地址清单:

1
2
3
4
5
6
7
8
9
10
In [25]testip1 = IP('192.168.1.0/24')
In [26]print (testip1.len())
256
In [27]for x in testip1:
 ...: print (x)
 ...: 
192.168.1.0
192.168.1.1
192.168.1.2
......

        IP类常用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
In [32]testip2 = IP('192.168.100.10')
In [33]testip2.reverseNames() #
Out[33]['10.100.168.192.in-addr.arpa.']
In [34]testip2.iptype()   #IP192.168.100.10
Out[34]'PRIVATE'
In [35]IP('8.8.8.8').iptype()   #IP8.8.8.8
Out[35]'PUBLIC'
In [36]testip2.int()  #
Out[36]3232261130
In [37]testip2.strHex()   #
Out[37]'0xc0a8640a'
In [38]testip2.strBin()   #
Out[38]'11000000101010000110010000001010'
In [39]print (IP(0xc0a8640a))   #IP
192.168.100.10  

        网络地址转换,根据IP与掩码生成网段格式:

1
2
3
4
5
6
In [47]print (IP('192.168.1.0').make_net('255.255.255.0'))
192.168.1.0/24
In [48]print (IP('192.168.1.0/255.255.255.0',make_net=True))
192.168.1.0/24
In [49]print (IP('192.168.1.0-192.168.1.255',make_net=True))
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
In [52]IP('192.168.1.0/24').strNormal(wantprefixlen=0)
Out[52]'192.168.1.0'
In [53]IP('192.168.1.0/24').strNormal(wantprefixlen=1)
Out[53]'192.168.1.0/24'
#wantprefixlen
In [54]IP('192.168.1.0/24').strNormal(0)
Out[54]'192.168.1.0'
In [55]IP('192.168.1.0/24').strNormal(1)
Out[55]'192.168.1.0/24'
In [56]IP('192.168.1.0/24').strNormal(2)
Out[56]'192.168.1.0/255.255.255.0'
In [57]IP('192.168.1.0/24').strNormal(3)
Out[57]'192.168.1.0-192.168.1.255'  

 

 

2、多网络计算方法

    IPy支持类似于数值型数据的比较,以帮助IP对象进行比较

1
2
3
4
In [67]IP('10.0.0.0/24') > IP('12.0.0.0/24')
Out[67]False
In [68]IP('10.0.0.0/24') < IP('12.0.0.0/24')
Out[68]True  

 

        判断IP地址和网段是否包含于另一个网段中:

1
2
3
4
5
6
In [69]'192.168.1.100' in IP('192.168.1.0/24')
Out[69]True
In [70]'192.168.2.100' in IP('192.168.1.0/24')
Out[70]False
In [71]'192.168.1.0/24' in IP('192.168.0.0/16')
Out[71]True  

 

        判断两个网段是否存在重叠,采用IPy提供的overlaps方法:


1
2
3
4
In [74]IP('192.168.0.0/23').overlaps('192.168.1.0/24')
Out[74]1  #1
In [75]IP('192.168.1.0/24').overlaps('192.168.2.0')
Out[75]0  #0

 

实例:根据输入的IP或子网返回网络、掩码、广播、反向解析、子网数、IP类型等信息:

1
[root@kurol pkg1]vim testip.py   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
#coding:utf-8
#
from IPy import IP
import sys 
reload(sys)
sys.setdefaultencoding('utf-8')
ip_test = raw_input('IP')
ips = IP(ip_test)
if len(ips) > 1:
 print ('%s' % ips.net())
 print ('%s' % ips.netmask())
 print ('广%s' % ips.broadcast())
 print ('%s' % ips.reverseNames())
 print ('%s' % len(ips))
else:
 print ('IP %s' % ips.reverseNames())
print ('IP %s' % ips.strHex())
print ('IP %s' % ips.strBin())
print ('IP %s' % ips.iptype())  

 

 执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@kurol pkg1]python testip.py 
IP192.168.1.0/24
192.168.1.0
255.255.255.0
广192.168.1.255
['1.168.192.in-addr.arpa.']
256
IP 0xc0a80100
IP 11000000101010000000000100000000
IP PRIVATE
[root@kurol pkg1]python testip.py 
IP192.168.1.1
IP ['1.1.168.192.in-addr.arpa.']
IP 0xc0a80101
IP 11000000101010000000000100000001
IP PRIVATE  

 

Open source is not only a kind of behavior, it is a faith.  欢迎交流,QQ:526653382