2013-03-07 56 views
0

我想写一个Python脚本,将搜索Shodan API并返回ID,CVE和说明。由于我的一些搜索结果(例如'java')没有建立CVE(或CVE键),我的脚本扼杀了。我知道我需要在try/except错误处理中包装搜索,但是我一直没有找到能够找到网络研究的运气。这是我得到的错误,下面是代码。首先十分感谢。错误处理当Shodan API

-------- -------错误

print '%s: %s: %s' % (exploit['id'], exploit['cve'], exploit['description']) 
KeyError: 'cve 

--------我的代码------

from shodan import WebAPI 
api = WebAPI("my shodan key") 

user_selection = raw_input("Enter something: ") 
print "searching for", (user_selection),"....." 

results = api.exploitdb.search(user_selection) 

for exploit in results['matches']: 
    print '%s: %s: %s' % (exploit['id'], exploit['cve'], exploit['description']) 

回答

0

它看起来像你想要使用dict.get并提供一个默认值,在密钥不存在时返回:

print '%s: %s: %s' % (exploit.get('id', '[blank]'), exploit.get('cve', '[blank]'), exploit.get('description', '[blank]'))