python——IPy库 (2011-03-09 15:29)
分类: python
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

转载于:https://www.cnblogs.com/babykick/archive/2011/11/08/2241412.html

转:python——IPy库相关推荐

  1. python中IPy库用法详解

    使用python遍历所有的ip,可以借助第三方库IPy来获取所有的ip. 安装IPy库: pip install IPy 1.遍历IP网段中所有IP并打印个数 示例代码1: import IPydef ...

  2. [转载] python常用库

    参考链接: Python–新一代语言 转载至:https://www.cnblogs.com/jiangchunsheng/p/9275881.html 今天我将介绍20个属于我常用工具的Python ...

  3. python 常用库收集

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

  4. 分享一篇python常用库

    20个必不可少的Python库也是基本的第三方库 读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz ...

  5. Windows离线安装IPy库

    引言:一般情况下,直接在CMD命令窗口使用命令"pip install IPy"即可安装.本文介绍离线的方法安装IPy库. 关于IPy:IPy是python的一个库,方便使用pyt ...

  6. Python 标准库之 xml.etree.ElementTree xml解析

    Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...

  7. 4行指令解决pip下载Python第三方库太慢问题(pip更换国内下载源)

     问题由来: 之前在写一篇项目博客时,pip下载Python第三方库:graphic-verification-code,实在太慢了,于是使用Python库官网下载,还是很慢,而且不断失败,下载慢且不 ...

  8. Python培训教程分享:“高效实用” 的Python工具库

    作为一名合格Python技术员,对于Python工具库的使用是少不了的,本期Python培训教程就为大家分享的是""高效实用" 的Python工具库",希望能够 ...

  9. Python标准库介绍

    1. 关于Python标准库 众所周知,Python是一个依赖强大的组件库完成对应功能的语言,为了便捷实现各项功能,前辈大牛们打造了多种多样的工具库公开提供给大众使用,而越来越多的库已经因为使用的广泛 ...

最新文章

  1. 2018.3.15校内互测总结-点分治-线段树
  2. 使用正则表达式抽取新闻/BBS网页发表时间
  3. IDEA中MAVEN项目打JAR包的简单方法
  4. 在国企当程序员是什么体验?
  5. Linux(8) —— grep命令
  6. vuex的命名空间有哪些_Vue 3 带来的 Vuex 的替代方案
  7. python输出文本内容_python 打印文件里的内容
  8. 浅谈测试rhel7新功能时的感受及遇到的问题
  9. java异常处借接错书_利用Java异常机制实现模拟借书系统
  10. Navicat在输入da..时自动关闭解决方法(手心输入法)
  11. 怎样删除手机自带软件?
  12. Linux进程管理(redhat 8.0)
  13. 基于arduino的oled显示屏的使用
  14. 阿里云自建k8s存储插件csi安装使用
  15. 详解EventBus实现原理
  16. 银行排队信息预测系统数学建模
  17. Android客户端与PHP服务端API接口Token安全验证
  18. 00后女记者的一场直播挑战,触动了多少城市年轻打工人的心
  19. OpenCV3_C++_Erode()图像的收缩 实例
  20. 百度地图绘制行政区边界

热门文章

  1. SAD立体匹配算法在opencv中的实现
  2. Ansible之八:Playbook循环
  3. 从windows上传文件到linux,中文名乱码解决方法
  4. iOS APP提交上架最新流程(转)
  5. [Linux] 使用noatime属性优化文件系统读取性能
  6. Hyper-V 内存管理必须知道的
  7. EZ430 Chronos 如何提高开发调试效率探讨
  8. SQL Server Integration Services 包的开发与部署初探
  9. 不当免费技术支持的10种方法
  10. Solaris的syslog机制