jhfgds 发表于 2017-10-11 09:19:42

python统计nginx日志域名下载量

统计nginx访问日志,access.log形式:
1xx.xx.xxx.xxx- - "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的域名和下载次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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- - "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)://+.+.+'
    f = open('/tmp/access.log','r')
    for line in f:
      Domain_Name = re.search(patten,line.split())
      if Domain_Name:
            dn = Domain_Name.group(0)
            if dn not in dn_dict:
                dn_dict = 1
            else:
                dn_dict += 1
    #使用内建函数sorted()排序
    #sorted(dict.items(), key=lambda e:e, reverse=True)
    dn_ranking = sorted(dn_dict.items(),key=lambda item:item, 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,item))
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,item)
      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,row))
    except:
      print('Error: unable to fetch data')
    db.close()
if __name__ == '__main__':
    dispalydomainName()
    #insert()
    #select()



kissitss 发表于 2017-10-12 21:35:48

许学些uxi
页: [1]
查看完整版本: python统计nginx日志域名下载量