2015-10-30 25 views
4
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」對象沒有屬性「_request」 追蹤誤差表示線79在客戶端在Shodan API中的.py,僅僅是我還是他們的代碼不可靠?AttributeError的:「STR」對象具有用於初段API沒有屬性「_request」

這裏是回溯

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' 
+0

顯然,你傳遞一個字符串 - 用空格加入了命令行參數 - 作爲父母,而漏洞類期待別的東西具有_request參數。你應該閱讀你的api的文檔來看看。 –

+2

我對Shodan不熟悉,但看起來你應該使用'api.exploits'而不是'api.Exploits'。 –

回答

3

我是Shodan的創始人,也是您使用的相關圖書館的作者。上面的John Gordon提供了正確的答案:

您不需要實例化Exploits類,它會在您創建Shodan()實例時自動完成。這意味着你可以直接搜索的東西,沒有任何額外的工作:

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

謝謝您花時間幫助我。 –

+0

@JoshuaHarper請將此標記爲解決方案。 – Tgsmith61591

0

ExploitsShodan超類的子類。該課程有一個名爲_request的方法。在初始化Exploits實例並執行search方法時,代碼在內部調用super(讀取:Shodan)方法_request。由於您將字符串類型傳遞給類構造函數,因此它試圖在字符串對象上調用此方法,並且(正確)抱怨該方法不是str的成員。

這裏是git repo。在79行,你能看到這個呼叫正在發生:

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

因此,你可以看到你的parent變量應該是撒旦,或者您api變量的一個實例。

相關問題