Website: https://github.com/haypo/python-ipy/
安装:
easy_install IPy
 
>>> from IPy import IP
>>> dir(IP)                     
['__add__', '__cmp__', '__contains__', '__doc__', '__eq__', '__getitem__', '__hash__', '__init__', '__len__', '__lt__', '__module__', '__nonzero__', '__repr__', '__str__', '_getIPv4Map', '_printPrefix', 'broadcast', 'int', 'iptype', 'len', 'make_net', 'net', 'netmask', 'overlaps', 'prefixlen', 'reverseName', 'reverseNames', 'strBin', 'strCompressed', 'strDec', 'strFullsize', 'strHex', 'strNetmask', 'strNormal', 'version']
>>> IP('172.29.20.80/28').len()  —— IP数量
16
>>> IP('172.29.20.80/28').net()  —— 网段   
IP('172.29.20.80')
>>> IP('172.29.20.80/28').netmask() —— 掩码
IP('255.255.255.240')
>>> IP('172.29.20.0/24').prefixlen() —— 掩码,INT型
24
>>> IP('172.29.20.0/24').strNormal(0) —— 网段
'172.29.20.0'
>>> IP('172.29.20.0/24').strNormal(1) —— 网段 + 掩码
'172.29.20.0/24'
>>> IP('172.29.20.0/24').strNormal(2) —— 网段 + 掩码
'172.29.20.0/255.255.255.0'
>>> IP('172.29.20.0/24').strNormal(3) —— 网段 + 掩码
'172.29.20.0-172.29.20.255'
>>> IP('172.29.20.0/24').strNetmask() —— 掩码
'255.255.255.0'
>>> IP('172.29.20.80/28').strNetmask() —— 掩码
'255.255.255.240'
>>> IP('172.29.20.0/24').version() —— IP v4 or v6版本号
4
>>> '127.0.0.1' in IP('127.0.0.0/24')
True
>>> IP('127.0.0.0/24') in IP('127.0.0.0/25')
False
>>> print(IP('192.168.1.1').iptype())
PRIVATE
>>> print(IP('152.168.1.1').iptype()) 
PUBLIC
>>> help(IP) —— 可以看到最详细的文档,更多的方法
 
>>> ip=IP('127.0.0.0/30')
>>> for i in ip:
...     print(i)
... 
127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
>>> for i in ip:
...     print(type(i))
... 
<type 'instance'>
<type 'instance'>
<type 'instance'>
<type 'instance'>
>>> print(ip)
127.0.0.0/30
>>> for i in ip:            
...     print(str(i))
... 
127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
>>> print(ip[2])
127.0.0.2
>>> print(str(ip[2]))
127.0.0.2
 
其他常用方法介绍:
 |  __cmp__(self, other) —— 比较大小
 |      Called by comparison operations.
 |      
 |      Should return a negative integer if self < other, zero if self
 |      == other, a positive integer if self > other.
 |      
 |      Networks with different prefixlen are considered non-equal.
 |      Networks with the same prefixlen and differing addresses are
 |      considered non equal but are compared by their base address
 |      integer value to aid sorting of IP objects.
 |      
 |      The version of Objects is not put into consideration.
 |      
 |      >>> IP('10.0.0.0/24') > IP('10.0.0.0')
 |      1
 |      >>> IP('10.0.0.0/24') < IP('10.0.0.0')
 |      0
 |      >>> IP('10.0.0.0/24') < IP('12.0.0.0/24')
 |      1
 |      >>> IP('10.0.0.0/24') > IP('12.0.0.0/24')
 |      0
 |  __contains__(self, item) —— 检查包含关系
 |      Called to implement membership test operators.
 |      
 |      Should return true if item is in self, false otherwise. Item
 |      can be other IP-objects, strings or ints.
 |      
 |      >>> IP('195.185.1.1').strHex()
 |      '0xc3b90101'
 |      >>> 0xC3B90101 in IP('195.185.1.0/24')
 |      True
 |      >>> '127.0.0.1' in IP('127.0.0.0/24')
 |      True
 |      >>> IP('127.0.0.0/24') in IP('127.0.0.0/25')
 |      False
 |  overlaps(self, item) —— 检查是否重叠
 |      Check if two IP address ranges overlap.
 |      
 |      Returns 0 if the two ranges don't overlap, 1 if the given
 |      range overlaps at the end and -1 if it does at the beginning.
 |      
 |      >>> IP('192.168.0.0/23').overlaps('192.168.1.0/24')
 |      1
 |      >>> IP('192.168.0.0/23').overlaps('192.168.1.255')
 |      1
 |      >>> IP('192.168.0.0/23').overlaps('192.168.2.0')
 |      0
 |      >>> IP('192.168.1.0/24').overlaps('192.168.0.0/23')
 |      -1
 
 

