简体   繁体   中英

Understanding the Python netaddr library commands

I am trying to understand how some code operates from the netaddr Python tutorials on https://pythonhosted.org/netaddr/tutorial_01.html . In particular the following tutorial.

Summarizing list of addresses and subnets

Another useful operation is the ability to summarize groups of IP subnets and addresses, merging them together where possible to create the smallest possible list of CIDR subnets.

You do this in netaddr using the cidr_merge() function.

First we create a list of IP objects that contains a good mix of individual addresses and subnets, along with some string based IP address values for good measure. To make things more interesting some IPv6 addresses are thrown in as well.

 >>> ip_list = [ip for ip in IPNetwork('fe80::/120')] >>> ip_list.append(IPNetwork('192.0.2.0/24')) >>> ip_list.extend([str(ip) for ip in IPNetwork('192.0.3.0/24')]) >>> ip_list.append(IPNetwork('192.0.4.0/25')) >>> ip_list.append(IPNetwork('192.0.4.128/25')) >>> len(ip_list) 515 >>> cidr_merge(ip_list) [IPNetwork('192.0.2.0/23'), IPNetwork('192.0.4.0/24'), IPNetwork('fe80::/120')] 

I am a bit confused with the different options available. What is the difference between ip_list.extend([str(ip) for ip in IPNetwork('192.0.3.0/24')]) and ip_list.append(IPNetwork('192.0.4.0/25')) .

If I don't want to start the list with an IPv6 ( fe80::/120 ) and instead want to use an IPv4 ( 192.0.4.0/24 ) what would the syntax be. Would it be as simple as the following?

ip_list = IPNetwork('192.0.4.0/25')

Thanks.

That code is trying to demonstrate that the cidr_merge() function can merge a list of netaddr.IPAddress objects, IP address strings, and netaddr.IPNetwork objects into a minimal group of IP addresses and subnets. To do that a Python list containing various IP address related objects is created. That list is then passed to cidr_merge() to show that the ip address objects have been coalesced into a minimal collection of objects.

Initially it creates a Python list ip_list containing a number of individual netaddr.IPAddress objects. To this list it appends a IPNetwork subnet object. Note that this is a standard Python list operation - list.append() simply adds its argument to the end of the list, eg:

>>> l = [1, 2]
>>> l.append(3)
>>> print l
[1, 2, 3]

list.extend() extends the given list with elements from another list (well, any iterable actually), eg

>>> l.extend([4, 5, 6])
>>> print l
[1, 2, 3, 4, 5, 6]

See the Python documentation for more details about these list operations.

So yes, it is as simple as:

>>> ip_list = [IPNetwork('192.0.4.0/25')]    # create a list containing IPNetwork object
>>> print ip_list
[IPNetwork('192.0.4.0/25')]

and appending/extending the list as required.

With regard to the difference between Apend and Extend see:

https://stackoverflow.com/a/252711/1172412

You should be able to start the list like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM