About netaddr that operates IP address, CIDR, etc. in Python

CIDR An acronym for Classless inter-domain routing. The meaning is "a mechanism that enables IP address assignment and route selection without using the concept of address class". (From the IT terminology dictionary that makes you feel "understood" even if you "understand" and "do not understand") https://wa3.i-3-i.info/word11990.html This site explains various IT terms. The description is very easy to understand even for amateurs, and it is useful.

netaddr A Python library used to operate IP networks. The official documentation (https://pypi.org/project/netaddr/) describes A network address manipulation library for Python. You can easily compare IP networks, take a set of differences, take an intersection, get a list of included IP addresses, and so on.

Example). Difference set

Use IPSet () to get the set of differences between IP blocks in CIDR notation.

>>> s1 = IPSet(['0.0.0.0/0']) # 0.0.0.0/Add 0 as an element
>>> s1.remove('255.255.255.255') # remove()Delete only the address specified in
>>> s1
IPSet(['0.0.0.0/1', '128.0.0.0/2', '192.0.0.0/3', '224.0.0.0/4', '240.0.0.0/5', ... '255.255.255.254/32'])

If you do this, the remaining IP blocks and addresses after deducting the address specified by remove will be returned in a list. Trying to do this without a library is quite annoying.

Recommended Posts

About netaddr that operates IP address, CIDR, etc. in Python
Get your own IP address in Python
About __all__ in python
Think about architecture in python
[Python] Insert ":" in MAC address
Convert CIDR notation in Python
About "for _ in range ():" in python
I thought about the logic that divides / 22 or / 23 into / 24 units by specifying the IP range in Python.
About psd-tools, a library that can process psd files in Python
About dtypes in Python and Cython
I learned about processes in Python
Hash method (open address method) in Python
Get the client's IP address in Django
Implement __eq__ etc. generically in Python class
Introducing the magical "jsii" that runs programs written in TypeScript with Python etc.