Python中IP地址如何处理IPy模块

发布时间:2021-08-18 10:26:22 作者:小新
来源:亿速云 阅读:256

这篇文章将为大家详细讲解有关Python中IP地址如何处理IPy模块,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

安装

先下载源码,地址: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个数。

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

ipy模块用法

一个自动识别IP地址、子网、方向解析、IP类型等信息的脚本

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def ip():
  try:
    from IPy import IP  ###加载模块
    ip_s = raw_input('请输入IP地址或者网段地址:' )###输入一个IP地址或者网段
    ips = IP(ip_s)  #定义元素
    if len(ips) > 1: #如果len出来的数字大于1,那么就是一个网段
      print('网络地址: %s' % ips.net())
      print('子网掩码: %s' % ips.netmask())
      print('网络广播地址: %s' % ips.reverseNames() [0])
      print('网络子网数: %s' % len(ips))
    else:  ###否则就是一个地址
      print('IP反向解析: %s' % ips.reverseNames() [0])
      print('十六进制地址: %s' % ips.strHex())
      print('二进制地址: %s' % ips.strBin())
      print('地址类型: %s' % ips.iptype())
    print time.strftime("%Y-%m-%d %H:%M:%S")
    #code
  except Exception, e:
    logging.info("error:" + str(e) + "\n" + traceback.format_exc())
    print traceback.format_exc()
  finally:
    pass

运行效果:

[root@mylinuxer python]# 192.168.1.0/24
-bash: 192.168.1.0/24: No such file or directory
[root@mylinuxer python]# python python.py
请输入IP地址或者网段地址: 192.168.1.0/24
网络地址: 192.168.1.0
子网掩码: 255.255.255.0
网络广播地址: 1.168.192.in-addr.arpa.
网络子网数: 256
[root@mylinuxer python]# python python.py
请输入IP地址或者网段地址: 192.168.1.1
IP反向解析: 1.1.168.192.in-addr.arpa.
十六进制地址: 0xc0a80101
二进制地址: 11000000101010000000000100000001
地址类型: PRIVATE
[root@mylinuxer python]# python python.py
请输入IP地址或者网段地址: 116.213.249.211
IP反向解析: 211.249.213.116.in-addr.arpa.
十六进制地址: 0x74d5f9d3
二进制地址: 01110100110101011111100111010011
地址类型: PUBLIC

关于“Python中IP地址如何处理IPy模块”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. python日志处理模块
  2. IPy模块使用IP计算子网报错思考

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python ipy ip

上一篇:怎么给ASP.NET MVC及WebApi添加路由优先级

下一篇:mysql如何全量备份和增量备份

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》