def print_all(path): group_d = defaultdict(list) print u"规则文件:" #.decode('utf-8').encode("gb2312") for s in get_rules(path): print '-' * 4, os.path.basename(s) try: tree = ET.parse(s) root = tree.getroot() # rules = [] for joint in root: rule = JOINT_RULE().fill_rule(root, joint) group_d[root.get('id')].append(rule) except ET.ParseError as e: print e, '\n', '#######', s sum = 0 print "*" * 80 for k, v in group_d.iteritems(): m = len(v) sum += m print '-' * 60 print v[0].groupName, ":", m for r in v: print '-' * 4, r.id, ":", r.name r.basic_check() print '--' * 4, '-->'.join([x["name"] for x in r.detail.states]) # print '---'*4,'\n'.join([x["eventFilter"] for x in r.detail.states]) print '#' * 70 print u"规则总数:", sum
def sort_and_store_nmap_info(file1): conn = sqlite3.connect(config.serverPortDBName) tree = ET.parse(file1) root = tree.getroot() hosts = root.findall("host") for host in hosts: ip = host.find("address").attrib['addr'] ports = host.find("ports").findall("port") for port in ports: portID = port.attrib['portid'] serviceName = port.find( "service").attrib['name'] if "name" in port.find( "service").attrib else "Null" productName = port.find( "service").attrib['product'] if "product" in port.find( "service").attrib else "Null" productVersion = port.find( "service").attrib['version'] if "version" in port.find( "service").attrib else "Null" #print("{} {} {} {} {}".format(ip,portID,serviceName,productName,productVersion)) metaStr = ip + portID + serviceName + productName + productVersion h1 = hashlib.md5() h1.update(metaStr.encode("utf-8")) sql = "INSERT OR IGNORE INTO portInfoDB VALUES('{}','{}','{}','{}','{}','{}')".format( h1.hexdigest(), ip, portID, serviceName, productName, productVersion) conn.execute(sql) conn.commit() conn.close()
def extractNmapInfo(self, file1): tree = ET.parse(file1) root = tree.getroot() hosts = root.findall("host") tmpList = [] for host in hosts: ip = host.find("address").attrib['addr'] ports = host.find("ports").findall("port") tmpDict = {} for port in ports: portID = port.attrib['portid'] serviceName = port.find( "service").attrib['name'] if "name" in port.find( "service").attrib else "Null" productName = port.find( "service").attrib['product'] if "product" in port.find( "service").attrib else "Null" productVersion = port.find( "service").attrib['version'] if "version" in port.find( "service").attrib else "Null" # print("{} {} {} {} {}".format(ip,portID,serviceName,productName,productVersion)) metaStr = ip + portID + serviceName + productName + productVersion h1 = hashlib.md5() h1.update(metaStr.encode("utf-8")) hashstr = h1.hexdigest() tmpDict['hashstr'] = hashstr tmpDict['ip'] = ip tmpDict['port'] = portID tmpDict['serviceName'] = serviceName tmpDict['productName'] = productName tmpDict['productVersion'] = productVersion tmpList.append(tmpDict) return tmpList.copy()
def load_xml(xmlfilename): xmlstr = os.path.abspath(xmlfilename) try: tree = ET.parse(xmlstr) #print ("tree type:", type(tree)) return tree.getroot() except Exception as e: print("parse %s fail!" % xmlstr) return None
def parse(self): try: tree = ET.parse(self.xml) root = tree.getroot() data = self.get_node_data(root) # print(data) except Exception as e: print(e) data = {} return data
def ReadXml(self): try: print("xmlfile:", self.xml_file_path) self.tree = ET.parse(self.xml_file_path) self.root = self.tree.getroot() except Exception as e: print("parse xml faild!") sys.exit() else: print("parse xml success!") finally: return self.tree
def get_match(path, e_path): group_d = defaultdict(list) # print u"规则文件:" #.decode('utf-8').encode("gb2312") for s in get_rules(path): # print '-'*4,os.path.basename(s) try: tree = ET.parse(s) root = tree.getroot() # rules = [] for joint in root: rule = JOINT_RULE().fill_rule(root, joint) group_d[root.get('id')].append(rule) except ET.ParseError as e: print e, s # print "*" * 80 no_rules = get_file_rules(e_path) results = defaultdict(str) for k, v in group_d.iteritems(): # print '-'*60 # print v[0].groupName for r in v: # print '-'*4,r.id,":", r.name r.basic_check() for states in r.detail.states: # print '--'*4,states["name"] rp = r"evm_netthreat_([\d]+)" state_rules = re.findall(rp, states["eventFilter"]) if state_rules: for rr in state_rules: if rr in no_rules: results[rr] = states["name"] # print rr # print states["eventFilter"] print "match counts:%d/%d" % (len(results.keys()), len(no_rules)) for dd in no_rules: if results[dd]: print dd, ",", results[dd] else: print dd, ",Is not in any EP rules"
def get_element_list(self) -> list: r"""解析uidump.xml文件并将其中包含的控件元素以列表的形式返回. """ try: tree = ET.parse(self.path) nodes = tree.getiterator("node") if len(nodes) == 0: for tag in self.AndroidNodeClassTypeList: nodes.extend(tree.getiterator(tag)) element = [] for node in nodes: element.append(node.attrib) return element except (FileNotFoundError, FileExistsError): logger.warning( "File {} not found, please check the corresponding path". format(self.path))
def process_gdtv(cid, date, retry=3): url = 'http://epg.gdtv.cn/f/%s/%s.xml' % (cid, date) data = None while retry > 0: data = get(url, ctype='xml') if not data: LOG.error('[%s]No data, try again.' % cid) retry -= 1 else: break if not data: return None root = ET.fromstring(data.encode('utf8')) datas = list() for obj in root[1].findall('content'): datas.append({ 'time': timestamp_to_time(obj.attrib['time1']), 'program_name': obj.text }) return datas
def load_xml(xmlfilename): xmlstr = os.path.abspath(xmlfilename) try: tree = ET.parse(xmlstr) #print ("tree type:", type(tree)) # 获得根节点 return tree.getroot() except Exception as e: #捕获除与程序退出sys.exit()相关之外的所有异常 print ("parse %s fail!" % xmlstr) return None def list_inter(lista,listb): ulist = list(set(a_list).union(set(b_list))) ret = [item for item in ulist if item not in lista or item not in listb] #print(ret) # return ret def recurrent_check(nodea,nodeb): cnlvl_1 = [c.tag for c in rootcn] enlvl_1 = [c.tag for c in rooten] delta = list_inter(cnlvl_1,enlvl_1) if(delta): print("CN and EN string resouce file not match!==>") print(delta) return -3 for childa in nodea: childa = nodeb.find(childa.tag) if(not childb): print("CN and EN string resouce file not match!==>") print(childa.tag) else: return recurrent_check(childa,childb)
def readXml(filename): xml_tree = ET.parse(filename) root=xml_tree.getroot() q=queue.Queue() treeQ=queue.Queue() q.put(root) st=Tree('E') treeQ.put(st) while(q.empty()==0): node=q.get() t=treeQ.get() if(node.tag=='op'): t.value=node.text else: t.value=node.tag i=0 for child in node: q.put(child) t.appendSon(child.tag) treeQ.put(t.getChildren(i)) i+=1 # print("遍历root的下一层", child.tag, ) return st
def CreateNode(self, tag, attrib, text): element = ET.Element(tag, attrib) element.text = text print("tag:%s;attrib:%s;text:%s" % (tag, attrib, text)) return element
def __init__(self): parse = eTree.parse(self.xml_path) self.root = parse.getroot()
def parse_response(self, meta, content): try: import xml.etree.CElementTree as etree except ImportError: import xml.etree.ElementTree as etree return etree.fromstring(content)