统计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
原文地址:http://wangpengtai.blog.51cto.com/3882831/1971249