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. 模糊测试(fuzz testing)介绍(一)
  2. gazebo卡了_ardupilot gazebo打开卡死解决办法
  3. 如何查看linux系统的体系结构
  4. Hadoop+GPU强强联手的性能探索
  5. 单片机测量代码运行时间方法-STM32
  6. 2021计算机科学调剂,2021北京科技大学计算机科学与技术专业接收调剂研究生的通知...
  7. 如何把html转为excel,怎么把网页转化为excel
  8. K210车牌归属地识别[获取图像+训练+识别效果演示]
  9. 开放世界游戏中的大地图的实现——程序技术篇
  10. js制作动态图片时钟
  11. js中的深拷贝和浅拷贝
  12. Duplicated tag: ‘dependencies‘报错
  13. 从春招到秋招,算法工程师养成记
  14. 燃起来 ESP32移植LVGL最新版本8.2
  15. 第三集 Spring for Apache Kafka 接受消息
  16. 计算机体系结构在线读,计算机体系结构_arch01_intro-c1.pdf
  17. The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with
  18. 使用现代C++如何避免bugs(上)
  19. 三十天学会绘画pdf_三十天学会实用画法
  20. python 绘图类_关于创建自定义绘图对象-python中的类,有很好的教程吗? - python...

热门文章

  1. [转]《RabbitMQ官方指南》安装指南
  2. Windows Phone 7 学习笔记1:Hello, Windows Phone 7
  3. linux下防火墙iptables原理及使用
  4. mybatis学习笔记(3)-入门程序一
  5. Windows 2003 安装WLM2009/MSN9错误的另一种解决办法
  6. 虚拟机里安装Linux系统出现乱码
  7. Maven让资源文件处理插件能够解析资源文件中的Maven属性
  8. 【转】提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果
  9. 5中排序算法(冒泡,选择,插入,快速,归并)
  10. Java 6.编写类