AttributeError: 'str' object has no attribute '_request' for shodan api

AttributeError: 'str' object has no attribute '_request' for shodan api

import shodan
import sys
from ConfigParser import ConfigParser

#grab the api key from auth.ini
config = ConfigParser()
config.read('auth.ini')
SHODAN_API_KEY = config.get('auth','API_KEY')

#initialize the api object
api = shodan.Shodan(SHODAN_API_KEY)\

# Input validation
if len(sys.argv) == 1:
        print 'Usage: %s <search query>' % sys.argv[0]
        sys.exit(1)

try:

        query = ' '.join(sys.argv[1:])
        parent = query
        exploit = api.Exploits(parent)
        #WHY DOESNT THIS WORK 
        #AttributeError: 'str' object has no attribute '_request'
        print exploit.search(query)

except Exception, e:
        print 'Error: %s' % e
        sys.exit(1)

我正在使用 Python 2.7 我得到 AttributeError: 'str' object has no attribute '_request' 回溯错误在 Shodan API 的 client.py 中显示第 79 行,是我的问题还是他们的代码不可靠?

这是回溯

Traceback (most recent call last):
  File "exploitsearch.py", line 26, in <module>
    print exploit.search('query')
  File "/usr/local/lib/python2.7/dist-packages/shodan/client.py", line 79, in search
    return self.parent._request('/api/search', query_args, service='exploits')
AttributeError: 'str' object has no attribute '_request'

parent 变量的类型应为 Shodan。您正在使用 string 变量初始化 Exploits class。这是您导致问题的行 https://github.com/achillean/shodan-python/blob/master/shodan/client.py#L79.

ExploitsShodan 超级class 的子class。这个 class 有一个名为 _request 的方法。当你初始化一个Exploits的实例并执行search方法时,代码在内部调用super(read:Shodan)方法,_request。由于您将字符串类型传递给 class 构造函数,它试图在字符串对象上调用此方法并且(正确地)抱怨该方法不是 str.

的成员

这里是git repo。在第 79 行,你可以看到这个调用发生在哪里:

return self.parent._request('/api/search', query_args, service='exploits')

因此,您可以看到您的 parent 变量应该是 Shodan 的一个实例,或者您的 api 变量。

我是 Shodan 的创始人,也是您正在使用的相关库的作者。正确答案由上面的 John Gordon 提供:

您不需要实例化 Exploits class,它会在您创建 Shodan() 实例时自动为您完成。这意味着您可以直接搜索事物而无需任何额外工作:

    api = shodan.Shodan(YOUR_API_KEY)
    results = api.exploits.search('apache')