IPy的使用
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的使用的更多相关文章
- Python之实用的IP地址处理模块IPy
实用的IP地址处理模块IPy 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等 别担心,Ipy模块拯救你.Ipy模块可以很好的辅助我们高效的完成IP的规划工 ...
- 【python】IP地址处理模块IPy
来源:https://pypi.python.org/pypi/IPy IPy模块 该模块可以方便的处理IPv4和IPv6地址. 以下是从来源中拷贝的一些例子: >>> from I ...
- IPy
IPy生成网段列表from IPy import IPip = IP('192.168.0.0/16')print ip.len()for x in ip:print (x) ip的属性,'PUBLI ...
- 【Python】 http客户端库requests & urllib2 以及ip地址处理IPy
requests requests是个HTTPClient库,相比于urllib,urllib2等模块比更加简洁易用 ■ get请求 作为示例,讲一下关于requests如何发起并处理一个get请求 ...
- python自动化运维笔记2 —— IP地址处理模块IPy
1.2 实用的IP地址处理模块IPy ip地址规划是网络设计中非常重要的一个环节,规划的好坏会直接影响路由协议算法的效率,包括网络性能.可扩展性等方面,在这个过程当中,免不了要计算大量的IP地址,包括 ...
- Python IPy模块
#!/usr/bin/env python # -*- coding: utf-8 -* # Created by YangYongming at 2018/09/17 20:22 # FileNam ...
- 实用的IP地址处理模块IPy
https://www.cnblogs.com/cherishry/p/5916935.html IPy安装 pip install IPy IP地址.网段的基本处理 IPy模块包含IP类,使用它可以 ...
- Python模块学习 - IPy
简介 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6地址的类与工 ...
- 2.python IP/DNS地址处理之IPy/Dnspython模块
1.IPy模块 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6 ...
随机推荐
- POJ 2379 ACM Rank Table(排序)
题很水,数据注意一下四点即可: 1.有些team会在一道题AC了之后还提交,这个时候只需要算第一次ac的时间以及这之前的wa,之后的全部忽略.2.如果一道题没有ac,那么在计算时间时不应该加上它的wa ...
- 【POJ3358】
题目描述: 题意: 就是给定一个a/b,求a/b的结果变成二进制之后的小数.这个小数后面会有一段循环节,只要求输出循环节开始循环的位置和循环长度. 分析: 这题我是这么想的,比如说样例中的1/5,我们 ...
- JAVA多线程的问题以及处理【转】
http://www.cnblogs.com/springcsc/archive/2009/12/03/1616394.html 12.4 多线程问题及处理 多线程编程为程序开发带来 ...
- 套题T4
Problem 1 无聊的gcd(gcd.c/cpp/pas) 话说出题人不会被查水表吧. 简单的问题描述:从N个正整数里面取出K个数的最大公因数最大是多少.(请将答案乘上k之后输出哦,谢谢合作.) ...
- 使用apt-fast 来加速你的Ubuntu 的apt
使用apt-fast 来加速你的Ubuntu 的apt sudo add-apt-repository ppa:apt-fast/stable sudo apt-get update sudo apt ...
- 腾讯QQ的开发分客户端软件和服务器端软件
Windows客户端主要是C++ COM/ATL Q+Web 后端C++ CGI ,前端javascript和flash 望采纳 腾讯QQ使用何种开发平台? 腾讯QQ的开发分客户端软件和服务器端软件两 ...
- Android:Android SDK Manager顺利下载
默认的Android SDK只有Android 4.4的版本,如果需要其他版本的模拟器,需要Android SDK Manager下载, 1.打开Eclipse 2.选择Android SDK Man ...
- context:component-scan扫描使用上的容易忽略的use-default-filters
问题 如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean.正确 <context:component-scan base-pa ...
- C++:运算符重载函数之"++"、"--"、"[ ]"、"=="的应用
5.2.5 "++"和"--"的重载 对于前缀方式++ob,可以用运算符函数重载为: ob.operator++() //成员函数重载 或 operator++ ...
- swift:高级运算符(位运算符、溢出运算符、优先级和结合性、运算符重载函数)
swift:高级运算符 http://www.cocoachina.com/ios/20140612/8794.html 除了基本操作符中所讲的运算符,Swift还有许多复杂的高级运算符,包括了C语和 ...