• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python中IP地址处理IPy模块的方法

python 搞代码 4年前 (2022-01-09) 26次浏览 已收录 0个评论

安装

先下载源码,地址:ps://pypi.python.org/pypi/IPy/”>https://pypi.python.org/pypi/IPy/ ,然后解压后使用命令python setup.py install安装。

使用

1、显示IP类型

>>> IP('192.168.1.1').version()
 4
 >>> IP('::1').version()
 6

类似如上所示,通过version方法可以的判断输入的IP是IPv4还是IPv6 。

2、网段计算输出

代码:

from IPy import IP
ip=IP('192.168.0.0/28')
print ip.len()
for x in ip:
  print x
print ip.strNormal(0)
print ip.strNormal(1)
print ip.strNormal(2)
print ip.strNormal(3)

len()方法可以计算网段的IP个数。

本文来源gao($daima.com搞@代@#码$网

strNormal()方法指定不同wantprefixlen参数可以定制不同类型的输出。上面输出类似如下:

16
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
......
192.168.0.15
192.168.0.0
192.168.0.0/28
192.168.0.0/255.255.255.240
192.168.0.0-192.168.0.15

3、格式转换

实例介绍几个常用方法,包括方向解析名称、IP类型、IP进制转换、网络地址网段地址转换。

ip=IP('192.168.0.1')
print ip.reverseNames() #反向解析地址格式
print ip.iptype() #显示IP地址类型,私有还是公有
ip=IP('8.8.8.8')
print ip.iptype()
print ip.int() #转换成整型格式
print ip.strHex() #转换成十六进制格式
print ip.strBin() #转换成二进制格式
#网络地址、网段地址格式转换
print (IP('192.168.1.0').make_net('255.255.255.0'))
print (IP('192.168.1.0/255.255.255.0',make_net=True))
print (IP('192.168.1.0-192.168.1.255',make_net=True))

4、地址比较

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

>>> '192.168.1.1' in IP('192.168.1.0/24')
True
>>> IP('192.168.1.0/24') in IP('192.168.0.0/16')
True

判断两个网段是否存在重叠,如下:

>>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')
1
>>> IP('192.168.1.0/24').overlaps('192.168.2.0')
0

其中1表示存在重叠,0表示不存在重叠。

举例

代码:

#coding:utf-8
from IPy import IP
ip_s=raw_input("please input an IP or net-range: ")
ips=IP(ip_s)
if len(ips)>1: #网络地址
  print('net: %s' % ips.net())
  print('netmask: %s' % ips.netmask())
  print('broadcast: %s' % ips.broadcast())
  print('reverse address: %s' % ips.reverseNames()[0])
  print('subnet: %s' % len(ips))
else: #单个地址
  print('reverse address: %s' % ips.reverseNames()[0])
print('hexadecimal: %s' % ips.strHex())
print('binary: %s' % ips.strBin())
print('iptype: %s' % ips.iptype())

运行结果:

C:\Users\admin\workspace\zhangnq>python IPy_test2.py
please input an IP or net-range: 192.168.1.1
reverse address: 1.1.168.192.in-addr.arpa.
hexadecimal: 0xc0a80101
binary: 11000000101010000000000100000001
iptype: PRIVATE
C:\Users\admin\workspace\zhangnq>python IPy_test2.py
please input an IP or net-range: 8.8.8.8
reverse address: 8.8.8.8.in-addr.arpa.
hexadecimal: 0x8080808
binary: 00001000000010000000100000001000
iptype: PUBLIC
C:\Users\admin\workspace\zhangnq>python IPy_test2.py
please input an IP or net-range: 192.168.1.0/28
net: 192.168.1.0
netmask: 255.255.255.240
broadcast: 192.168.1.15
reverse address: 0.1.168.192.in-addr.arpa.
subnet: 16
hexadecimal: 0xc0a80100
binary: 11000000101010000000000100000000
iptype: PRIVATE

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python中IP地址处理IPy模块的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址