[英]How can I get the data of this table from HackerRank and filter it by country of origin and score for then exporting it as a csv file?
[英]How can I filter ports by country?
這是我的代碼:
我導入模塊
import shodan
import json
我創造我的鑰匙,
SHODAN_API_KEY = ('xxxxxxxxxxxxxxxxxxxx')
api = shodan.Shodan(SHODAN_API_KEY)
我打開我的 json 文件,
with open('Ports.json', 'r') as f:
Ports_dict = json.load(f)
我遍歷我的字典,
for Port in Ports_dict:
print(Port['Port_name'])
try:
results = api.search(Port['Port_name']) # how can I filter ports by country??
我打印內容。
print('Results found: {}'.format(results['total']))
for result in results['matches']:
print('IP: {}'.format(result['ip_str']))
print(result['data'])
print('')
print ('Country_code: %s' % result['location']['country_code'])
except shodan.APIError as e:
print(' Error: %s' % e)
但是如何按國家/地區過濾端口?
為了過濾結果,您需要使用搜索過濾器。 以下文章解釋了 Shodan 的一般搜索查詢語法:
https://help.shodan.io/the-basics/search-query-fundamentals
以下是所有可用搜索過濾器的列表:
https://beta.shodan.io/search/filters
這是一個充滿示例搜索查詢的頁面:
https://beta.shodan.io/search/examples
在您的情況下,您可能希望使用端口和國家/地區過濾器。 例如,以下搜索查詢返回美國的 MySQL 和 PostgreSQL 服務器:
https://beta.shodan.io/search?query=port%3A3306%2C5432+country%3AUS
我還建議使用 Shodan CLI 下載數據,因為它會為您處理結果分頁:
https://help.shodan.io/guides/how-to-download-data-with-api
如果您需要在 Python 中自己執行此操作,那么您還需要通過提供page
參數或簡單地使用Shodan.search_cursor()
Shodan.search()
(而不是像您在代碼)。 上面的文章還展示了如何使用search_cursor()
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.