Convert CIDR notation netmask to dotted decimal notation in Python

The way to convert a netmask from dotted decimal notation (ABCD) to CIDR notation is previously posted, but you need to convert it the other way around. I was pressed by, so I wrote it.

No error handling is done, so if you pass a strange netmask, a strange value will be returned. Please be careful.

code

import socket
import struct

def mask2cidr(mask):
    """Convert netmask from Dotted address to CIDR."""
    return bin(struct.unpack('!L', socket.inet_pton(socket.AF_INET, mask))[0])[2:].index('0')

test

>>> mask2cidr('255.255.255.0')
24
>>> mask2cidr('255.255.240.0')
20
>>> mask2cidr('255.255.255.192')
26

reference

-Convert CIDR notation with Python --Qiita

Recommended Posts

Convert CIDR notation netmask to dotted decimal notation in Python
Convert CIDR notation in Python
Convert exponential notation float to str in Python
Convert markdown to PDF in Python
Convert decimal numbers to n-ary numbers [python]
Convert psd file to png in Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
Convert FBX files to ASCII <-> BINARY in Python
Convert PDFs to images in bulk with Python
Convert cubic mesh code to WKT in Python
Class notation in Python
Convert timezoned date and time to Unixtime in Python2.7
How to convert / restore a string with [] in python
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
[python] Method to darken RGB value in hexadecimal notation
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
Convert callback-style asynchronous API to async / await in Python
Convert / return class object to JSON format in Python
[Python] Created a method to convert radix in 1 second
python, php, ruby How to convert decimal numbers to n-ary numbers
Convert Webpay Entity type to Dict type (recursively in Python)
[python] Convert date to string
To flush stdout in Python
[Python] Convert list to Pandas [Pandas]
Login to website in Python
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert IP address to decimal
Speech to speech in python [text to speech]
How to develop in Python
Convert python 3.x code to python 2.x
Post to Slack in Python
Convert images passed to Jason Statham-like in Python to ASCII art
Convert Excel file to text in Python for diff purposes
Determine the date and time format in Python and convert to Unixtime
[Python] How to do PCA in Python
Included notation in Python function arguments
How to collect images in Python
How to use SQLite in Python
Workflow to convert formula (image) to python
In the python command python points to python3.8
Convert list to DataFrame with python
Read big endian binary in Python and convert it to ndarray
Try to calculate Trace in Python
How to convert 0.5 to 1056964608 in one shot
Python> list> Convert double list to single list
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
[Python] Convert natural numbers to ordinal numbers
How to handle Japanese in Python
Python> tuple> Convert double tuple to single tuple
An alternative to `pause` in Python
I wrote a code to convert quaternions to z-y-x Euler angles in Python
What to do if there is a decimal in python json .dumps
I tried to create a program to convert hexadecimal numbers to decimal numbers with python