Python Learning-ipy Module

Source: Internet
Author: User
Tags ip number

classes and tools for handling IPv4 and IPV6 addresses and networks

IPy-classes and tools for handling IPv4 and IPV6 addresses and networks.

#! /env python3#coding=utf-8# uses pip install ipy, the latest version is 0.83 ' IP address, basic processing of network segment ' import  ipyfrom ipy import ip  #调用IPip_v4  = ip (' 192.168.1.0/24 '). Version ()  # Determine the type of the IPv4 address segment Ip_v6 = ip (':: 1 '). Version ()   #判断ipv6地址段的类型print   (IP_V4) print  (IP_V6) IP  = ip (' 192.168.1.0/24 ')   #输入192.168.1.0/24 Network Segment print  (Ip.len ())   #192.168.1.0/ 24 Network segment IP number for x in ip:    print  (x)    #输出192.168.1.0/ 24 Network Segment all IP list "' Resolution name, IP type, IP conversion, etc. ' Ip_adr = ip (' 192.168.1.20 ') print  (Ip_adr.reversenames ())   #反向解析地址格式ip_adr_type  = ip (' 8.8.8.8 '). Iptype ()   #判断8.8.8.8 is the public address or private address print  (Ip_adr_type) ip_ Adr_int = ip (' 8.8.8.8 '). Int ()   #将地址转换为整数形式ip_adr_hex  = ip (' 8.8.8.8 '). Strhex ()  # Convert to 16 binary format ip_adr_bin = ip (' 8.8.8.8 '). Strbin ()   #转换为二进制格式print   (ip_adr_int) print  (Ip_adr _hex) print  (ip_adr_bin) "Network address translation, for example, based on IP and mask productionNetwork segment format ' Ip_net = ip (' 192.168.1.0 ') print  (ip_net.make_net (' 255.255.255.0 '))  # Get 192.168.1.0 with mask 255.255.255.0 calculation, CDR format,192.168.1.0/24print  (IP (' 192.168.1.0/255.255.255.0 ', make_net=true))   #将192.168.1.0/255.255.255.0 address format to 192.168.1.0/24print  (IP (' 192.168.1.0-192.168.1.255 ', make_net=true )   #将192.168.1.0-192.168.1.255 address segment conversion to 192.168.1.0/24 format "" Specify different Wanprefixlen parameters by Strnormal method to make different output type networks. The output type is the string "' print  ((IP (' 192.168.1.0/24 ')). Strnormal (0))   #wanltprefixlen = 0, no return, e.g. 192.168.1.0print   (IP (' 192.168.1.0/24 '). Strnormal (1))   #wanltprefixlen =1,prefix format, such as 192.168.1.0/24print  (IP (' 192.168.1.0/24 ')). Strnormal (2))   #wanltprefixlen =2,decimalnetmask format, such as 192.168.1.0/255.255.255.0;print  ((IP (' 192.168.1.0/24 ')). Strnormal (3))   #wanltprefixlen =3,lastip format, such as 192.168.1.0-192.168.1.255 ' multi-Network Computing method ' IP (' 10.0.0.0/24 ')  < ip (' 12.0.0.0/24 ') #判断10.0.0.0/24 segment is less than 12.0.0.0/24 and the output is Trueip (' 192.168.1.100 ')  in ip (' 192.168.1.0/24 ') #判断192.If the 168.1.100 address is in the 192.168.1.0/24 segment, the output is true ' ' to determine whether there is overlap in two segments, using the overlaps method provided by Ipy ' ' IP (' 192.168.0.0/23 '). Overlaps ( ' 192.168.1.0/24 ')     #返回值为1, on behalf of overlapping IPs (' 192.168.1.0/24 '). overlaps (' 192.168.2.0 ')         #返回值为0, represented as non-existent overlap

Returns the output of information such as network, mask, broadcast, reverse resolution, subnet number, etc. according to the input IP or subnet

#! /env python3#coding=utf-8 ' ' ' Returns Network, mask, broadcast, direction resolution, number of subnets, IP type, etc. according to the input IP or subnet ' From ipy import  ipip_s = input (' please input an ip or net-range:  ')      #接收用户输入, parameter is IP address or network address IPS&NBSP;=&NBSP;IP (ip_s) If len (IPs)  > 1:        #为1个网络地址     print (' net: %s '  % ips.net ())            #输出网络地址     print (' netmask: %s '  %  Ips.netmask ())      #输出网络掩码地址     print (' broadcast: %s '  %  Ips.broadcast ())      #输出网络广播地址     print (' reverse address: %s ')  % ips.reversenames () [0])      #输出地址反向解析     print (' Subnet:  %s '  % len (IPs))      #输出网络子网数else:     #为单个IP地址      print (' REVERSE&Nbsp;address: %s '  % ips.reversenames () [0])    #输出IP反向解析print (' hexadecimal: %s ')  % ips.strhex ())      #16进制地址格式print (' binary ip: %s '  %  Ips.strbin ())      #二进制地址格式print (' iptype: %s '  % ips.iptype ())               #输出地址类型, public network or private network


Python Learning-ipy Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.