#!/usr/local/python/bin/python
# -*- coding: utf-8 -*-
import sys,time,commands,linecache
import subprocess
from urlparse import urlparse
from tailf import tailf
import shutil,struct,socket
ip_max_count = 100
def str2int(str):
uint = socket.ntohl(struct.unpack("I",socket.inet_aton(str))[0])
return struct.unpack("i", struct.pack('I', uint))[0]
cur_ip_cache = {}
commands.getstatusoutput("/home/nginx/sbin/nginx -s reload")
time.sleep(5)
fh=open('/home/nginx/conf/v42_zhaoren_deny.conf.python','r')
for line in fh.readlines():
if line[0:4] == "deny":
ip_temp = line.split(' ')[1][0:-2].strip().lstrip()
cur_ip_cache[ip_temp] = ip_max_count+1
fh.close()
log_file = "/home/nginx/logs/access.cn.log"
for line in tailf(log_file):
parse_info = line.split(' ')
ip_address = parse_info[0].strip()
log_time = parse_info[1]+parse_info[2]
if not cur_ip_cache.has_key(ip_address):
cur_ip_cache[ip_address] = 1
else:
#print cur_ip_cache[ip_address]
if cur_ip_cache[ip_address] < ip_max_count:
cur_ip_cache[ip_address] += 1
if cur_ip_cache[ip_address] == ip_max_count:
f=open('zhaoren.log','a')
f.write(log_time+"\t\t"+ip_address+"\n")
f.close()
f=open('/home/nginx/conf/access_deny.conf.python','a')
f.write("deny \t"+ip_address+";\n")
f.close()
shutil.copyfile("/home/nginx/conf/access_deny.conf.python", "/home/nginx/conf/access_deny.conf")
#temp = commands.getstatusoutput("/home/nginx/sbin/nginx -s reload")
python分析nginx根据每日请求量自动屏蔽
最新推荐文章于 2024-07-18 21:32:02 发布