22import re
33import yaml
44import sys
5- import os
65import time
76import bisect
87import logging
98from os .path import isfile
10- import socket
11- import struct
129
1310logger = logging .getLogger (__name__ )
1411
@@ -22,42 +19,38 @@ def ip2long(ip):
2219def long2ip (num ):
2320 "convert long int to dotted quad string"
2421 iplist = []
25- for n in range (4 ):
22+ for _ in range (4 ):
2623 num , mod = divmod (num , 256 )
2724 iplist .insert (0 , str (mod ))
2825 return '.' .join (iplist )
2926
3027
3128class BaseIPPool (object ):
29+
3230 def __init__ (self , ipfile , recordfile ):
33- if not isfile (ipfile ):
31+ if not isfile (ipfile ) or not isfile ( recordfile ) :
3432 logger .warning ("can't find ip data file: %s" % ipfile )
35- # 故意返回数据,另程序退出
36- return 1
37- self .ipfile = ipfile
38-
39- if not isfile (recordfile ):
40- logger .warning ("can't find A record file: %s" % recordfile )
41- return 2
42- self .recordfile = recordfile
33+ else :
34+ self .ipfile = ipfile
35+ self .recordfile = recordfile
4336
44- # 初始化iplist,用来进行2分查找
45- self .iplist = []
46- # 初始化iphash,用来检索详细信息
47- self .iphash = {}
37+ # 初始化iplist,用来进行2分查找
38+ self .iplist = []
39+ # 初始化iphash,用来检索详细信息
40+ self .iphash = {}
4841
49- # 初始化存储a.yaml配置
50- self .record = {}
51- # 存储各个域名的地域对于ip信息
52- self .locmapip = {}
42+ # 初始化存储a.yaml配置
43+ self .record = {}
44+ # 存储各个域名的地域对于ip信息
45+ self .locmapip = {}
5346
54- # load record data
55- self .LoadRecord ()
47+ # load record data
48+ self .LoadRecord ()
5649
57- # load ip data
58- self .LoadIP ()
50+ # load ip data
51+ self .LoadIP ()
5952
60- print ('Init IP pool finished !' )
53+ print ('Init IP pool finished !' )
6154
6255 def LoadIP (self ):
6356 f = open (self .ipfile , 'r' )
@@ -186,7 +179,7 @@ def SearchLocation(self, ip):
186179 return i , j , ipnum
187180
188181 def FindIP (self , ip , name ):
189- i , j , ipnum = self .SearchLocation (ip )
182+ i , _ , ipnum = self .SearchLocation (ip )
190183 ip_list = None
191184 if i in self .iphash :
192185 ipstart = self .iphash [i ][0 ]
@@ -216,7 +209,9 @@ def __init__(self, ipfile, recordfile, monitor_mapping):
216209 self .finder = BaseIPPool (ipfile , recordfile )
217210
218211 def FindIP (self , ip , name ):
212+ start_time = time .time ()
219213 tmp_ip_list = self .finder .FindIP (ip , name )
214+ logger .warning ("use time: %s" % (time .time () - start_time ))
220215 ip_list = [
221216 tmp_ip for tmp_ip in tmp_ip_list if self .monitor_mapping .check (name , tmp_ip )]
222217 if len (ip_list ) == 0 :
0 commit comments