python+flask解析服务器日志

2021-03-15 15:11:59 浏览数 (1)

拿到了自己阿里云服务器的日志,对其需要进行处理

代码语言:javascript复制
class Read_Rizhi:
def __init__(self,filename):
self.filename=filename
def open_file(self):
try:
f = open(self.filename, 'r', encoding='utf-8')
resuly = {'code': 1, 'result': f}
except Exception as e:
resuly = {'code': 0, 'result': e}
return resuly
def read_line(self):
result=self.open_file()
if result['code']==0:
return  {'read':'fail','relust':result['result']}
elif result['code']==1:
return {'read':'pass','relust':result['result'].readlines()}
else:
return {'read':'error','relust':'未知错误'}
def print_eachline(self,splist:str):
eachline=self.read_line()
if eachline['read']=='pass':
for rizhi in eachline['relust']:
ri=rizhi.split(splist)
print('请求ip:', ri[0])
print('请求时间磋:', ri[3])
print('请求方式:', ri[5])
print('请求路径:', ri[6])
print('请求协议:', ri[7])
print('返回状态吗:', ri[8])
elif eachline['read']=='fail':
print('读取失败!原因:%s'�chline['relust'])
else:
print('读取异常')
if __name__=='__main__':
rizhi=Read_Rizhi('access.log')
rizhi.print_eachline(' ')

展示结果:

能不能用flask给前端展示呢,其实这样也是可以的,对代码进行优化

代码语言:javascript复制
class Read_Rizhi:
def __init__(self,filename):
self.filename=filename
def open_file(self):
try:
f = open(self.filename, 'r', encoding='utf-8')
resuly = {'code': 1, 'result': f}
except Exception as e:
resuly = {'code': 0, 'result': e}
return resuly
def read_line(self):
result=self.open_file()
if result['code']==0:
return  {'read':'fail','relust':result['result']}
elif result['code']==1:
return {'read':'pass','relust':result['result'].readlines()}
else:
return {'read':'error','relust':'未知错误'}
def print_eachline(self,splist:str):
eachline=self.read_line()
if eachline['read']=='pass':
ip_list=[]
for rizhi in eachline['relust']:
ri=rizhi.split(splist)
ip_list.append({'ip':ri[0],'time':ri[3],
'meth':ri[5],'path':ri[6],'xieyi':ri[7],
'code':ri[8]})
relust={'code':1,'result':ip_list}
elif eachline['read']=='fail':
relust = {'code':2, 'result':eachline['relust']}
else:
relust = {'code': 3, 'result':'读取异常'}
return relust

flask部分代码

代码语言:javascript复制
from flask import Flask,render_templatefrom jiexi import Read_Rizhi
app = Flask(__name__)
@app.route('/')

def hello_world():
   rizhi = Read_Rizhi(r'C:\UsersAdministratorDesktop\untitled1access.log')
   relust = rizhi.print_eachline(' ')   
    if relust['code'] == 1:
       f_list = relust['result']    
    return render_template('rizi.html',f_lis=f_list)

if __name__ == '__main__':
   app.run()

rizi.html代码:

代码语言:javascript复制
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>阿里云日志分析</title></head><body><h1 style="text-align: center">
阿里云日志分析</h1><table style="width: 60%;margin-top: 40px" border="1">
<tbody>
<tr>
<td>ip</td>
<td>时间</td>
<td>请求方式</td>
<td>请求路径</td>
<td>协议</td>
<td>状态码</td>
</tr>
   {% for item in f_lis%}    <tr>
<td>{{ item.ip }}</td>
<td>{{ item.time }}</td>
<td>{{ item.meth }}</td>
<td>{{ item.path }}</td>
<td>{{ item.xieyi }}</td>
<td>{{ item.code }}</td>
</tr>
   {% endfor %}    </tbody></table></body></html>

启动后访问:

python解析日志就是这么简单,十分钟就能解决。配合flask,让你的日志展示的高大上,

0 人点赞