# update: 2021-6-21-16 def int_to_ip(integer): """ 整型转ip """ raw = bin(int(integer)).lstrip('0b').zfill(32) return '%d.%d.%d.%d' % (int(raw[0:8], 2), int(raw[8:16], 2), int(raw[16:24], 2), int(raw[24:32], 2)) def ip_to_int(ip): """ ip转整型 """ if not ip: return 0 ip = str(ip) raw = ip.split('.') if len(raw) != 4: return 0 binnum = bin(int(raw[0])).lstrip('0b').zfill(8) binnum += bin(int(raw[1])).lstrip('0b').zfill(8) binnum += bin(int(raw[2])).lstrip('0b').zfill(8) binnum += bin(int(raw[3])).lstrip('0b').zfill(8) return int(binnum, 2)