實現根據ip尋找出對應的地區code,
對應的尋找常值內容格式如下
iparea 22165248 22165503 CN6109
iparea 22165504 22347775 CN6100
iparea 22347776 22413311 CN6101
iparea 22413312 22544383 CN6100
iparea 22544384 23068671 CN1102
iparea 24379392 24641535 CN0000
iparea 27262976 28311551 CN9100
iparea 28573696 28835839 CN1500
iparea 28835840 28966911 CN4401
..............................
areas_arr是儲存了上面文本的字典/數組,其key是對應的在文本中的行號
awk實現函數
def search_newarea(areas_arr, ip, s, e):
start=s;
end=e;
ips = ip.split(".")
long_ip=int(ips[0])*256*256*256 + int(ips[1])*256*256 + int(ips[2])*256 + int(ips[3]);
while(start <= end and start >= s and end <= e):
middle=int((start+end)/2);
ip_range = areas_arr[middle].split(",")
if(long_ip>=int(ip_range[0]) and long_ip<=int(ip_range[1])):
return ip_range[2];
if(long_ip>int(ip_range[0])):
start=middle+1;
else:
end=middle-1;
return '';
python實現代碼
def search_newarea(areas_arr, ip, s, e):
start=s;
end=e;
ips = ip.split(".")
long_ip=int(ips[0])*256*256*256 + int(ips[1])*256*256 + int(ips[2])*256 + int(ips[3]);
while(start <= end and start >= s and end <= e):
middle=int((start+end)/2);
ip_range = areas_arr[middle].split(",")
if(long_ip>=int(ip_range[0]) and long_ip<=int(ip_range[1])):
return ip_range[2];
if(long_ip>int(ip_range[0])):
start=middle+1;
else:
end=middle-1;
return '';