python - 在尝试提取日志的python脚本中获取“ KeyError:'pagination'”
问题描述
我正在尝试使用 API 和 python 获取日志。但是,在运行脚本文件时,出现以下错误:
Unexpected error processing audit event data. Exception: 'pagination'
Traceback (most recent call last):
File "audit_collection.py", line 88, in get_audit_logs
if 'next' in response_body['meta']['pagination']['next_link']:
KeyError: 'pagination'
我正在使用的代码是:
if 'next' in response_body['meta']['pagination']['next_link']:
logging.debug('Saving pagination checkpoint')
with open(os.path.join(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
'checkpoint_audit_next')), 'w') as ca:
ca.write(response_body['meta']['pagination']['next'])
return True
我对python很陌生,还在学习:-)
谢谢
编辑
if os.path.exists(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint', 'checkpoint_audit_next')):
with open(os.path.join(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
'checkpoint_audit_next')), 'r') as c:
payload['meta']['pagination']['pageToken'] = c.read()
try:
logging.debug('Calling Mimecast: ' + url + ' Request ID: ' + request_id + ' Request Body: ' + str(payload))
response = requests.post(url=url, data=str(payload), headers=headers, timeout=120)
logging.debug('Response code: ' + str(response.status_code) + ' Headers: ' + str(response.headers))
if response.status_code == 429:
logging.warn('Mimecast API rate limit reached, sleeping for 30 seconds')
time.sleep(30)
logging.debug('Calling Mimecast: ' + url + ' Request ID: ' + request_id)
response = requests.post(url=url, data=str(payload), headers=headers, timeout=120)
elif response.status_code != 200:
logging.error('Request to ' + url + ' with , request id: ' + request_id + ' returned with status code: '
+ str(response.status_code) + ', response body: ' + response.text)
return False
else:
try:
response_body = json.loads(response.text)
logging.debug('Setting log file name...')
data_file_name = datetime.datetime.utcnow().strftime('%d%m%Y')
data_file_name = 'audit_log_' + str(data_file_name) + '.log'
with open(os.path.join(data_dir, 'audit', data_file_name), 'a') as al:
for event in response_body['data']:
al.write("date=" + event["eventTime"] + "|mcType=auditLog|user=" + event["user"] +
"|auditType=\"" + event["auditType"] + "\"|eventInfo=\"" + event["eventInfo"] + "\n")
if 'next' in response_body['meta']['pagination']['next_link']:
logging.debug('Saving pagination checkpoint')
with open(os.path.join(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
'checkpoint_audit_next')), 'w') as ca:
ca.write(response_body['meta']['pagination']['next'])
return True
else:
logging.debug('No more pages to collect. Saving end date as start date checkpoint for next run')
with open(os.path.join(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
'checkpoint_audit_start')), 'w') as csd:
csd.write(end)
if os.path.exists(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
'checkpoint_audit_next')):
logging.debug('Cleaning up page token')
os.remove(os.path.join(dirname(dirname(abspath(__file__))), 'checkpoint',
'checkpoint_audit_next'))
return False
except Exception, e:
logging.exception('Unexpected error processing audit event data. Exception: ' + str(e))
return False
except Exception, e:
logging.error('Unexpected error calling API. Exception: ' + str(e))
return False
解决方案
推荐阅读
- openmdao - 是否可以设置初始值以用于优化?
- angular - 从 6.1 更新后,@Input() 在 Angular 9 上具有空值
- codenameone - CN.getProperty("AppArg", null); 的行为;在 iOS 上
- android - 带有定位服务的 Android 应用程序被检测为病毒
- mysql - Amazon RDS 中的数据屏蔽(PostgreSql、MySQL)
- android - ClassNotFoundException:找不到类“android.webkit.TracingController”
- flutter - Flutter Bloc 使用 Timer 重新获取数据
- android - Kotlin 中没有 Internet 警报框
- r - 创建自定义注释函数
- java - 如何使用 java-jwt 从 JWT 令牌获取标头/有效负载的 JSON 字符串