自己 proxy 里写的二叉代码,逻辑可以参考
def init_chn_list():
global CHN_IP, KEYS_CHN_IP
t = requests.get("
https://cdn.jsdelivr.net/gh/17mon/[email protected]/china_ip_list.txt", proxies=proxies, stream=True)
for l in t.iter_lines():
ip, sub = l.split(b'/')
ip = ip.decode('ascii')
ip = struct.unpack("!I", inet_aton(ip))[0]
ip_end = ip + 2 ** (32 - int(sub))
CHN_IP.append((ip, ip_end))
CHN_IP.sort(key=lambda key: key[0])
INDEX_CHN_IP = [i[0] for i in CHN_IP]
logger.info('init done')
def is_chn_ip(ip):
start, end = CHN_IP[bisect.bisect_right(INDEX_CHN_IP, ip) - 1]
if end > ip >= start:
return True
else:
return False