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之包装类

    针对八种基本数据类型定义相应的引用类型--包装类: 有了类的特点,接可以调用类中的方法: 基本数据类型 包装类 boolean Bollean byte Byte short Short int In ...

  2. linux下录屏和回放工具script和scriptreplay

    读书是一个长见识的过程,以前偶尔会用到录屏的工具,很少用想系统的学习一下.最近看了linux shell脚本攻略,发现很多新东西是以前自己没有接触到的.比如,这个非常好用的录屏工具:script,这次 ...

  3. 用 Python 图像识别打造一个小狗分类器

    ​ 项目介绍 小狗分类器可以做什么? 通过这个分类器,你只需要上传照片,就可以得到小狗的品种,以及更多的信息. 这就是所谓的「机器学习」,让机器自己去“学习”.我们今天要做的这个分类任务,是一个“监督 ...

  4. JQ的offset().top与JS的getBoundingClientRect区别详解,JS获取元素距离视窗顶部可变距离

     壹 ❀ 引 我在 JQ的offset().top与js的offsetTop区别详解 这篇博客中详细分析了JQ方法offset().top与JS属性offsetTop的区别,并得出了一条offset( ...

  5. ETCD:gRPC命名与发现

    原文地址:gRPC naming and discovery etcd提供一个gRPC解析器支持备用的命名系统,该命名系统从etcd获取主机以发现gRPC服务.以下机制基于监视对以服务名称为前缀的Ke ...

  6. Java题库——Chapter12 异常处理和文本IO

    异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...

  7. python Windows环境下文件路径问题

    转自:http://blog.sina.com.cn/s/blog_5ee7254801013zu7.html 在python程序里面我们经常需要对文件进行操作,Windows下的文件目录路径使用反斜 ...

  8. Postsql 修改字段长度和类型

    以后更改字段长度会重写表,如果表比较大,那么表会加锁,需要很长时间 通过一种方法通过修改pg_attribute.atttypmod字段修改长度,不需要重写表 查询select * from pg_a ...

  9. YUM命令总结

    1.关于YUM源 Yum 全称为 Yellow dog Updater Modified,它是一个在线的软件安装命令. 能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装 ...

  10. 简单理解Busybox下halt/poweroff/reboot实现及区别

    关键词:halt/poweroff/reboot.reboot().SIGUSR1/SIGTERM/SIGUSR2等. 1. busybox下的halt/poweroff/reboot实现 通过app ...