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. SpringBoot入门(简单详细教程)

    Spring Boot 简介 简化Spring应用开发的一个框架:整个Spring技术栈的一个大整合:J2EE开发的一站式解决方案: 微服务 martin fowler:微服务:架构风格(服务微化): ...

  2. SpringBoot控制台版图书借阅程序

    // 实验存档... 效果图: 完整程序:https://pan.baidu.com/s/1-d1J90dkEtM0WKkABu0K0Q 提取码:hcnm DAO层代码由MyBatis Generat ...

  3. Python 从入门到进阶之路(五)

    之前的文章我们简单介绍了一下 Python 的函数,本篇文章我们来看一下 Python 中的面向对象. Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是 ...

  4. 一起学Android之Animation

    本文以一个简单的小例子,简述在Android开发中,动画的简单应用,仅供学习分享使用. 概述 android提供了各种强大的apis,用于将动画应用到ui元素中,来丰富应用程序的功能和应用. 动画分类 ...

  5. Web前端基础(11):JavaScript(五)

    1. 初识DOM 1.1 DOM介绍 1.1.3 什么是DOM DOM:文档对象模型.DOM 为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构.目的其实就是为了能让js操作html元素而制定 ...

  6. opencv-python图像处理基础(一)

    #一.读取图像数据 import cv2 img=cv2.imread("d:/image0.JPG") #读取图片数据 print(img) cv2.imshow('image' ...

  7. xBIM 综合使用案例与 ASP.NET MVC 集成(一)

    XbimWebUI是一个Javascript库,可用于BIM模型的Web表示.它使用WebGL并且独立于任何第三方WebGL框架.查看器的数据格式为WexBIM.不能直接加载IFC文件. 一.将IFC ...

  8. Notification 弹出一个通知在桌面右下角

    if (!("Notification" in window)) { //alert("This browser does not support desktop not ...

  9. gcc栈溢出保护机制:stack-protector

    关键词:stack-protector.stack-protector-strong.stack-protector-all等等. 1. gcc栈保护机制stack-protector简介 gcc提供 ...

  10. jira问题更改项目

    1.点击“问题”在问题中,筛选你要修改的问题,以下是以创建用户为搜索条件,然后点击“工具”---移动所有问题 2.选择要移动的问题,选择项目,进行移动