0

I want to list once my script in python search for specific strings , but I also want to add country code first two letters , but when I try then it says invalid KeyError: 'country_code', but the api says ocation.country_code how can I achieve that?

 #!/usr/bin/python
import shodan
SHODAN_API_KEY="xxxxxxxxxxxxxxxxxxxx"
api = shodan.Shodan(SHODAN_API_KEY)

try:
    # Search Shodan
    results = api.search('ProFTPd-1.3.3c')

    # Show the results
    for result in results['matches']:
        print '%s' % result['ip_str']
        print '%s' % result['country_code']

except shodan.APIError, e:
        print 'Error: %s' % e
Store LTE
  • 11
  • 6

1 Answers1

2

I think this is the method You are using in Python https://github.com/achillean/shodan-python/blob/master/shodan/client.py#L324 and it triggers:

return self._request('/shodan/host/search', args)

Shodan API documentation: https://developer.shodan.io/api check out /shodan/host/search API

I just saw that the answer is in Your question but You ate one letter from location (ocation).

Try this:

print '%s' % result['location']['country_code']

So field You are looking for is there but it is in another dictionary.

I would recommend to read API documentation well next time and as Nofal Daud said, Python error are self explanatory if You have KeyError on dict it means that field is not there. Next time listen to Python it will reveal the truth.

Igor
  • 1,384
  • 3
  • 17
  • 34
  • thanks, this is what I was looking for. I thought it was location.country_code or something like this, but thanks – Store LTE Feb 17 '18 at 19:37