本文整理汇总了Python中xml.etree.cElementTree.XML.findall方法的典型用法代码示例。如果您正苦于以下问题:Python XML.findall方法的具体用法?Python XML.findall怎么用?Python XML.findall使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.cElementTree.XML
的用法示例。
在下文中一共展示了XML.findall方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mtgx2json
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def mtgx2json(graph):
zipfile = ZipFile(graph)
graphs = filter(lambda x: x.endswith(".graphml"), zipfile.namelist())
for f in graphs:
multikeys = []
xml = XML(zipfile.open(f).read())
links = {}
for edge in xml.findall(
"{http://graphml.graphdrawing.org/xmlns}graph/" "{http://graphml.graphdrawing.org/xmlns}edge"
):
src = edge.get("source")
dst = edge.get("target")
if src not in links:
links[src] = dict(in_=[], out=[])
if dst not in links:
links[dst] = dict(in_=[], out=[])
links[src]["out"].append(dst)
links[dst]["in_"].append(src)
for node in xml.findall(
"{http://graphml.graphdrawing.org/xmlns}graph/" "{http://graphml.graphdrawing.org/xmlns}node"
):
node_id = node.get("id")
node = node.find(
"{http://graphml.graphdrawing.org/xmlns}data/" "{http://maltego.paterva.com/xml/mtgx}MaltegoEntity"
)
record = OrderedDict({"NodeID": node_id, "EntityType": node.get("type").strip()})
props = {"Data": {}}
for prop in node.findall(
"{http://maltego.paterva.com/xml/mtgx}Properties/" "{http://maltego.paterva.com/xml/mtgx}Property"
):
value = prop.find("{http://maltego.paterva.com/xml/mtgx}Value").text or ""
entity_prop = {prop.get("displayName"): value.strip()}
props["Data"].update(entity_prop)
record.update(props)
s = " - ".join(["%s: %s" % (key, value) for (key, value) in record["Data"].items()])
record.pop("Data")
data = {"Data": s}
record.update(data)
link = {"Links": {}}
i_link = {"Incoming": links.get(node_id, {}).get("in_", 0)}
link["Links"].update(i_link)
o_link = {"Outgoing": links.get(node_id, {}).get("out", 0)}
link["Links"].update(o_link)
record.update(link)
multikeys.append(record)
return multikeys
示例2: mtgx2csv
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def mtgx2csv(opts):
zipfile = ZipFile(opts.graph)
graphs = filter(lambda x: x.endswith(".graphml"), zipfile.namelist())
for f in graphs:
filename = "%s_%s" % (opts.graph.replace(".", "_", 1), os.path.basename(f).replace(".graphml", ".csv", 1))
print "Writing data from %s/%s to %s..." % (opts.graph, f, filename)
with open(filename, "wb") as csvfile:
csv = writer(csvfile)
xml = XML(zipfile.open(f).read())
links = {}
for edge in xml.findall(
"{http://graphml.graphdrawing.org/xmlns}graph/" "{http://graphml.graphdrawing.org/xmlns}edge"
):
src = edge.get("source")
dst = edge.get("target")
if src not in links:
links[src] = dict(in_=0, out=0)
if dst not in links:
links[dst] = dict(in_=0, out=0)
links[src]["out"] += 1
links[dst]["in_"] += 1
for node in xml.findall(
"{http://graphml.graphdrawing.org/xmlns}graph/" "{http://graphml.graphdrawing.org/xmlns}node"
):
node_id = node.get("id")
node = node.find(
"{http://graphml.graphdrawing.org/xmlns}data/" "{http://maltego.paterva.com/xml/mtgx}MaltegoEntity"
)
row = [to_utf8(("Entity Type=%s" % node.get("type")).strip())]
for prop in node.findall(
"{http://maltego.paterva.com/xml/mtgx}Properties/" "{http://maltego.paterva.com/xml/mtgx}Property"
):
value = prop.find("{http://maltego.paterva.com/xml/mtgx}Value").text or ""
row.append(to_utf8(("%s=%s" % (prop.get("displayName"), value)).strip()))
row.append("Incoming Links=%s" % links.get(node_id, {}).get("in_", 0))
row.append("Outgoing Links=%s" % links.get(node_id, {}).get("out", 0))
csv.writerow(row)
示例3: listChildrenViaPropfind
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def listChildrenViaPropfind():
data = yield self.simpleSend(
"PROPFIND", "/", resultcode=responsecode.MULTI_STATUS,
headers=[('Depth', '1')]
)
tree = XML(data)
seq = [e.text for e in tree.findall("{DAV:}response/{DAV:}href")]
shortest = min(seq, key=len)
seq.remove(shortest)
filtered = [elem[len(shortest):].rstrip("/") for elem in seq]
returnValue(filtered)
示例4: parse_highlights
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def parse_highlights(xml):
soup = XML(xml)
highlightList = []
for series in soup.findall('series'):
tempSeries = dict(series.items())
tempSeries.update(xml_text_elements(series))
highlightList.append(tempSeries)
return highlightList
示例5: listChildrenViaPropfind
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def listChildrenViaPropfind():
request = SimpleStoreRequest(self, "PROPFIND", "/calendars/__uids__/user01/", authid="user01")
request.headers.setHeader("depth", "1")
response = yield self.send(request)
response = IResponse(response)
data = yield allDataFromStream(response.stream)
tree = XML(data)
seq = [e.text for e in tree.findall("{DAV:}response/{DAV:}href")]
shortest = min(seq, key=len)
seq.remove(shortest)
filtered = [elem[len(shortest):].rstrip("/") for elem in seq]
returnValue(filtered)
示例6: mtgx2csv
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def mtgx2csv(opts):
zipfile = ZipFile(opts.graph)
graphs = filter(lambda x: x.endswith('.graphml'), zipfile.namelist())
for f in graphs:
filename = '%s_%s' % (opts.graph.replace('.', '_', 1), os.path.basename(f).replace('.graphml', '.csv', 1))
print 'Writing data from %s/%s to %s...' % (opts.graph, f, filename)
with open(filename, 'wb') as csvfile:
csv = writer(csvfile)
xml = XML(zipfile.open(f).read())
links = {}
for edge in xml.findall('{http://graphml.graphdrawing.org/xmlns}graph/'
'{http://graphml.graphdrawing.org/xmlns}edge'):
src = edge.get('source')
dst = edge.get('target')
if src not in links:
links[src] = dict(in_=0, out=0)
if dst not in links:
links[dst] = dict(in_=0, out=0)
links[src]['out'] += 1
links[dst]['in_'] += 1
for node in xml.findall('{http://graphml.graphdrawing.org/xmlns}graph/'
'{http://graphml.graphdrawing.org/xmlns}node'):
node_id = node.get('id')
node = node.find('{http://graphml.graphdrawing.org/xmlns}data/'
'{http://maltego.paterva.com/xml/mtgx}MaltegoEntity')
row = [to_utf8(('Entity Type=%s' % node.get('type')).strip())]
for prop in node.findall('{http://maltego.paterva.com/xml/mtgx}Properties/'
'{http://maltego.paterva.com/xml/mtgx}Property'):
value = prop.find('{http://maltego.paterva.com/xml/mtgx}Value').text or ''
row.append(to_utf8(('%s=%s' % (prop.get('displayName'), value)).strip()))
row.append('Incoming Links=%s' % links.get(node_id, {}).get('in_', 0))
row.append('Outgoing Links=%s' % links.get(node_id, {}).get('out', 0))
csv.writerow(row)
示例7: run
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def run(args):
opts = parse_args(args)
zipfile = ZipFile(opts.graph)
graphs = filter(lambda x: x.endswith('.graphml'), zipfile.namelist())
for f in graphs:
with open(f.split('/')[1].split('.')[0] + '.csv', 'wb') as csvfile:
csv = writer(csvfile)
xml = XML(zipfile.open(f).read())
links = {}
for edge in xml.findall('{http://graphml.graphdrawing.org/xmlns}graph/'
'{http://graphml.graphdrawing.org/xmlns}edge'):
src = edge.get('source')
dst = edge.get('target')
if src not in links:
links[src] = dict(in_=0, out=0)
if dst not in links:
links[dst] = dict(in_=0, out=0)
links[src]['out'] += 1
links[dst]['in_'] += 1
for node in xml.findall('{http://graphml.graphdrawing.org/xmlns}graph/'
'{http://graphml.graphdrawing.org/xmlns}node'):
node_id = node.get('id')
node = node.find('{http://graphml.graphdrawing.org/xmlns}data/'
'{http://maltego.paterva.com/xml/mtgx}MaltegoEntity')
row = [to_utf8(('Entity Type=%s' % node.get('type')).strip())]
for prop in node.findall('{http://maltego.paterva.com/xml/mtgx}Properties/'
'{http://maltego.paterva.com/xml/mtgx}Property'):
value = prop.find('{http://maltego.paterva.com/xml/mtgx}Value').text or ''
row.append(to_utf8(('%s=%s' % (prop.get('displayName'), value)).strip()))
row.append('Incoming Links=%s' % links[node_id]['in_'])
row.append('Outgoing Links=%s' % links[node_id]['out'])
csv.writerow(row)
示例8: run
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def run(args):
opts = parse_args(args)
zip = ZipFile(opts.graph)
graphs = filter(lambda x: x.endswith('.graphml'), zip.namelist())
for f in graphs:
csv = open(f.split('/')[1].split('.')[0] + '.csv', 'w')
xml = XML(zip.open(f).read())
for e in xml.findall('{http://graphml.graphdrawing.org/xmlns}graph/{http://graphml.graphdrawing.org/xmlns}node/{http://graphml.graphdrawing.org/xmlns}data/{http://maltego.paterva.com/xml/mtgx}MaltegoEntity'):
csv.write(('"Entity Type=%s",' % e.get('type')).strip())
for prop in e.findall('{http://maltego.paterva.com/xml/mtgx}Properties/{http://maltego.paterva.com/xml/mtgx}Property'):
value = prop.find('{http://maltego.paterva.com/xml/mtgx}Value').text or ''
if '"' in value:
value.replace('"', '""')
csv.write(('"%s=%s",' % (prop.get('displayName'), value)).strip())
csv.write('\n')
示例9: run
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def run(args):
opts = parse_args(args)
if path.exists(opts.outfile) and not opts.append and not \
parse_bool('%s already exists. Are you sure you want to overwrite it? [y/N]: ' % repr(opts.outfile),
default='n'):
exit(-1)
entity_source = None
if opts.mtz_file is None:
d = detect_settings_dir()
if maltego_version(d) >= '3.4.0':
print("""
=========================== ERROR: NOT SUPPORTED ===========================
Starting from Maltego v3.4.0 the 'canari generate-entities' command can no
longer generate entity definition files from the Maltego configuration
directory. Entities can only be generated from export files (*.mtz). To
export entities navigate to the 'Manage' tab in Maltego, then click on the
'Export Entities' button and follow the prompts. Once the entities have
been exported, run the following command:
shell> canari generate-entities -m myentities.mtz
=========================== ERROR: NOT SUPPORTED ===========================
""")
exit(-1)
entity_source = DirFile(
path.join(d, 'config', 'Maltego', 'Entities')
)
else:
entity_source = ZipFile(opts.mtz_file)
entity_files = filter(lambda x: x.endswith('.entity'), entity_source.namelist())
namespaces = dict()
excluded_entities = []
if opts.append:
existing_entities = get_existing_entities(opts.outfile)
# excluded_entities.extend([e._type_ for e in existing_entities])
for entity_class in existing_entities:
excluded_entities.extend(entity_class._type_)
if entity_class._type_.endswith('Entity'):
namespaces[entity_class._namespace_] = entity_class.__name__
print 'Generating %s...' % repr(opts.outfile)
outfile = open(opts.outfile, 'ab' if opts.append else 'wb')
if opts.append:
outfile.write('\n\n')
else:
outfile.write('#!/usr/bin/env python\n\nfrom canari.maltego.entities import EntityField, Entity\n\n\n')
for entity_file in entity_files:
xml = XML(entity_source.open(entity_file).read())
id_ = xml.get('id')
if (opts.entity and id_ not in opts.entity) or id_ in excluded_entities:
continue
namespace_entity = id_.split('.')
base_classname = None
namespace = '.'.join(namespace_entity[:-1])
name = namespace_entity[-1]
classname = name
if (opts.namespace and namespace not in opts.namespace) or namespace in opts.exclude_namespace:
continue
if namespace not in namespaces:
base_classname = '%sEntity' % (''.join([n.title() for n in namespace_entity[:-1]]))
namespaces[namespace] = base_classname
outfile.write('class %s(Entity):\n _namespace_ = %s\n\n' % (base_classname, repr(namespace)))
else:
base_classname = namespaces[namespace]
for field in xml.findall('Properties/Fields/Field'):
fields = [
'name=%s' % repr(field.get('name')),
'propname=%s' % repr(normalize_fn(field.get('name'))),
'displayname=%s' % repr(field.get('displayName'))
]
outfile.write('@EntityField(%s)\n' % ', '.join(fields))
outfile.write('class %s(%s):\n pass\n\n\n' % (classname, base_classname))
outfile.close()
print 'done.'
示例10: XML
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
for e in entities:
xml = XML(zip.open(e).read())
id_ = xml.get('id')
ens = id_.split('.')
base_classname = None
namespace = '.'.join(ens[:-1])
name = ens[-1]
classname = name
if namespace not in nses:
base_classname = '%sEntity' % (''.join([ n.title() for n in ens[:-1] ]))
nses[namespace] = base_classname
print 'class %s(Entity):\n namespace = %s\n\n' % (base_classname, repr(namespace))
else:
base_classname = nses[namespace]
for f in xml.findall('Properties/Fields/Field'):
fields = [
'name=%s' % repr(f.get('name')),
'propname=%s' % repr(normalize_fn(f.get('name'))),
'displayname=%s' % repr(f.get('displayName'))
]
print '@EntityField(%s)' % ', '.join(fields)
print 'class %s(%s):\n pass\n\n' % (classname, base_classname)
示例11: NmapReportParser
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
class NmapReportParser(object):
def __init__(self, output):
self.output = output
self.xml = XML(output)
def os(self, address):
host = self._host(address)
if host is not None:
r = {
'osmatch': [osm.attrib for osm in host.findall('os/osmatch')],
'osclass': [osm.attrib for osm in host.findall('os/osclass')],
'portused': host.find('os/portused').attrib
}
return r
return { 'osmatch' : [], 'osclass' : [], 'portused' : {} }
@property
def addresses(self):
return [ a.get('addr') for a in self.xml.findall('host/address') if a.get('addrtype') == 'ipv4' ]
@property
def report(self):
return self.output
def mac(self, address):
host = self._host(address)
if host is not None:
for addr in host.findall('address'):
if addr.get('addrtype') == 'mac':
return addr.get('addr')
return None
def _host(self, address):
for host in self.xml.findall('host'):
for addr in host.findall('address'):
if addr.get('addr') == address:
return host
return None
def ports(self, address):
host = self._host(address)
ports = []
if host is not None:
for p in host.findall('ports/port'):
r = p.attrib
map(lambda x: r.update(x.attrib), p.getchildren())
ports.append(r)
return ports
@property
def scaninfo(self):
return self.xml.find('scaninfo').attrib
@property
def verbosity(self):
return self.xml.find('verbose').get('level')
@property
def debugging(self):
return self.xml.find('debugging').get('level')
def hostnames(self, address):
host = self._host(address)
if host is not None:
return [ hn.attrib for hn in host.findall('hostnames/hostname') ]
return []
def times(self, address):
host = self._host(address)
if host is not None:
return host.find('times').attrib
return {}
@property
def runstats(self):
rs = {}
map(lambda x: rs.update(x.attrib), self.xml.find('runstats').getchildren())
return rs
def scanstats(self, address):
host = self._host(address)
if host is not None:
return host.attrib
return {}
def status(self, address):
host = self._host(address)
if host is not None:
return host.find('status').attrib
return {}
@property
def nmaprun(self):
return self.xml.attrib
def tobanner(self, port):
banner = port.get('product', 'Unknown')
version = port.get('version')
if version is not None:
#.........这里部分代码省略.........
示例12: run
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import findall [as 别名]
def run(args):
opts = parse_args(args)
if path.exists(opts.outfile) and not opts.append and not \
parse_bool('%s already exists. Are you sure you want to overwrite it? [y/N]: ' % repr(opts.outfile), default='n'):
exit(-1)
ar = DirFile(
path.join(detect_settings_dir(), 'config', 'Maltego', 'Entities')
) if opts.mtz_file is None else ZipFile(opts.mtz_file)
entities = filter(lambda x: x.endswith('.entity'), ar.namelist())
nses = dict()
el = []
if opts.append:
l = diff(opts.outfile)
el.extend([i.type for i in l])
for i in l:
if i.type.endswith('Entity'):
nses[i.namespace] = i.__class__.__name__
print 'Generating %s...' % repr(opts.outfile)
fd = open(opts.outfile, 'ab' if opts.append else 'wb')
if opts.append:
fd.write('\n\n')
else:
fd.write('#!/usr/bin/env python\n\nfrom canari.maltego.entities import EntityField, Entity\n\n\n')
for e in entities:
xml = XML(ar.open(e).read())
id_ = xml.get('id')
if (opts.entity and id_ not in opts.entity) or id_ in el:
continue
ens = id_.split('.')
base_classname = None
namespace = '.'.join(ens[:-1])
name = ens[-1]
classname = name
if (opts.namespace and namespace not in opts.namespace) or namespace in opts.exclude_namespace:
continue
if namespace not in nses:
base_classname = '%sEntity' % (''.join([ n.title() for n in ens[:-1] ]))
nses[namespace] = base_classname
fd.write('class %s(Entity):\n namespace = %s\n\n' % (base_classname, repr(namespace)))
else:
base_classname = nses[namespace]
for f in xml.findall('Properties/Fields/Field'):
fields = [
'name=%s' % repr(f.get('name')),
'propname=%s' % repr(normalize_fn(f.get('name'))),
'displayname=%s' % repr(f.get('displayName'))
]
fd.write('@EntityField(%s)\n' % ', '.join(fields))
fd.write('class %s(%s):\n pass\n\n\n' % (classname, base_classname))
fd.close()
print 'done.'