本文實例講述了Python簡單實作子網路遮罩轉換的方法。分享給大家供大家參考,具體如下:
這裡實作將子網路遮罩長度轉換為具體的子網路遮罩位址:
##
def exchange_maskint(mask_int): bin_arr = ['0' for i in range(32)] for i in range(mask_int): bin_arr[i] = '1' tmpmask = [''.join(bin_arr[i * 8:i * 8 + 8]) for i in range(4)] tmpmask = [str(int(tmpstr, 2)) for tmpstr in tmpmask] return '.'.join(tmpmask) if __name__ == '__main__': print exchange_maskint(24)
#