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. 当面试官要你介绍一下MQ时,该怎么回答?

    一.为什么要使用MQ消息中间件? 一个用消息队列的人,不知道为啥用,有点尴尬.没有复习这点,很容易被问蒙,然后就开始胡扯了. 回答:这个问题,咱只答三个最主要的应用场景,不可否认还有其他的,但是只答三 ...

  2. Cypress 之 常用API

    .visit() 访问一个远程URL.>>详情参考 Cypress 之 cy.visit() cy.visit(url) cy.visit(url, options) cy.visit(o ...

  3. router-view 与 动态组件 区别

    提问:router-view 可以页面跳转,使用 is特性 也可以进行页面跳转,有什么区别? 参考链接 https://segmentfault.com/q/1010000010750059

  4. React躬行记(1)——函数式编程

    函数式编程是React的精髓,在正式讲解React之前,有必要先了解一下函数式编程,有助于更好的理解React的特点.函数式编程(Functional Programming)不是一种新的框架或工具, ...

  5. ZooKeeper(四):从TCP数据流到zk内部处理包的转换

    通过前面几篇文章,我们可以从整体上看到zk是如何处理网络数据的宏观架构. 本文我们从细节着手,看一下一个tcp的包是如何转换到内部的数据流处理的. 一.监听用户请求socket 基于NIO的端口监听, ...

  6. Add a Parametrized Action 添加带参数的按钮

    In this lesson, you will learn how to add a Parametrized Action. These types of Actions are slightly ...

  7. [转]uipath team svn

    本文转自:https://docs.uipath.com/studio/docs/svn-version-control SVN Version Control Suggest Edits Openi ...

  8. SQL Server通过定义函数返回字段数据列表模板-干货

    CREATE FUNCTION [dbo].[GetReportDWCustomerOrder] (      @YearDate DATETIME,    参数条件.....    @Categor ...

  9. Master Note: Undo 空间使用率高 (Doc ID 1578639.1)

    Master Note: High Undo Space Usage (Doc ID 1578639.1) APPLIES TO: Oracle Database Cloud Schema Servi ...

  10. android 在基类activity中注册BroadcastReceiver,子activity类实现响应

    android app 一般都会定义自己的BaseActivity, 如果各子Activity都需要接收广播但对广播的处理又不同时,可以考虑在BaseActivity中注册BroadcastRecei ...