业务场景:在一个目录里,有许多日志文件,里面是一条条的json数据,格式如下,为防止一个账号被多个ip使用,现在我想知道:哪些用户登录了哪些ip,和哪些ip登录了哪些用户,如果一个ip对应一个用户,就不展示了
import json, os, sys ip_map = {} email_map = {} path = 'E:/GoogleDownload/' #日志路径 type = 1 # 1:countByIp 2:countByEmail def countByIp(line): try: if line['ip'] in ip_map.keys(): ip_v_map = ip_map[line['ip']] if line['email'] in ip_v_map.keys(): new_email_count = ip_v_map[line['email']] + 1 ip_v_map[line['email']] = new_email_count else: ip_v_map[line['email']] = 1 ip_map[line['ip']] = ip_v_map else: ip_v_map = {} ip_v_map[line['email']] = 1 ip_map[line['ip']] = ip_v_map except: print("countByIp error: %s"%line) def countByEmail(line): try: if line['email'] in email_map.keys(): email_v_map = email_map[line['email']] if line['ip'] in email_v_map.keys(): new_ip_count = email_v_map[line['ip']] + 1 email_v_map[line['ip']] = new_ip_count else: email_v_map[line['ip']] = 1 email_map[line['email']] = email_v_map else: email_v_map = {} email_v_map[line['ip']] = 1 email_map[line['email']] = email_v_map except: print("countByEmail error: %s"%line) def getResult(file, type): fr = open(path+file, "r") line = fr.readline() line = json.loads(line) if type == 1: countByIp(line) elif type == 2: countByEmail(line) else: return i = 0 while line: line = fr.readline() if line == "" or line is None: continue i += 1 line = json.loads(line[:-1]) if type == 1: countByIp(line) elif type == 2: countByEmail(line) else: return print("读取%s, 行数:%s"%(file,i)) fr.close() if __name__ == "__main__": for f in os.listdir(path): if os.path.isfile(os.path.join(path, f)) and str(f).endswith('.log'): getResult(str(f), type) fw_ip = open(path + "result_ip.txt", "w") fw_email = open(path + "result_email.txt", "w") for ip in list(ip_map): if 1 == len(ip_map[ip]): ip_map.pop(ip) for ip in list(email_map): if 1 == len(email_map[ip]): email_map.pop(ip) fw_ip.write(str(ip_map)) fw_email.write(str(email_map)) fw_ip.close() fw_email.close()
最后,结果如下
当前ip被哪些用户登录了多少次
当前用户在哪些ip上登录过多少次