1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| """ 出现错误最多的邮箱有: ['online.sh.cn'] 其出错次数为: 6 """
import re
pat_delivery = "delivery \d{0,}" pat_id = "\d+" pat_start = "starting" pat_mail = "\w+@\S+\.(com|cn)" pat_status = "(success|deferral|failure)" pat_ip= "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
mail_error = "mail not found" ip_error = "ip not found"
id_mail_dict = {}
info_list = []
error_count = {}
error_max_list = [] try: maillog = open("maillog.txt","r") for each in maillog: ret = re.search(pat_delivery,each) if ret is not None: isStart = re.search(pat_start,each) id = re.search(pat_id, str(ret.group())).group() if isStart is not None: mail = re.search(pat_mail,each).group() id_mail_dict[id] = mail else: status = re.search(pat_status,each).group() ip = re.search(pat_ip,each) try: if ip is None: ip = ip_error else: ip = ip.group() info_list.append((id,id_mail_dict[id],status,ip)) except: info_list.append((id,mail_error,status,ip)) try: info_file = open("result.txt","w") for each in info_list: id,mail,status,ip = each[0],each[1],each[2],each[3] str = "id:"+id+"\tmail:"+mail+"\tstatus:"+status+"\tip:"+ip info_file.write(str+"\n") if status == "deferral" or status == "failure": if mail != mail_error: mail_kind = mail.split("@")[1] if mail_kind not in error_count.keys(): error_count[mail_kind] = 1 else: error_count[mail_kind] += 1 sorted_count = sorted(error_count.items(),key= lambda x:x[1],reverse=True) max_count = sorted_count[0][1] print(sorted_count) for each in sorted_count: if each[1] == max_count: error_max_list.append(each[0]) else: break print("出现错误最多的邮箱有:\n",error_max_list) print("其出错次数为:\n",max_count)
except: print("文件创建有误,请检查您的操作是否正确") maillog.close() except FileNotFoundError: print("文件不存在,请检查您的日志文件是否有效")
|