. 获取日志的堆栈信息
'''
应用了python提供的traceback模块,traceback.format_exc
import traceback
def process(self,hostname,ssh_func):
"""
执行命令,去获取主板
:return:
"""
info = {'status': True, 'data': None, 'error': None}
try:
content = ssh_func(hostname, 'sudo dmidecode -t1')
data = self.parse(content)
info['data'] = data
except Exception as e:
msg = traceback.format_exc()
logger.log(msg)
info['status'] = False
info['error'] = msg
return info
'''
数据的封装思想
'''
将需要的信息封装成一个字典
class BaseResponse(object):
def __init__(self):
self.status = True
self.data = None
self.error = None
@property
def dict(self):
return self.__dict__
def process():
info = BaseResponse()
try:
info.status = True
info.data = "sfdsdf"
except Exception:
pass
return info.dict
result = process()
print(result) # {'status': True, 'data': 'sfdsdf', 'error': None}
'''