Iterate Python data structure -


i'm having problems getting head around python data structure:

 data = {'nmap': {'command_line': u'ls',                   'scaninfo': {u'tcp': {'method': u'connect',                                         'services': u'80,443'}},                   'scanstats': {'downhosts': u'0',                                 'elapsed': u'1.18',                                 'timestr': u'wed mar 19 21:37:54 2014',                                 'totalhosts': u'1',                                 'uphosts': u'1'}},          'scan': {u'url': {'addresses': {u'ipv6': u'2001:470:0:63::2'},                                         'hostname': u'abc.net',                                         'status': {'reason': u'syn-ack',                                                    'state': u'up'},                                         u'tcp': {80: {'conf': u'3',                                                       'cpe': '',                                                       'extrainfo': '',                                                       'name': u'http',                                                       'product': '',                                                       'reason': u'syn-ack',                                                       'state': u'open',                                                       'version': ''},                                                  443: {'conf': u'3',                                                        'cpe': '',                                                        'extrainfo': '',                                                        'name': u'https',                                                        'product': '',                                                        'reason': u'syn-ack',                                                        'script': {                                                            u'ssl-cert': u'place holder'},                                                        'state': u'open',                                                        'version': ''}},                                         'vendor': {}          }          }  } 

basically need iterate on 'tcp' key values , extract contents of 'script' item if exists.

this i've tried:

items = data["scan"] item in items['url']['tcp']:     if t["script"] not none:         print t   

however can't seem work.

this find dictionary items key 'script' anywhere in data structure:

def find_key(data, search_key, out=none):     """find values nested dictionary given key."""     if out none:         out = []     if isinstance(data, dict):         if search_key in data:             out.append(data[search_key])         key in data:             find_key(data[key], search_key, out)     return out 

for data, get:

>>> find_key(data, 'script') [{'ssl-cert': 'place holder'}] 

to find ports, too, modify slightly:

tcp_dicts = find_key(data, 'tcp') # find values key 'tcp' ports = [] # list hold ports d in tcp_dicts: # iterate through values key 'tcp'     if all(isinstance(port, int) port in d): # ensure port numbers         port in d:             ports.append((port,                            d[port].get('script'))) # extract number , script 

now like:

[(80, none), (443, {'ssl-cert': 'place holder'})] 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -