开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