zoukankan      html  css  js  c++  java
  • python实战,

    1.把日志状态码为200得请求记录下来
    记录信息(ip,访问时间,请求资源)
    封装函数再次调用,健壮性try except

    #coding=utf-8
    import re
    def  aclog(path,putpath):
        result=""
        with open(path,"r")  as fp:
            lines=fp.readlines()
            for line in lines:
                if "200" in line:
                    
                    ip=re.search(r"(d{1,3}.){3}d{1,3}",line).group()
                    time=re.search(r"[.*]",line).group()
                    url=re.search(r"GET.*(200)",line).group()[:-3]
                    result=ip+time+url+" "
                    with open(putpath,"a+") as fp1:
                        fp1.write(result)
            
    aclog("e:\accesslog.txt","e:\aclog.txt")

    另一种方法:使用split

    #encoding=utf-8
    def AnalysisLog(filenameget,filenameput):
        try:
            with open(filenameget) as f:
                as1=[i.split() for i in f]
            as2=[[i[0],i[3],i[6]] for i in as1 if i[8].startswith("2")]
            with open(filenameput,"w") as f:
                f.writelines(["".join([" ".join(i)," "]) for i in as2])
            return "success!"
        except Exception,e:
            return "failed!",str(e)
     
    if __name__=="__main__":
        filenameget=ur"e:\get.txt"
        filenameput=ur"e.\put.txt"
        print AnalysisLog(filenameget,filenameput)
    在方法二基础上改善
    #encoding=utf-8
    def AnalysisLog(filenameget,filenameput):
        result=""
        try:
            with open(filenameget) as f:
                for line in f.readlines():
                    i=line.split()
                    if "200" in line:
                        result+=i[0]+i[3]+i[6]+" "
                
            
            with open(filenameput,"w") as f:
                f.write(result)
            return "success!"
        except Exception,e:#
            return "failed!",str(e)
     
    if __name__=="__main__":
        filenameget=ur"e:\accesslog.txt"
        filenameput=ur"e:\put.txt"
        print AnalysisLog(filenameget,filenameput)
  • 相关阅读:
    低代码:时代的选择
    AI+云原生,把卫星遥感虐的死去活来
    网络货运平台要智能,安全的数据底座少不了
    基于昇腾CANN的卡通图像生成可在线体验啦!十分钟带你了解CANN应用开发全流程
    什么是强化学习?
    高可用架构演进之单元化
    AOC萌新探索:搭建和体验在线AOC环境
    如何将知识引入机器学习模型提升泛化能力?
    零代码以“王者荣耀”为例解析设计七原则
    从零开始搭建前端脚手架
  • 原文地址:https://www.cnblogs.com/zyy98877/p/8811097.html
Copyright © 2011-2022 走看看