Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 搜索Shodan API时的错误处理_Python_Python 2.7_Shodan - Fatal编程技术网

Python 搜索Shodan API时的错误处理

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

我正在尝试编写一个Python脚本,它将搜索ShodanAPI并返回ID、CVE和描述。由于我的一些搜索结果(例如“java”)没有一个已建立的CVE(或CVE键),我的脚本阻塞了。我知道我需要将搜索包装在try/except错误处理中,但我在搜索web时没有找到任何运气。这是我得到的错误,下面是代码。非常感谢

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

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


看起来您希望使用
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]'))