2011-04-14 23 views
0
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." 

我得到shodan.api.WebAPIError: No information available for that IP.當它無法找到數據庫中的IP,我怎麼能提出這個異常打印出來,沒有該ip的信息。如何提出此異常

回答

2

你應該首先從包中導入異常:

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') 

但我更喜歡第一個。