class IpinfoOperater: __taobaoapi = None __dbconn = None def __init__(self): self.__taobaoapi = IpinfoApi() self.__dbconn = Dbconnecter() def __get_from_taobao(self, ip): retry = 3 value = [] while retry > 0: result = self.__taobaoapi.getipinfo(ip) if ('code' in result.keys()) and (result['code'] == 0): value.append(result['data']['ip']) value.append(result['data']['country']) value.append(result['data']['region']) value.append(result['data']['city']) value.append(result['data']['isp']) break retry = retry - 1 return value def __check_ip_exist(self, ip): sql = 'select count(*) from ipinfo where ipaddr = ' + '\'' + ip.strip() + '\';' result = self.__dbconn.selectSql(sql) return result[0] def set_ipinfo(self, ip): if ip is None or ip == '': return -1 if self.__check_ip_exist(ip) < 1: sql = "INSERT INTO ipinfo VALUES(%s,%s,%s,%s,%s)" value = self.__get_from_taobao(ip) #print value if len(value): retult = self.__dbconn.executeSql(sql,value) return retult print ip return -2
def __init__(self): self.__taobaoapi = IpinfoApi() self.__dbconn = Dbconnecter()
#logfile = open("testapi.uhouzz.com.access.log") etl = Nginxetl() ipinfo = IpinfoOperater() sql = "INSERT INTO web_log.nginx_log VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" for web_log_path in web_log_paths: servername = web_log_path.keys()[0] fullpath = web_log_path[servername] filename = os.path.basename(fullpath) path = os.path.dirname(fullpath) cut = Nginxcut(path, path, nginx_pid, filename) newfilename = cut.cutlog() logfile = open(newfilename) dbconn = Dbconnecter() lastline = '' while True: line = logfile.readline() if not line: break value = etl.getetl(line) if not value: continue #print value[0] value.append(servername) #print value ipinfo.set_ipinfo(value[1]) ret = dbconn.executeSql(sql, value) if ret == -1: print value