Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Complete Guide to Shodan
#1
I am going through this Book "Complete Guide to Shodan" and there is this code but it doesn't work

import shodan api = shodan.Shodan('myapikeyhere') # Wrap the request in a try/ except block to catch errors try: results = api.search('apache') print('Results found: %s' % results['total']) for result in results['matches']: print('IP: %s' % result['ip_str']) print(result['data']) print('') except shodan.APIError, e: print('Error: %s' % e)
The error I am getting

 except shodan.APIError, e: ^^^^^^^^^^^^^^^^^^ SyntaxError: multiple exception types must be parenthesized
Reply
#2
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}')
Reply
#3
(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)
Reply
#4
(Jul-16-2022, 07:08 PM)Calli Wrote: I'm getting this error what could be the reason
See if you have named a script shodan.py and rename or delete it.
>>> import shodan >>> >>> dir(shodan) ['APIError', 'Shodan', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'client', 'exception', 'helpers', 'stream'] >>> shodan.Shodan('myapikeyhere') <shodan.client.Shodan object at 0x000001E23C9BA8C0> >>> shodan.Shodan.Dns <class 'shodan.client.Shodan.Dns'>
Reply
#5
(Jul-16-2022, 07:35 PM)snippsat Wrote:
(Jul-16-2022, 07:08 PM)Calli Wrote: I'm getting this error what could be the reason
See if you have named a script shodan.py and rename or delete it.
>>> import shodan >>> >>> dir(shodan) ['APIError', 'Shodan', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'client', 'exception', 'helpers', 'stream'] >>> shodan.Shodan('myapikeyhere') <shodan.client.Shodan object at 0x000001E23C9BA8C0> >>> shodan.Shodan.Dns <class 'shodan.client.Shodan.Dns'>

My output

>>> import shodan >>> dir(shodan) ['APIError', 'Shodan', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'client', 'exception', 'helpers', 'stream'] >>> shodan.Shodan('myapikeyhere') <shodan.client.Shodan object at 0x7fed4f96bd60> >>> shodan.Shodan.Dns <class 'shodan.client.Shodan.Dns'> >>> 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please guide me to complete the attendance project pbkurd 3 737 Nov-14-2025, 03:33 PM
Last Post: Larz60+
  New User online Guide NAP1947 1 2,523 Sep-06-2020, 04:36 PM
Last Post: Larz60+
  "Up to but not including" (My personal guide on slicing & indexing) Drone4four 5 7,733 Nov-20-2019, 09:38 PM
Last Post: newbieAuggie2019
  Looking for a Guide Through the Labyrinth stephenmolnar 1 3,012 Jan-23-2019, 08:22 PM
Last Post: Larz60+
  [split] Python Guide / Document? kumatul 2 4,119 Jan-16-2019, 12:49 AM
Last Post: micseydel
  Python Guide / Document? Qui_Ten 1 3,789 Jan-13-2019, 07:50 AM
Last Post: Gribouillis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.