1. 程式人生 > >python統計nginx日誌域名下載量

python統計nginx日誌域名下載量

訪問量 統計 python

統計nginx訪問日誌,access.log形式:

1xx.xx.xxx.xxx - - [09/Oct/2017:10:48:21 +0800] "GET /images/yyy/2044/974435412336/Cover/9787503434r.jpg HTTP/1.1" 304 0 "http://www.xxx.net/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/xx.0.xx2.xx Safari/537.36" "0.001" "-" "-" "-""xxx.xxx.xxx.xxx:xxxx" "304" "0" "0.001"

(1)將被訪問的域名和次數存入數據庫中

(2)將下載量排名前10的列出來

方法:讀取access.log日誌將其所需要信息截取出來生成字典,利用字典鍵的唯一性統計出下載次數,並將字典進行排序,截取出排名前10的域名和下載次數

#!/usr/bin/env python3
#__*__coding:utf8__*__
import MySQLdb
import re
#db = MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘123456‘,db=‘testdb‘)
#access.log :log style is ===>
‘‘‘
114.250.90.86  - - [09/Oct/2017:10:48:21 +0800] "GET /images///book/201709/9787503541216/Cover/9787503541216.jpg HTTP/1.1" 304 0 "http://www.dyz100.net/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36" "0.001" "-" "-" "-""10.1.12.201:9092" "304" "0" "0.001"
‘‘‘
def domainName():
    dn_dict = {}
    patten = ‘(http|https)://[a-z]+.[a-z0-9]+.[a-zA-Z]+‘
    f = open(‘/tmp/access.log‘,‘r‘)
    for line in f:
        Domain_Name = re.search(patten,line.split()[10])
        if Domain_Name:
            dn = Domain_Name.group(0)
            if dn not in dn_dict:
                dn_dict[dn] = 1
            else:
                dn_dict[dn] += 1
    #使用內建函數sorted()排序
    #sorted(dict.items(), key=lambda e:e[1], reverse=True)
    dn_ranking = sorted(dn_dict.items(),key=lambda item:item[1], reverse = True)
    return dn_ranking
def dispalydomainName():
    ‘‘‘print top 10 ranking for download ‘‘‘
    dn_ranking = domainName()
    print("下載量排名前10的域名列表為:")
    for item in dn_ranking[:11]:
        print("域名: %s ===> 下載次數: %d" % (item[0],item[1]))
def insert():
    db = MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘123456‘,db=‘testdb‘)
    cursor = db.cursor()
    cursor.execute(‘drop table if exists ranking‘)
    sql = ‘create table ranking (id int(10) not null primary key auto_increment, domainName varchar(100), download_times int(100))‘
    cursor.execute(sql)
    dn_ranking = domainName()
    for item in dn_ranking:
        sql = ‘‘‘insert into ranking (domainName,download_times) values (‘%s‘,%d)‘‘‘ %(item[0],item[1])
        cursor.execute(sql)
    # sql = ‘‘‘insert into ranking (domainName,download_times) values (‘wpt‘,2)‘‘‘
    try:
        #執行sql語句
        cursor.execute(sql)
        db.commit()
    except:
        db.rollback()
    db.close()
def select():
    db = MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘123456‘,db=‘testdb‘)
    cursor = db.cursor()
    sql = ‘‘‘select * from ranking‘‘‘
    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        for row in results:
            print("域名: %s ===> 下載次數: %d" %(row[1],row[2]))
    except:
        print(‘Error: unable to fetch data‘)
    db.close()
if __name__ == ‘__main__‘:
    dispalydomainName()
    #insert()
    #select()

技術分享

技術分享


本文出自 “LINUX” 博客,請務必保留此出處http://wangpengtai.blog.51cto.com/3882831/1971249

python統計nginx日誌域名下載量