how to raise this exception
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:开发者_如何学C %s
Country: %s
City: %s
""" % (host['ip'], host.get('country', None), host.get('city', None))
except WebAPIError:
print "No information available for that IP."
I get shodan.api.WebAPIError: No information available for that IP.
when it cant find the IP in the database, how can I raise this exception to print out that there's no information available for that ip.
You should firstly import the Exception from the package:
from shodan.api import WebAPIError
Then, when you catch the error you can re-raise it with your message:
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
or:
try:
# Here your code
except WebAPIError:
raise WebAPIError('My new message')
But I prefer the first one.
精彩评论