Python IPAddress module uses

Source: Internet
Author: User

# -*- coding:utf-8 -*-import ipaddressn1 = ipaddress.ip_network (' 192.0.2.0/28 ') N2 = ipaddress.ip_network (' 192.0.2.1/32 ') a = list (N1.address_exclude (n2)) print (a) for  I in a:    print (i)  #  remove N2 from N1 b = list (ipaddress.ip_network (' 192.168.0.0/29 '). Hosts ()) print (b) for i in b:    print (i)   #   Print host address c = list (Ipaddress.ip_network (' 192.168.0.0/22 '). Subnets (new_prefix=24)) print (c) for  I in c:    print (i)  #  split ' 192.168.0.0/22 ' d = list with a 24-bit mask ( Ipaddress.ip_network (' 192.0.2.0/24 '). Supernet (new_prefix=20)) print (d) Print (Ipaddress.ip_network (' 192.0.2.0/24 ') ). Supernet (new_prefix=20)) for addr in ipaddress. Ipv4network (' 192.168.2.0/28 '):     print (addr)  #  print ' 192.168.2.0/28 ' IP address, Note the difference between the above host address and the interface = ipaddress. Ipv4interface (' 192.168.1.1/23 ') print (INTERFACE.IP)  #  printing IP address print (interface.network)  #  print the owning segment print (Interface.with_netmask The string representation of the  #  interface to the network is represented as a string representation of the netmask print (Interface.with_prefixlen)  #  interface with a mask, preceded by a prefix symbol. Print (Ipaddress.ip_address (3221225985))  #  convert to IP address print (ipaddress.v4_int_to_packed (3221225985))    #   Convert to byte print (ipaddress. IPv4Address (3221225985))  #  convert to IP address print (ipaddress. IPv4Address (b ' \xc0\x00\x02\x01 '))  #  convert to IP address d = [ipaddr for ipaddr in  Ipaddress.summarize_address_range (    ipaddress. IPv4Address (' 192.168.1.0 '),     ipaddress. IPv4Address (' 192.168.1.25 '))]for i in d:    print (i)  # network  objects can be iterated to list all the addresses belonging  to the network.e = [ipaddr for ipaddr in ipaddress.collapse_ Addresses ([ &NBsp;  ipaddress. Ipv4network (' 192.168.1.0/31 '),     ipaddress. Ipv4network (' 192.168.1.2/31 '),     ipaddress. Ipv4network (' 192.168.1.4/30 ')])]for i in e:    print (i)  #  Merge the above three segments into print (Ipaddress.ip_network (' 192.0.2.1/32 '). Compare_networks (Ipaddress.ip_network (' 192.0.2.2/32 '))  #  results for -1print (ipaddress.ip_network (' 192.0.2.1/32 '). Compare_networks (Ipaddress.ip_network (' 192.0.2.0/ )  #  results for 1print (ipaddress.ip_network (' 192.0.2.1/32 '). Compare_networks (' Ipaddress.ip_network (' 192.0.2.1/32 '))  #  results for 0print (IPAddress. IPv4Address (' 127.0.0.2 ')  > ipaddress. IPv4Address (' 127.0.0.1 ')) print (IPAddress. IPv4Address (' 127.0.0.2 ')  == ipaddress. IPv4Address (' 127.0.0.1 ')) print (IPAddress. IPv4Address (' 127.0.0.2 ')  != ipaddress. IPv4Address (' 127.0.0.1 '))   #  compare IP address #  instance one: Merge IP address with open ("IP.txt", ' R ', encoding= ' utf-8 ')  AS F:  &NBsp; allip = f.readlines () ip = [ipaddress. Ipv4network (Net.strip (' \ n '))  for net in allip]ip2 = [addr for addr  in ipaddress.collapse_addresses (IP)]for i in ip2:    print (i) #   Example Two: Remove the IP address (the IP address in the N2 from the N1) n1 = [ipaddress.ip_network (' 192.0.2.0/28 '), Ipaddress.ip_network (' 192.1.2.0/28 ')]n2 = [ipaddress.ip_network (' 192.1.2.1/32 '), Ipaddress.ip_network (' 192.0.2.4/32 ')]y  = []for i in n1:    for x in n2:         try:             y = list (I.address_exclude (x))         except  exception:            pass     For z in y:        prinT (z) 


This article is from the "Baby God" blog, make sure to keep this source http://babyshen.blog.51cto.com/8405584/1870014

Python IPAddress module uses

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.