def analyse(self,filename=None): message = {} # 调用mailAnalyse中的各个模块,进行分析和返回结果 mail = MailInfo() mail.analyseMail(filename) ip = mail.get_ip() # 使用ip进行联网判断 sender = mail.get_sender() # 使用sender进行黑白名单判断 c = mail.get_content() # 使用content进行机器学习的判断 # 判断是否存在于名单中,存在直接返回,否则进行下一步 try: r = Rule() judgeResult = r.find(sender) if judgeResult: r.content(c) message['rule'] = r.getInfo() if judgeResult is False: return str(message),judgeResult elif judgeResult is True: return str(message),judgeResult except: message['rule'] = 'wrong' # 判断是否被网站列为黑名单,正常则进行下一步,由于COOKIE问题,最容易出现问题 try: judge = Judge() judgeResult = judge.is_spam(ip) message['ipInfo'] = judge.get_info() if not judgeResult: return str(message),judgeResult except: message['ipInfo'] = 'wrong' # 通过机器学习进行内容的判断 try: strAttribute = MailAttribute(c).get_attribute() strAttribute = r.filter(strAttribute) numAtrribute = AToD().transfer_array(strAttribute) judgeResult = JudgeContent().judge(numAtrribute) message['machineJudge'] = str(judgeResult) except: message['machineJudge'] = 'wrong' # 组织信息,以便显示 if judgeResult : info = 'rule: '+message['rule']+'\r\n'+\ 'ipInfo: '+message['ipInfo']+'\r\n'+\ 'machineJudge: normal' else: info = 'rule: ' + message['rule'] + '\r\n' + \ 'ipInfo: ' + message['ipInfo'] + '\r\n' + \ 'machineJudge: trash' return info,judgeResult
def __parse(self, e, leave): for item in e: rule = Rule(leave) for child in item: if child.tag == 'from': rule.from_address = child.text if child.tag == 'to': rule.to_address = child.text if child.tag == 'cc': rule.cc_address = child.text if child.tag == 'subject': rule.subject = child.text if child.tag == 'content': rule.content = child.text if leave: self.__leave.append(rule) else: self.__discard.append(rule) Logger.info(str(rule))