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)
  • 相关阅读:
    使用XmlWriter写入XML
    Xml的一些基本概念(摘抄自w3school.com.cn)
    Basler相机启动问题xml读取出错
    c#开方,平方,sin函数计算
    如果遇到继承控件,添加到新项目里在工具栏找不到的情况下,F5启动一下,重新生成是不会有的,要运行成功才有
    添加项目文件时候不要把引用文件直接放到bin-debug里
    发现三个很好看的控件
    merge into 批量修改语句
    -- oracle上一些查询表和字段语句
    -- oracle上查看储存过程内容
  • 原文地址:https://www.cnblogs.com/zyy98877/p/8811097.html
Copyright © 2011-2022 走看看