Parsing data for xml file in python -


i have following xml file:

<address addr="x.x.x.x" addrtype="ipv4"/> <hostnames> </hostnames> <ports><port protocol="tcp" portid="1"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="tcpmux" method="table" conf="3"/></port> <port protocol="tcp" portid="64623"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="unknown" method="table" conf="3"/></port> </ports> <times srtt="621179" rttvar="35357" to="762607"/> </host> <host starttime="1418707433" endtime="1418707742"><status state="up" reason="syn-ack" reason_ttl="0"/> <address addr="y.y.y.y" addrtype="ipv4"/> <hostnames> </hostnames> <ports><port protocol="tcp" portid="1"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="tcpmux" method="table" conf="3"/></port> <port protocol="tcp" portid="64680"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="unknown" method="table" conf="3"/></port> </ports> <times srtt="834906" rttvar="92971" to="1206790"/> </host> <host starttime="1418707433" endtime="1418707699"><status state="up" reason="syn-ack" reason_ttl="0"/> <address addr="w.w.w.w" addrtype="ipv4"/> <hostnames> </hostnames> <ports><extraports state="filtered" count="997"> <extrareasons reason="no-responses" count="997"/> </extraports> <port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="smtp" method="table" conf="3"/></port> <port protocol="tcp" portid="443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="https" method="table" conf="3"/></port> <port protocol="tcp" portid="7443"><state state="open" reason="syn-ack" reason_ttl="0"/><service name="oracleas-https" method="table" conf="3"/></port> </ports> <times srtt="690288" rttvar="110249" to="1131284"/> </host> 

what tried extracting data each ip is:

import sys import xml.etree.elementtree et input=sys.argv[1]  tree=et.parse(input) root=tree.getroot()  host in root.findall('host'):     updown=host.find('status').get('state')     if updown=='up':         print 'ip address: '+host.find('address').get('addr')         ports=[port.get('portid') port in root.findall('.//port')]         state=[port.get('state') port in root.findall('.//port/state')]         name=[port.get('name') port in root.findall('.//port/service')] 

but returns me information of ips. how can specific information each ip ?

i think should change root.findall don't know how can that.

for me, code seems suspiсious:

        ports=[port.get('portid') port in root.findall('.//port')]         state=[port.get('state') port in root.findall('.//port/state')]         name=[port.get('name') port in root.findall('.//port/service')] 

inside of loop, searching entire root node './/port...' stuff.
seems need this:

        ports=[port.get('portid') port in host.findall('.//port')]         state=[port.get('state') port in host.findall('.//port/state')]         name=[port.get('name') port in host.findall('.//port/service')] 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -