(Jul-16-2022, 06:34 PM)snippsat Wrote: 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:
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}')
I'm getting this error what could be the reason
AttributeError: partially initialized module 'shodan' has no attribute 'Shodan' (most likely due to a circular import). Did you mean: 'shodan'?
I have shodan install
pip install shodan
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: shodan in /usr/local/lib/python3.10/dist-packages/shodan-1.27.0-py3.10.egg (1.27.0)
Requirement already satisfied: XlsxWriter in /usr/lib/python3/dist-packages (from shodan) (3.0.2)
Requirement already satisfied: click in /usr/local/lib/python3.10/dist-packages (from shodan) (7.0)
Requirement already satisfied: click-plugins in /usr/lib/python3/dist-packages (from shodan) (1.1.1)
Requirement already satisfied: colorama in /usr/lib/python3/dist-packages (from shodan) (0.4.5)
Requirement already satisfied: requests>=2.2.1 in /usr/local/lib/python3.10/dist-packages (from shodan) (2.28.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.2.1->shodan) (2.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests>=2.2.1->shodan) (2020.6.20)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.2.1->shodan) (1.24.3)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/lib/python3/dist-packages (from requests>=2.2.1->shodan) (2.0.6)
python3 -m pip install shodan
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: shodan in /usr/local/lib/python3.10/dist-packages/shodan-1.27.0-py3.10.egg (1.27.0)
Requirement already satisfied: XlsxWriter in /usr/lib/python3/dist-packages (from shodan) (3.0.2)
Requirement already satisfied: click in /usr/local/lib/python3.10/dist-packages (from shodan) (7.0)
Requirement already satisfied: click-plugins in /usr/lib/python3/dist-packages (from shodan) (1.1.1)
Requirement already satisfied: colorama in /usr/lib/python3/dist-packages (from shodan) (0.4.5)
Requirement already satisfied: requests>=2.2.1 in /usr/local/lib/python3.10/dist-packages (from shodan) (2.28.0)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/lib/python3/dist-packages (from requests>=2.2.1->shodan) (2.0.6)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests>=2.2.1->shodan) (2020.6.20)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.2.1->shodan) (2.7)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.2.1->shodan) (1.24.3)