Я работаю над вызовом API, который работает, но не совсем так, как я хочу.Я добавил в код цикл batch_idx
for, потому что хочу запустить первые 4 индикатора, затем записать результаты в CSV, затем запустить следующие 4 индикатора, затем записать их в другой CSV и т. Д. До * 1002.* список исчерпан.Приведенный ниже код выводит несколько CSV, но первый из них имеет первые 4, как и должно быть, но второй CSV содержит первые 8 индикаторов вместо желаемых вторых 4, а третий CSV содержит первые 12 индикаторов вместо 3-го набора 4 искоро.Как я могу настроить этот код, чтобы удовлетворить мой желаемый вывод CSV.
import pandas as pd
import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
url = 'https://something.com/api/v2/intelligence/?username='
username = '<username>'
api_key = '<key>'
WAIT_SECONDS = 5
'''ALL OTHERS'''
all_other_indicators =
pd.read_csv('Capture_Indicators_Months_Jan_Feb_2019/initial_all-other_indicators.csv')
all_other_indicators = all_other_indicators[0:20]
#all_other_list = all_other_indicators['value'].tolist()
column1 = list()
column2 = list()
column3 = list()
column4 = list()
column5 = list()
i=0
while i < len(all_other_list):
try:
for batch_idx in range(5): #how many time it breaks up the data(number of batches)
current_anomali_indicators = all_other_indicators[batch_idx*4:(batch_idx+1)*4] #number in each batch
all_other_list = current_anomali_indicators['value'].tolist()
for row in all_other_list:
value_api = url+username+'&api_key='+api_key+'&limit=10000&value='
responses = requests.get(value_api+row, verify=False)
data = json.loads(responses.text)
valuess = data['objects']
for index in valuess:
column1.append(index['value'])
column2.append(index['source'])
column3.append(index['trusted_circle_ids'])
column4.append(index['created_ts'])
column5.append(index['tags'])
all_other__output = pd.DataFrame({'Indicator': column1,'Source': column2,'Trusted Circle': column3,'Created': column4,'Tags': column5})
all_other__output.to_csv('Capture_Indicators_Months_Jan_Feb_2019/First_Seen_All_Others_Alone/all_other_with_others_first_seen_'+str(batch_idx)+'.csv', header=True, index=False)
i += 1
break
except requests.exceptions.ConnectionError:
print("Time to sleep for 5 seconds")
time.sleep(WAIT_SECONDS)
Токовый выход: первый csv:
Indicator,Source,Trusted Circle,Created,Tags
193.215.253.92,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.252Z,"[{'remote_api': True, 'id': '0ze', 'name': 'IP-used-by-necurs-C', 'resource_uri': None}]"
199.231.105.253,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.299Z,"[{'remote_api': True, 'id': '32b', 'name': 'IP-used-by-necurs-C', 'resource_uri': None}]"
52.210.177.133,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.432Z,"[{'remote_api': True, 'id': 'r5c', 'name': 'IP-used-by-simda-C', 'resource_uri': None}]"
199.167.151.143,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.475Z,"[{'remote_api': True, 'id': 'br3', 'name': 'IP-used-by-suppobox-C', 'resource_uri': None}]"
199.167.151.143,Blocklist Brute Force,[146],2015-01-31T01:16:17.770Z,"[{'remote_api': True, 'name': 'Blocklist-Brute-Force-IPs', 'resource_uri': None}]"
токовый выход второй csv:
Indicator,Source,Trusted Circle,Created,Tags
193.215.253.92,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.252Z,"[{'remote_api': True, 'id': '0ze', 'name': 'IP-used-by-necurs-C', 'resource_uri': None}]"
199.231.105.253,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.299Z,"[{'remote_api': True, 'id': '32b', 'name': 'IP-used-by-necurs-C', 'resource_uri': None}]"
52.210.177.133,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.432Z,"[{'remote_api': True, 'id': 'r5c', 'name': 'IP-used-by-simda-C', 'resource_uri': None}]"
199.167.151.143,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.475Z,"[{'remote_api': True, 'id': 'br3', 'name': 'IP-used-by-suppobox-C', 'resource_uri': None}]"
199.167.151.143,Blocklist Brute Force,[146],2015-01-31T01:16:17.770Z,"[{'remote_api': True, 'name': 'Blocklist-Brute-Force-IPs', 'resource_uri': None}]"
88.77.192.110,Snort IP BlockList,[146],2019-01-01T08:53:30.643Z,
88.77.192.110,Talos Intelligence IP Blacklist,[146],2019-01-01T05:38:21.091Z,
88.77.192.110,Blutmagie TOR Nodes,,2019-01-01T00:28:26.492Z,"[{'remote_api': True, 'id': 'v9t', 'name': 'tor', 'resource_uri': None}]"
88.77.192.110,TOR Exit Nodes,[146],2018-12-31T20:43:17.244Z,
195.88.242.31,Botscout BOT IPs,[146],2019-01-01T00:38:25.180Z,
http://hukum.ub.ac.id/Order/Document.zip,URLHaus ,[146],2019-01-22T11:18:30.065Z,
http://hukum.ub.ac.id/Order/Document.zip,URLHaus,,2019-01-01T00:58:59.597Z,"[{'id': '1q5', 'remote_api': True, 'org_id': '2316', 'name': 'URLHaus', 'resource_uri': None}]"
http://easydown.workday360.cn/pubg/union_plugin_a2af16fdafe50c3f0faecce317c46e57_xzq.exe,URLHaus ,[146],2019-01-22T11:19:17.014Z,
Желаемый выход: первый csv
193.215.253.92,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.252Z,"[{'remote_api': True, 'id': '0ze', 'name': 'IP-used-by-necurs-C', 'resource_uri': None}]"
199.231.105.253,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.299Z,"[{'remote_api': True, 'id': '32b', 'name': 'IP-used-by-necurs-C', 'resource_uri': None}]"
52.210.177.133,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.432Z,"[{'remote_api': True, 'id': 'r5c', 'name': 'IP-used-by-simda-C', 'resource_uri': None}]"
199.167.151.143,Bambenek Consulting C&C IP Addresses,[146],2019-01-01T00:23:02.475Z,"[{'remote_api': True, 'id': 'br3', 'name': 'IP-used-by-suppobox-C', 'resource_uri': None}]"
199.167.151.143,Blocklist Brute Force,[146],2015-01-31T01:16:17.770Z,"[{'remote_api': True, 'name': 'Blocklist-Brute-Force-IPs', 'resource_uri': None}]"
второй csv:
88.77.192.110,Snort IP BlockList,[146],2019-01-01T08:53:30.643Z,
88.77.192.110,Talos Intelligence IP Blacklist,[146],2019-01-01T05:38:21.091Z,
88.77.192.110,Blutmagie TOR Nodes,,2019-01-01T00:28:26.492Z,"[{'remote_api': True, 'id': 'v9t', 'name': 'tor', 'resource_uri': None}]"
88.77.192.110,TOR Exit Nodes,[146],2018-12-31T20:43:17.244Z,
195.88.242.31,Botscout BOT IPs,[146],2019-01-01T00:38:25.180Z,
http://hukum.ub.ac.id/Order/Document.zip,URLHaus ,[146],2019-01-22T11:18:30.065Z,
http://hukum.ub.ac.id/Order/Document.zip,URLHaus,,2019-01-01T00:58:59.597Z,"[{'id': '1q5', 'remote_api': True, 'org_id': '2316', 'name': 'URLHaus', 'resource_uri': None}]"
http://easydown.workday360.cn/pubg/union_plugin_a2af16fdafe50c3f0faecce317c46e57_xzq.exe,URLHaus ,[146],2019-01-22T11:19:17.014Z,