Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
python ipc_python开发工具,希望能够帮助你!!!。
IPy是Python支持IP的各种操作第三方模块,需要pip安装后才能使用,安装方法:
pip install IPy
IPy支持的方法和属性:
>>> dir(IPy) ['INT_TYPES', 'IP', 'IPSet', 'IPV6_MAP_MASK', 'IPV6_TEST_MAP', 'IPint', 'IPv4ranges', 'IPv6ranges', 'MAX_IPV4_ADDRESS', 'MAX_IPV6_ADDRESS', 'STR_TYPES', '_BitTable', '__builtins__', '__cached__', '__doc__', '__file__','__loader__', '__name__', '__package__', '__spec__', '__version__', '_checkNetaddrWorksWithPrefixlen', '_checkNetmask', '_checkPrefix', '_count0Bits', '_count1Bits', '_countFollowingZeros', '_intToBin', '_ipVersionToLen', '_netmaskToPrefixlen', '_parseAddressIPv6', '_prefixlenToNetmask', '_remove_subprefix', 'bisect', 'collections_abc', 'intToIp', 'parseAddress', 'types', 'xrange']
IPy的IP方法是处理IP地址和网络的常用方法,可以说使用IPy基本就是使用IP方法,所以本文主要介绍IP方法支持的一系列操作
>>> ip=IP('127.0.0.0/30') >>> for x in ip: ... print(str(x)) ... 127.0.0.0 127.0.0.1 127.0.0.2 127.0.0.3 >>> print(str(ip[2])) 127.0.0.2 >>> print(str(ip[-1])) 127.0.0.3
>>> IP('10.0.0.0/8').broadcast() IP('10.255.255.255') >>> print(IP('127.0.0.1').make_net('255.0.0.0')) 127.0.0.0/8
>>> IP('10.0.0.0/8').net() IP('10.0.0.0') >>> IP('10.0.0.0/8').netmask() IP('255.0.0.0')
>>> print(IP('195.185.1.1').reverseName()) 1.1.185.195.in-addr.arpa. >>> print(IP('195.185.1.0/28').reverseName()) 0-15.1.185.195.in-addr.arpa. >>> IP('::1:2').reverseName() '2.0.0.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.' >>> IP('ff02::/64').reverseName() '0.0.0.0.0.0.0.0.0.0.0.0.2.0.f.f.ip6.arpa.' >>> IP('213.221.113.87/32').reverseNames() ['87.113.221.213.in-addr.arpa.'] >>> IP('213.221.112.224/30').reverseNames() ['224.112.221.213.in-addr.arpa.', '225.112.221.213.in-addr.arpa.', '226.112.221.213.in-addr.arpa.', '227.112.221.213.in-addr.arpa.'] >>> IP('127.0.0.0/24').reverseNames() ['0.0.127.in-addr.arpa.'] >>> IP('127.0.0.0/23').reverseNames() ['0.0.127.in-addr.arpa.', '1.0.127.in-addr.arpa.'] >>> IP('127.0.0.0/16').reverseNames() ['0.127.in-addr.arpa.'] >>> IP('127.0.0.0/15').reverseNames() ['0.127.in-addr.arpa.', '1.127.in-addr.arpa.'] >>> IP('128.0.0.0/8').reverseNames() ['128.in-addr.arpa.'] >>> IP('128.0.0.0/7').reverseNames() ['128.in-addr.arpa.', '129.in-addr.arpa.'] >>> IP('::1:2').reverseNames() ['2.0.0.0.1.ip6.arpa.']
>>> IP('10.0.0.0') < IP('2001:db8::') 1 Then the first address is compared. Lower addresses are always smaller: >>> IP('10.0.0.0') > IP('10.0.0.1') 0 >>> IP('10.0.0.0/24') > IP('10.0.0.1') 0 >>> IP('10.0.1.0') > IP('10.0.0.0/24') 1 >>> IP('10.0.1.0/24') > IP('10.0.0.0/24') 1 >>> IP('10.0.1.0/24') > IP('10.0.0.0') 1 Then the prefix length is compared. Shorter prefixes are considered smaller than longer prefixes: >>> IP('10.0.0.0/24') > IP('10.0.0.0') 0 >>> IP('10.0.0.0/24') > IP('10.0.0.0/25') 0 >>> IP('10.0.0.0/24') > IP('10.0.0.0/23') 1
>>> 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
>>> IP('10.0.0.0/24').__hash__() -
>>> 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 >>> print(IP('127.0.0.1/255.0.0.0', make_net=True)) 127.0.0.0/8
>>> "%X" % IP('10.10.10.10').int() 'A0A0A0A'
>>> print(IP('127.0.0.1').iptype()) LOOPBACK >>> print(IP('192.168.1.1').iptype()) PRIVATE >>> print(IP('195.185.1.2').iptype()) PUBLIC >>> print(IP('::1').iptype()) LOOPBACK >>> print(IP('2001:0658:022a:cafe:0200::1').iptype()) ALLOCATED RIPE NCC
>>> print(IP('195.185.1.0/28').len()) 16 >>> print(IP('195.185.1.0/24').len()) 256
>>> 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('10.0.0.0/8').prefixlen() 8
>>> print(IP('127.0.0.1').strBin()) 000000000000000000001 >>> print(IP('2001:0658:022a:cafe:0200::1').strBin()) 00000000000000000000000000000000000000000000000000000000000001
>>> print(IP('127.0.0.1').strDec()) >>> print(IP('2001:0658:022a:cafe:0200::1').strDec()) 46625
>>> print(IP('127.0.0.1').strHex()) 0x7f000001 >>> print(IP('2001:0658:022a:cafe:0200::1').strHex()) 0xacafe00001
>>> print(IP('195.185.0.0/16').strNetmask()) 255.255.0.0 >>> print(IP('2001:0658:022a:cafe::0/64').strNetmask()) /64
>>> print(IP('127.0.0.1').strNormal()) 127.0.0.1 >>> print(IP('2001:0658:022a:cafe:0200::1').strNormal()) 2001:658:22a:cafe:200:0:0:1
>>> IP('10.0.0.0/8').version() 4 >>> IP('::1').version() 6
>>> IP('192.168.1.1').strNormal() '192.168.1.1'
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。