def unpacking_mis(file_path): str_reads = None str_write = None if not os.path.isfile(file_path): print "WARNING: file " + file_path + " don't exist." with codecs.open(file_path, 'r', encoding='gb2312', errors='ignore') as log: # 在读取完read和write之后用jion()方法合并字符串,然后去空格 reads = [] # 空read报文容器 writes = [] # 空write报文容器 # 记录读写状态 read = False write = False rev_buf = None for line in log.readlines(): pid = Library.ID(line) if rev_buf is None: rev_buf = Library.DIYSearch( Configuration.mis_clt_key_words['recv buf'], line) message_head = Library.message_head(line) if pid: # 如果开头是pid文 # 1.报文头 # 2.连续的报文尾 # 如果检测到报文类型关键词且是第一次出现 if 'write2 .....' in line and write is False: write = True if read is True: read = False # 释放read开关节省时间 elif 'read .....' in line and read is False: read = True if write is True: write = False elif 'read from MISP len' in line and read != 0: str_write = "".join(writes).replace("\n", "") str_reads = "".join(reads).replace("\n", "") return str_write, str_reads, rev_buf else: # 如果不是pid头 if message_head: # 只有可能是报文的头部被检测到了 if write: # print("W:" + line) writes += str( get_pure_8583( Library.message_head(line) + ": ", Library.message_tail(line), line)).split(" ") if read: # print("R:" + line) reads += str( get_pure_8583( Library.message_head(line) + ": ", Library.message_tail(line), line)).split(" ") return str_write, str_reads, rev_buf
def unpacking(file_path, type): result = [] # 空结果列表,采集完了再把列表变成字典 if not os.path.isfile(file_path): print "WARNING: file " + file_path + " don't exist." with codecs.open(file_path, 'r', encoding='gb2312', errors='ignore') as log: words = list(Configuration.get_re(type)) rules = list(Configuration.get_re(type).values()) for line in log.readlines(): # 一次性匹配多个关键词 for i in range(0, len(words)): if words[i] in line: if rules[i]: data = Library.DIYSearch(rules[i], line) if data: result.append((words[i], data)) rules[i] = None return result