python实现IP地址转换为32位二进制
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class IpAddrConverter(object):
def __init__(self, ip_addr):
self.ip_addr = ip_addr
@staticmethod
def _get_bin(target):
if not target.isdigit():
raise Exception('bad ip address')
target = int(target)
assert target < 256, 'bad ip address'
res = ''
temp = target
for t in range(8):
a, b = divmod(temp, 2)
temp = a
res += str(b)
if temp == 0:
res += '0' * (7 - t)
break
return res[::-1]
def to_32_bin(self):
temp_list = self.ip_addr.split('.')
assert len(temp_list) == 4, 'bad ip address'
return ''.join(list(map(self._get_bin, temp_list)))
if __name__ == '__main__':
ip = IpAddrConverter("192.168.25.68")
print(ip.to_32_bin())