mirror of
https://github.com/nmap/nmap.git
synced 2025-12-16 12:49:02 +00:00
Performance improvement.
Replaced try/except sequences by a dict.get()/if not None. This simple change improves parsing time by 1s on a ~150MB file.
This commit is contained in:
50
ndiff/ndiff
50
ndiff/ndiff
@@ -1177,9 +1177,8 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
|
|||||||
def _start_status(self, name, attrs):
|
def _start_status(self, name, attrs):
|
||||||
assert self.parent_element() == u"host"
|
assert self.parent_element() == u"host"
|
||||||
assert self.current_host is not None
|
assert self.current_host is not None
|
||||||
try:
|
state = attrs.get(u"state")
|
||||||
state = attrs[u"state"]
|
if state is None:
|
||||||
except KeyError:
|
|
||||||
warn(u"%s element of host %s is missing the \"state\" attribute; assuming \"unknown\"." % (name, self.current_host.format_name()))
|
warn(u"%s element of host %s is missing the \"state\" attribute; assuming \"unknown\"." % (name, self.current_host.format_name()))
|
||||||
return
|
return
|
||||||
self.current_host.state = state
|
self.current_host.state = state
|
||||||
@@ -1187,9 +1186,8 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
|
|||||||
def _start_address(self, name, attrs):
|
def _start_address(self, name, attrs):
|
||||||
assert self.parent_element() == u"host"
|
assert self.parent_element() == u"host"
|
||||||
assert self.current_host is not None
|
assert self.current_host is not None
|
||||||
try:
|
addr = attrs.get(u"addr")
|
||||||
addr = attrs[u"addr"]
|
if addr is None:
|
||||||
except KeyError:
|
|
||||||
warn(u"%s element of host %s is missing the \"addr\" attribute; skipping." % (name, self.current_host.format_name()))
|
warn(u"%s element of host %s is missing the \"addr\" attribute; skipping." % (name, self.current_host.format_name()))
|
||||||
return
|
return
|
||||||
addrtype = attrs.get(u"addrtype", u"ipv4")
|
addrtype = attrs.get(u"addrtype", u"ipv4")
|
||||||
@@ -1198,9 +1196,8 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
|
|||||||
def _start_hostname(self, name, attrs):
|
def _start_hostname(self, name, attrs):
|
||||||
assert self.parent_element() == u"hostnames"
|
assert self.parent_element() == u"hostnames"
|
||||||
assert self.current_host is not None
|
assert self.current_host is not None
|
||||||
try:
|
hostname = attrs.get(u"name")
|
||||||
hostname = attrs[u"name"]
|
if hostname is None:
|
||||||
except KeyError:
|
|
||||||
warn(u"%s element of host %s is missing the \"name\" attribute; skipping." % (name, self.current_host.format_name()))
|
warn(u"%s element of host %s is missing the \"name\" attribute; skipping." % (name, self.current_host.format_name()))
|
||||||
return
|
return
|
||||||
self.current_host.add_hostname(hostname)
|
self.current_host.add_hostname(hostname)
|
||||||
@@ -1208,18 +1205,20 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
|
|||||||
def _start_extraports(self, name, attrs):
|
def _start_extraports(self, name, attrs):
|
||||||
assert self.parent_element() == u"ports"
|
assert self.parent_element() == u"ports"
|
||||||
assert self.current_host is not None
|
assert self.current_host is not None
|
||||||
try:
|
state = attrs.get(u"state")
|
||||||
state = attrs[u"state"]
|
if state is None:
|
||||||
except KeyError:
|
|
||||||
warn(u"%s element of host %s is missing the \"state\" attribute; assuming \"unknown\"." % (name, self.current_host.format_name()))
|
warn(u"%s element of host %s is missing the \"state\" attribute; assuming \"unknown\"." % (name, self.current_host.format_name()))
|
||||||
state = None
|
state = None
|
||||||
if state in self.current_host.extraports:
|
if state in self.current_host.extraports:
|
||||||
warn(u"Duplicate extraports state \"%s\" in host %s." % (state, self.current_host.format_name()))
|
warn(u"Duplicate extraports state \"%s\" in host %s." % (state, self.current_host.format_name()))
|
||||||
try:
|
|
||||||
count = int(attrs[u"count"])
|
count = attrs.get(u"count")
|
||||||
except KeyError:
|
if count is None:
|
||||||
warn(u"%s element of host %s is missing the \"count\" attribute; assuming 0." % (name, self.current_host.format_name()))
|
warn(u"%s element of host %s is missing the \"count\" attribute; assuming 0." % (name, self.current_host.format_name()))
|
||||||
count = 0
|
count = 0
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
count = int(count)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
warn(u"Can't convert extraports count \"%s\" to an integer in host %s; assuming 0." % (attrs[u"count"], self.current_host.format_name()))
|
warn(u"Can't convert extraports count \"%s\" to an integer in host %s; assuming 0." % (attrs[u"count"], self.current_host.format_name()))
|
||||||
count = 0
|
count = 0
|
||||||
@@ -1228,9 +1227,8 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
|
|||||||
def _start_port(self, name, attrs):
|
def _start_port(self, name, attrs):
|
||||||
assert self.parent_element() == u"ports"
|
assert self.parent_element() == u"ports"
|
||||||
assert self.current_host is not None
|
assert self.current_host is not None
|
||||||
try:
|
portid_str = attrs.get(u"portid")
|
||||||
portid_str = attrs[u"portid"]
|
if portid_str is None:
|
||||||
except KeyError:
|
|
||||||
warn(u"%s element of host %s missing the \"portid\" attribute; skipping." % (name, self.current_host.format_name()))
|
warn(u"%s element of host %s missing the \"portid\" attribute; skipping." % (name, self.current_host.format_name()))
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
@@ -1238,9 +1236,8 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
warn(u"Can't convert portid \"%s\" to an integer in host %s; skipping port." % (portid_str, self.current_host.format_name()))
|
warn(u"Can't convert portid \"%s\" to an integer in host %s; skipping port." % (portid_str, self.current_host.format_name()))
|
||||||
return
|
return
|
||||||
try:
|
protocol = attrs.get(u"protocol")
|
||||||
protocol = attrs[u"protocol"]
|
if protocol is None:
|
||||||
except KeyError:
|
|
||||||
warn(u"%s element of host %s missing the \"protocol\" attribute; skipping." % (name, self.current_host.format_name()))
|
warn(u"%s element of host %s missing the \"protocol\" attribute; skipping." % (name, self.current_host.format_name()))
|
||||||
return
|
return
|
||||||
self.current_port = Port((portid, protocol))
|
self.current_port = Port((portid, protocol))
|
||||||
@@ -1269,14 +1266,13 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
|
|||||||
|
|
||||||
def _start_script(self, name, attrs):
|
def _start_script(self, name, attrs):
|
||||||
result = ScriptResult()
|
result = ScriptResult()
|
||||||
try:
|
result.id = attrs.get(u"id")
|
||||||
result.id = attrs[u"id"]
|
if result.id is None:
|
||||||
except KeyError:
|
|
||||||
warn(u"%s element missing the \"id\" attribute; skipping." % name)
|
warn(u"%s element missing the \"id\" attribute; skipping." % name)
|
||||||
return
|
return
|
||||||
try:
|
|
||||||
result.output = attrs[u"output"]
|
result.output = attrs.get(u"output")
|
||||||
except KeyError:
|
if result.output is None:
|
||||||
warn(u"%s element missing the \"output\" attribute; skipping." % name)
|
warn(u"%s element missing the \"output\" attribute; skipping." % name)
|
||||||
return
|
return
|
||||||
if self.parent_element() == u"prescript":
|
if self.parent_element() == u"prescript":
|
||||||
|
|||||||
Reference in New Issue
Block a user