【问题标题】:AttributeError: 'str' object has no attribute '_request' for shodan apiAttributeError:“str”对象没有 shodan api 的属性“_request”
【发布时间】:2015-10-31 08:52:16
【问题描述】:
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'

【问题讨论】:

  • 很明显,您传递了一个字符串 - 命令行参数与空格连接 - 作为父级,而 Exploits 类期望具有 _request 参数的其他内容。你应该阅读你的 api 的文档来看看是什么。
  • 我对 Shodan 不熟悉,但看起来你应该使用 api.exploits 而不是 api.Exploits

标签: python api shodan


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您抽出宝贵时间帮助我。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    这里是git repo。在第 79 行,您可以看到此调用发生的位置:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 2021-10-04
      • 2019-12-02
      • 2021-09-25
      • 2014-03-04
      相关资源
      最近更新 更多