zoukankan      html  css  js  c++  java
  • python分析apahce网站日志的例子

    有关python实现apahce网站日志分析的方法。

    应用到:shell与python数据交互、数据抓取,编码转换

    #coding:utf-8

    #!/usr/bin/python
    '''
    程序说明:apache access.log日志分析
    分析访问网站IP 来源情况
    日期:2014-01-06 17:01
    author:gyh9711

    程序说明:应用到:shell与python数据交互、数据抓取,编码转换
    '''
    import os
    import json
    import httplib
    import codecs
    LogFile='/var/log/apache2/access.log'
    #日志
    logMess='/tmp/acc.log'
    if os.path.isfile(logMess):
    os.system('cp /dev/null %s'% logMess)
    file=codecs.open(logMess,'w+',encoding='utf-8')
    def cmd(cmd):
    return os.popen(cmd).readlines()
    '''
    def getIp(ip):
    return json.loads(os.popen("/usr/bin/curl http://ip.taobao.com/service/getIpInfo.php?ip=%s" % ip).readline())['data']
    '''
    conn = httplib.HTTPConnection('ip.taobao.com')
    def getIpCountry(ip): www.jbxue.com
    conn.request('GET','/service/getIpInfo.php?ip=%s' % ip)
    r1=conn.getresponse()
    if r1.status == 200:
    return json.loads(r1.read())['data']
    else:
    return "Error"
    #将access.log文件进行分析,并转为python数组
    file.write(u"字段说明:ip 访问次数据 ip国家 城市的 isp号 省份 所在地区 ")
    ipDb=[]
    for i in cmd('''/usr/bin/awk '{print $1}' %s |sort |uniq -c''' % LogFile):
    ip = i.strip().split(' ')
    ipDb.append(ip)
    #通过taobao 提供接口分析ip地址来源
    for i in ipDb:
    _tmpD=getIpCountry(i[1])
    #格式说明:ip 访问次数据 ip国家 城市的 isp号 省份 所在地区
    out="%s%s%s%s%s%s%s"%(i[1].ljust(20),i[0].ljust(10),_tmpD['country'].ljust(20),_tmpD['city'].ljust(16),_tmpD['isp_id'].ljust(16),_tmpD['region'].ljust(16),_tmpD['area'].ljust(16))
    print out
    file.write("%s "%out)
    conn.close()
    file.close()
    '''
    '''

  • 相关阅读:
    MySQL DDL 在指定位置新增字段
    .NET平台常见技术框架整理汇总
    使用Linq求和方法Sum计算集合中多个元素和时应该注意的性能问题
    python时间操作
    django 利用原生sql操作数据库
    滑动验证码获取像素偏移量
    python opencv简单使用
    django通过旧数据库库生成models
    pandas 操作csv文件
    简单工厂模式(Simple Factory Pattern)
  • 原文地址:https://www.cnblogs.com/cfinder010/p/3830470.html
Copyright © 2011-2022 走看看