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

  

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'%eachline['relust'])
        else:
            print('读取异常')
if __name__=='__main__':
    rizhi=Read_Rizhi('access.log')
    rizhi.print_eachline(' ')

  对日志解析进行封装,对日志的需求进行了自己的分析,

  python+flask 分分钟完美解析阿里云日志_flask

 学了flask,你能不能吧这个日志给我放到flask 给一个前端的界面去展示呢,答案是没有问题的,对代码进行修改:

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部分代码如下:

from flask import Flask,render_template
from jiexi import Read_Rizhi
app = Flask(__name__)
@app.route('/')
def hello_world():
    rizhi = Read_Rizhi(r'C:\\Users\Administrator\Desktop\\untitled1\access.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部分代码:

<!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>

启动flask模块,

访问:

python+flask 分分钟完美解析阿里云日志_flask_02

 

 这样我们进一步优化就结束了,其实还可以进行优化,

这样还得需要我们进一步去的优化,部分切割还是不完善的。简单的切割,展示完成。十分钟就能实现的一个小功能。