根据ip地址和子网掩码计算网段地址和广播地址(原创) Python里有一个专门处理该类问题的IP类库,来看看:

view sourceprint?01 #! /usr/bin/env python

02

03 import sys

04 from IPy import IP

05

06 address = sys.argv[1]

07 netmask = sys.argv[2]

08

09 #print address,netmask

10

11 #help(IP)

12 networkAddress = IP(address).make_net(netmask) # init a IP instance, can with netmask directly, or use make_net(netmask)

13 bcastAddress = IP(networkAddress).broadcast() # return the broadcast ip address

14

15 print networkAddress

16 print bcastAddress

 

python IPy库的更多相关文章

  1. python 常用库收集

    读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都 ...

  2. python+paramiko库+svn写的自动化部署脚本

    第一篇博文 直接开门见山的说了. 这是件什么事?:每次部署都是复制本地的文件粘贴到服务器端,因为路径复杂,所以费时且手工容易出漏洞. 一直在想有什么办法可以解决这种,因为以前在微软的一个牛人同事做过一 ...

  3. 安装Python算法库

    安装Python算法库 主要包括用NumPy和SciPy来处理数据,用Matplotlib来实现数据可视化.为了适应处理大规模数据的需求,python在此基础上开发了Scikit-Learn机器学习算 ...

  4. Python标准库14 数据库 (sqlite3)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python自带一个轻量级的关系型数据库SQLite.这一数据库使用SQL语言.S ...

  5. 常用python机器学习库总结

    开始学习Python,之后渐渐成为我学习工作中的第一辅助脚本语言,虽然开发语言是Java,但平时的很多文本数据处理任务都交给了Python.这些年来,接触和使用了很多Python工具包,特别是在文本处 ...

  6. [Python] 机器学习库资料汇总

    声明:以下内容转载自平行宇宙. Python在科学计算领域,有两个重要的扩展模块:Numpy和Scipy.其中Numpy是一个用python实现的科学计算包.包括: 一个强大的N维数组对象Array: ...

  7. python常用库

    本文由 伯乐在线 - 艾凌风 翻译,Namco 校稿.未经许可,禁止转载!英文出处:vinta.欢迎加入翻译组. Awesome Python ,这又是一个 Awesome XXX 系列的资源整理,由 ...

  8. python标准库00 学习准备

    Python标准库----走马观花 python有一套很有用的标准库.标准库会随着python解释器一起安装在你的电脑上的.它是python的一个组成部分.这些标准库是python为你准备的利器,可以 ...

  9. Python标准库:内置函数hasattr(object, name)

    Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...

随机推荐

  1. java之可变个数的形参

    //采用数组形参来定义方法 public static void test (int a, String[] books); //采用可变个数形参来定义方法 public static void te ...

  2. E-factory

    E-factory为生成XML和HTML提供了一种简单而紧凑的语法 # coding:utf-8 from lxml.builder import E def CLASS(*args): # clas ...

  3. 2018简约商务工作汇报工作总结公司培训团队介绍PPT模

    这几款ppt模板都是简约大气类型的,32页足够丰富,有完整结构框架,可以修改文字图片直接套用模板,是通用的商务ppt模板. 模版来源:http://ppt.dede58.com/gongzuohuib ...

  4. NIO基础方法一

    1.remaining();返回当前位置与limit之间得元素数. int[] intArray={1,2,3,4}; IntBuffer intBuffer=IntBuffer.wrap(intAr ...

  5. Vue中jsx的最简单用法

    最终页面显示效果为 <div class="open-service" style="color: #0199f0; cursor: pointer;"& ...

  6. windows 应急流程及实战演练

    前言 本文摘自信安之路公众号的文章. 当企业发生黑客入侵.系统崩溃或其它影响业务正常运行的安全事件时,急需第一时间进行处理,使企业的网络信息系统在最短时间内恢复正常工作,进一步查找入侵来源,还原入侵事 ...

  7. Android 上下文菜单 PopupMenu

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s ...

  8. Java反射02 : Class对象获取的三种方式和通过反射实例化对象的两种方式

    1.Class对象获取的三种方式 本文转载自:https://blog.csdn.net/hanchao5272/article/details/79361463 上一章节已经说过,一般情况下,Jav ...

  9. VirtualBox创建的Debian虚拟机与Windows宿主共享文件

    术语: 1.VM:虚拟机 步骤: 1.在Windows10上下载并安装VirtualBox6.0.8(时间:2019/5/30),下载地址:https://download.virtualbox.or ...

  10. 模拟超市付款 (if 多分支结构)

    """ 模拟超市付款: 商品单价 商品数量 键盘上输入商品单价,以及商品数量, 然后计算应付总额 计算总额 float 提示用户可以有4种付款方式 不同的付款方式有不同的 ...