How can I convert a CIDR prefix to a dotted-quad netmask in Python?
For example, if the prefix is 12
I need to return 255.240.0.0
.
How can I convert a CIDR prefix to a dotted-quad netmask in Python?
For example, if the prefix is 12
I need to return 255.240.0.0
.
You can do it like this:
And this is a more efficient one:
or, if you have difficulties counting the number of F:
and now a possibly even more efficient (albeit more difficult to read):
To get the dotted.quad version of the mask, you can use inet.ntoa to convert the above netmask.
Note: for uniformity with other message, I used 'len' as the mask length, even if I do not like to use a function name as variable name.
Here is a solution on the lighter side (no module dependencies):