There is some old Python 2 code in there,maybe the book is a little old and not updated.
In Python 3 must do it like this:
In Python 3 must do it like this:
except shodan.APIError as e:Updated with newer string formatting f-string.
import shodan api = shodan.Shodan('myapikeyhere') # Wrap the request in a try/ except block to catch errors try: results = api.search('apache') print(f"Results found: {results['total']}") for result in results['matches']: print(f"IP: {result['ip_str']}") print(result['data']) print('') except shodan.APIError as error: print(f'Error: {error}')