Python 搜索Shodan API时的错误处理
我正在尝试编写一个Python脚本,它将搜索ShodanAPI并返回ID、CVE和描述。由于我的一些搜索结果(例如“java”)没有一个已建立的CVE(或CVE键),我的脚本阻塞了。我知道我需要将搜索包装在try/except错误处理中,但我在搜索web时没有找到任何运气。这是我得到的错误,下面是代码。非常感谢 --------错误------- --------我的代码------Python 搜索Shodan API时的错误处理,python,python-2.7,shodan,Python,Python 2.7,Shodan,我正在尝试编写一个Python脚本,它将搜索ShodanAPI并返回ID、CVE和描述。由于我的一些搜索结果(例如“java”)没有一个已建立的CVE(或CVE键),我的脚本阻塞了。我知道我需要将搜索包装在try/except错误处理中,但我在搜索web时没有找到任何运气。这是我得到的错误,下面是代码。非常感谢 --------错误------- --------我的代码------ 看起来您希望使用dict.get,并在不存在密钥的情况下为其提供一个要返回的默认值: print '%s: %s
看起来您希望使用
dict.get
,并在不存在密钥的情况下为其提供一个要返回的默认值:
print '%s: %s: %s' % (exploit.get('id', '[blank]'), exploit.get('cve', '[blank]'), exploit.get('description', '[blank]'))
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'])
print '%s: %s: %s' % (exploit.get('id', '[blank]'), exploit.get('cve', '[blank]'), exploit.get('description', '[blank]'))