##将IP地址转为二进制
def ipToBinary(ip):
'''ip address transformat into binary
Argv:
ip: ip address
Return:
binary
'''
ip_num = ip.split('.')
x = 0
##IP地址是点分十进制,例如:192.168.1.33,共32bit
##第1节(192)向前移24位,第2节(168)向前移16位
##第3节(1)向迁移8位,第4节(33)不动
##然后进行或运算,得出数据
for i in range(len(ip_num)):
num = int(ip_num[i]) << (24 - i*8)
x = x | num
brnary = str(bin(x).replace('0b',''))
return brnary
##将子网掩码转为二进制
def maskToBinary(mask):
'''netmask change, example: 24 or 255.255.255.0 change binary
Argv:
mask: netmask, example:24 or 255.255.255.0
Return: