Skip to content Skip to sidebar Skip to footer

Attributeerror: 'unicode' Object Has No Attribute 'key'

I'm very new to Python coding and have run into an issue while trying to upgrade some code. I'm working with an app that pulls data via an API from stored data from a scan. here is

Solution 1:

Well after further efforts I was able to figure out what I needed to do with the code. It was much easier than I thought it would be but sometime when learning a new language its hard to envision what is needed. Code posted below.

 def _collect_one_host_scan_info(self, host_id, sid, scan_info):
     """
     The method to collect all the vulnerabilities of one host and generate 
     the event data.
     """
     count = 0
     host_uri = self.endpoint + '/' + str(sid) + '/hosts/' + str(host_id)
     result = self.client.request(host_uri).get("content")
     # if there is exception in request, return None
     if result is None:
         _LOGGER.info("There is exception in request, return None")
         return None
     else:
         host_info = result.get("info", {})
         host_end_time = host_info.get("host_end", "")
         if self.ckpt.is_new_host_scan(host_end_time,
                                       self.config.get("start_date")):
             self.source = self.url + self.endpoint + '/' + str(
                 sid) + '/hosts/' + str(host_id)
             for vuln in result.get("vulnerabilities", []):
                 vuln["sid"] = sid
                 vuln["host_id"] = host_id
                 plugin_id = vuln.get("plugin_id", "")

                 # get plugin_output data
                 plugin_output_info = []
                 if plugin_id:
                     plugin_uri = "{}/plugins/{}".format(host_uri,
                                                         plugin_id)
                     plugin_outputs = self.client.request(plugin_uri).get(
                         "content", {}).get("outputs", [])
                     data_output = []
                     for output in plugin_outputs:
                         items = output.get("plugin_output", 'no value')
                         item = str(items)
                         #clean = re.sub('[^a-zA-Z0-9-()_*.(:\\)]', ' ', item)
                         plugin_output_info.append(item)

                 # get the port info        
                 port_info = []
                 if plugin_id:
                     plugin_uri = "{}/plugins/{}".format(host_uri,
                                                         plugin_id)
                     plugin_outputs = self.client.request(plugin_uri).get(
                         "content", {}).get("outputs", [])
                     ports = []
                     for output in plugin_outputs:
                         ports.extend(output.get("ports", {}).keys())
                     for port in ports:
                         port_elem = {}
                         port_items = re.split(r"\s*/\s*", port)
                         port_elem["port"] = int(port_items[0])
                         if port_items[1]:
                             port_elem["transport"] = port_items[1]
                         if port_items[2]:
                             port_elem["protocol"] = port_items[2]
                         port_info.append(port_elem)

                 vuln = dict(vuln, **scan_info)
                 vuln = dict(vuln, **host_info)
                 if port_info:
                     vuln["ports"] = port_info
                 if plugin_output_info:
                     vuln["plugin_output"] = plugin_output_info
                 entry = NessusObject(
                     vuln.get("timestamp"), self.sourcetype, self.source,
                     vuln)
                 self._print_stream(entry)
                 count += 1
     return count

Post a Comment for "Attributeerror: 'unicode' Object Has No Attribute 'key'"