#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Random_lee import time import os import re def count_pv(): logfile = 'access_log' if not os.path.exists(logfile): print('error:' + logfile + ' not existed.') return 0 else: f=open(logfile,'r') num=0 for line in f: expr= re.compile('^(?P<RemoteIP>.*) - - (?P<datatime>.*) (?P<request>".+") (?P<status>\d{3}) (?P<web_size>\d{1,10})') try: log_info=expr.search(line) log_info=log_info.groupdict() # 解析日志信息 datatime = log_info["datatime"] # 取出日志信息中的datatime datatime = datatime.replace('[', '') datatime = datatime.replace(']', '') # 去掉[] data_time = datatime.split(' ')[0] # 取出日期时间 time_zone = datatime.split(' ')[1] # 取出时区 if time_zone == '+0800': # print(time_zone) # print(data_time) ctime = time.strptime(data_time, '%d/%b/%Y:%H:%M:%S') # 转换为格式化时间 time_stamp = time.mktime(ctime) # 转换为时间戳 # print(time_stamp) if time.time() - time_stamp <= 300: num+=1 # print(datatime) else: # print("error data_time:%s"%datatime) pass else: return 0 except: pass f.close() return num a=count_pv() a=str(a) status_file=open('pv_info','w') status_file.write(a)
统计nginx日志里每五分钟的访问量
猜你喜欢
转载自www.cnblogs.com/randomlee/p/9490466.html
今日推荐
周排行