LoginSignup
0
0

More than 3 years have passed since last update.

Pythonでサブネットマスクをプレフィックス表記からオクテット表記へ変換する方法

Last updated at Posted at 2021-04-13

趣旨

socket使う方法もあるけど、あんまり使いたくない人用

コード

from ipaddress import IPv4Interface

def cidr_to_netmask(cidr:str) -> str:
    itfc = IPv4Interface('0.0.0.0' + cidr)
    return str(itfc.netmask)

print(cidr_to_netmask('/0'))
print(cidr_to_netmask('/8'))
print(cidr_to_netmask('/16'))
print(cidr_to_netmask('/24'))
print(cidr_to_netmask('/32'))

結果

0.0.0.0
255.0.0.0
255.255.0.0
255.255.255.0
255.255.255.255
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0