IPy - class and tools for handling of IPv4 and IPv6 addresses and networks.

Website: https://github.com/autocracy/python-ipy/

Presentation of the API
======================= The IP class allows a comfortable parsing and handling for most
notations in use for IPv4 and IPv6 addresses and networks. It was
greatly inspired by RIPE's Perl module NET::IP's interface but
doesn't share the implementation. It doesn't share non-CIDR netmasks,
so funky stuff like a netmask of 0xffffff0f can't be done here. >>> from IPy import IP
>>> ip = IP('127.0.0.0/30')
>>> for x in ip:
... print(x)
...
127.0.0.0
127.0.0.1
127.0.0.2
127.0.0.3
>>> ip2 = IP('0x7f000000/30')
>>> ip == ip2
1
>>> ip.reverseNames()
['0.0.0.127.in-addr.arpa.', '1.0.0.127.in-addr.arpa.', '2.0.0.127.in-addr.arpa.', '3.0.0.127.in-addr.arpa.']
>>> ip.reverseName()
'0-3.0.0.127.in-addr.arpa.'
>>> ip.iptype()
'PRIVATE' Supports most IP address formats
================================ It can detect about a dozen different ways of expressing IP addresses
and networks, parse them and distinguish between IPv4 and IPv6 addresses: >>> IP('10.0.0.0/8').version()
4
>>> IP('::1').version()
6 IPv4 addresses
-------------- >>> print(IP(0x7f000001))
127.0.0.1
>>> print(IP('0x7f000001'))
127.0.0.1
>>> print(IP('127.0.0.1'))
127.0.0.1
>>> print(IP('10'))
10.0.0.0 IPv6 addresses
-------------- >>> print(IP('1080:0:0:0:8:800:200C:417A'))
1080::8:800:200c:417a
>>> print(IP('1080::8:800:200C:417A'))
1080::8:800:200c:417a
>>> print(IP('::1'))
::1
>>> print(IP('::13.1.68.3'))
::d01:4403 Network mask and prefixes
------------------------- >>> print(IP('127.0.0.0/8'))
127.0.0.0/8
>>> print(IP('127.0.0.0/255.0.0.0'))
127.0.0.0/8
>>> print(IP('127.0.0.0-127.255.255.255'))
127.0.0.0/8 Derive network address
=========================== IPy can transform an IP address into a network address by applying the given
netmask:
>>> print(IP('127.0.0.1/255.0.0.0', make_net=True))
127.0.0.0/8 This can also be done for existing IP instances:
>>> print(IP('127.0.0.1').make_net('255.0.0.0'))
127.0.0.0/8 Convert address to string
========================= Nearly all class methods which return a string have an optional
parameter 'wantprefixlen' which controls if the prefixlen or netmask
is printed. Per default the prefilen is always shown if the network
contains more than one address:: wantprefixlen == 0 / None don't return anything 1.2.3.0
wantprefixlen == 1 /prefix 1.2.3.0/24
wantprefixlen == 2 /netmask 1.2.3.0/255.255.255.0
wantprefixlen == 3 -lastip 1.2.3.0-1.2.3.255 You can also change the defaults on an per-object basis by fiddling with
the class members: * NoPrefixForSingleIp
* WantPrefixLen Examples of string conversions: >>> IP('10.0.0.0/32').strNormal()
'10.0.0.0'
>>> IP('10.0.0.0/24').strNormal()
'10.0.0.0/24'
>>> IP('10.0.0.0/24').strNormal(0)
'10.0.0.0'
>>> IP('10.0.0.0/24').strNormal(1)
'10.0.0.0/24'
>>> IP('10.0.0.0/24').strNormal(2)
'10.0.0.0/255.255.255.0'
>>> IP('10.0.0.0/24').strNormal(3)
'10.0.0.0-10.0.0.255'
>>> ip = IP('10.0.0.0')
>>> print(ip)
10.0.0.0
>>> ip.NoPrefixForSingleIp = None
>>> print(ip)
10.0.0.0/32
>>> ip.WantPrefixLen = 3
>>> print(ip)
10.0.0.0-10.0.0.0 Work with multiple networks
=========================== Simple addition of neighboring netblocks that can be aggregated will yield
a parent network of both, but more complex range mapping and aggregation
requires is available with the IPSet class which will hold any number of
unique address ranges and will aggregate overlapping ranges. >>> from IPy import IP, IPSet
>>> IP('10.0.0.0/22') - IP('10.0.2.0/24')
IPSet([IP('10.0.0.0/23'), IP('10.0.3.0/24')])
>>> IPSet([IP('10.0.0.0/23'), IP('10.0.3.0/24'), IP('10.0.2.0/24')])
IPSet([IP('10.0.0.0/22')])
>>> s = IPSet([IP('10.0.0.0/22')])
>>> s.add(IP('192.168.1.0/29'))
>>> s
IPSet([IP('10.0.0.0/22'), IP('192.168.1.0/29')])
>>> s.discard(IP('192.168.1.2'))
>>> s
IPSet([IP('10.0.0.0/22'), IP('192.168.1.0/31'), IP('192.168.1.3'), IP('192.168.1.4/30')]) IPSet supports the `set` method `isdisjoint`: >>> s.isdisjoint(IPSet([IP('192.168.0.0/16')]))
False
>>> s.isdisjoint(IPSet([IP('172.16.0.0/12')]))
True IPSet supports intersection: >>> s & IPSet([IP('10.0.0.0/8')])
IPSet([IP('10.0.0.0/22')]) Compatibility and links
======================= IPy 0.83 works on Python version 2.6 - 3.4. The IP module should work in Python 2.5 as long as the subtraction operation
is not used. IPSet requires features of the collecitons class which appear
in Python 2.6, though they can be backported. Eratta
====== When using IPv6 addresses, it is best to compare using IP().len() instead of
len(IP). Addresses with an integer value > 64 bits can break the 2nd method.
See http://stackoverflow.com/questions/15650878 for more info. Fuzz testing for IPSet will throw spurious errors when the IPSet module
combines two smaller prefixes into a larger prefix that matches the random
prefix tested against. This Python module is under BSD license: see COPYING file. Further Information might be available at:
https://github.com/autocracy/python-ipy

