from shodan import WebAPI
SHODAN_API_KEY = "MY API KEY"
api = WebAPI(SHODAN_API_KEY)
host = api.host('98.111.2.190')
# Print general info
try:
print """
IP: %s
Country: %s
City: %s
""" % (host['ip'], host.get('country', None), host.get('city', None))
except WebAPIError:
print "No information available for that IP."
当我无法在数据库中找到IP时,我得到shodan.api.WebAPIError: No information available for that IP.
,如何提出此异常以打印出该ip没有可用的信息。
答案 0 :(得分:2)
首先应该从包中导入Exception:
from shodan.api import WebAPIError
然后,当您发现错误时,可以使用您的消息重新提出错误:
try:
# Here your code
except WebAPIError as e:
e.args = ('My new message',) # Remember the comma! It is a tuple
raise # Re-raise the exception
或:
try:
# Here your code
except WebAPIError:
raise WebAPIError('My new message')
但我更喜欢第一个。