IPy的使用的更多相关文章

  1. Python之实用的IP地址处理模块IPy

    实用的IP地址处理模块IPy 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等 别担心,Ipy模块拯救你.Ipy模块可以很好的辅助我们高效的完成IP的规划工 ...

  2. 【python】IP地址处理模块IPy

    来源:https://pypi.python.org/pypi/IPy IPy模块 该模块可以方便的处理IPv4和IPv6地址. 以下是从来源中拷贝的一些例子: >>> from I ...

  3. IPy

    IPy生成网段列表from IPy import IPip = IP('192.168.0.0/16')print ip.len()for x in ip:print (x) ip的属性,'PUBLI ...

  4. 【Python】 http客户端库requests & urllib2 以及ip地址处理IPy

    requests requests是个HTTPClient库,相比于urllib,urllib2等模块比更加简洁易用 ■ get请求 作为示例,讲一下关于requests如何发起并处理一个get请求 ...

  5. python自动化运维笔记2 —— IP地址处理模块IPy

    1.2 实用的IP地址处理模块IPy ip地址规划是网络设计中非常重要的一个环节,规划的好坏会直接影响路由协议算法的效率,包括网络性能.可扩展性等方面,在这个过程当中,免不了要计算大量的IP地址,包括 ...

  6. Python IPy模块

    #!/usr/bin/env python # -*- coding: utf-8 -* # Created by YangYongming at 2018/09/17 20:22 # FileNam ...

  7. 实用的IP地址处理模块IPy

    https://www.cnblogs.com/cherishry/p/5916935.html IPy安装 pip install IPy IP地址.网段的基本处理 IPy模块包含IP类,使用它可以 ...

  8. Python模块学习 - IPy

    简介 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6地址的类与工 ...

  9. 2.python IP/DNS地址处理之IPy/Dnspython模块

     1.IPy模块 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6 ...

随机推荐

  1. Android开发--解决AndroidADT开发工具不能代码提示的问题

    google android的新的开发工具,打开以后没有代码自动提示功能,下面对ADT工具的一些配置: 1.设置代码的字体 设置JAVA文件代码的字体:我这里设置的14 常规.

  2. lintcode 中等题:N Queens N皇后问题

    题目: N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击.<不同行,不同列,不同对角线> 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案 ...

  3. MySQL错误代码大全【转载】

    B.1. 服务器错误代码和消息 服务器错误信息来自下述源文件: 错误消息信息列在share/errmsg.txt文件中."%d"和"%s"分别代表编号和字符串, ...

  4. org.apache.common.io-FileUtils详解

    org.apache.common.io---FileUtils详解 getTempDirectoryPath():返回临时目录路径; public static String getTempDire ...

  5. iOS开发--网络下载

    这里使用的是NSURLConnection的代理请求下载,并且是具有进度,UI能实时刷新,至于NSURLConnection如何请求.并且有几种请求方法请看NSURLConnection请求简介,在这 ...

  6. VA对于开发QT是神器

    我怎么就忘了,VA也可以适用于VS下开发QT程序.其中QT的头文件自己增加,主要是: C:\Qt\4.8.6_2008\include 但还有一些特殊类不认识,所以还得继续增加: C:\Qt\4.8. ...

  7. Shell最多支持多少个参数

    本文转自:http://www.jb51.net/article/56548.htm   这篇文章主要介绍了Shell最多支持多少个参数?本文是对Shell最多可以输入多少个参数的一篇测试文章,需要的 ...

  8. Spring面向切面编程(AOP,Aspect Oriented Programming)

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  9. eclipse运行emulator时,PANIC:Could not open emulator的解决办法

    使用eclipse启动emulator的时候,出现PANIC:Could not open emulator,模拟器无法正常的运行. 经过搜索得知,因为我的SDK的环境变量出问题,需要重新配置下环境变 ...

  10. Android log日志

    LOG是用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录.在Android系统中,提供了简单.便利的LOG机制,开发人员可以方便地使用. androidsdk中提供了l ...