python-Monitoring Log Exercise

Source: Internet
Author: User
Tags ip number log log

There is a access.log log, formatted as follows, with each line starting with an IP address:

1. Demand:

#1, if you access more than 200 times within the same IP address 60s, add IP to blacklist
#需求分析:
#1, 60 seconds to read a file
#2, split, take the first element, IP address
#3, add all the IP to a list, if the IP number more than 200 times, blacklist

Import time
Point = 0 #文件指针
While True:
IPS = [] #存放所有的ip地址
Blk_set = set () #存放需要加入黑名单ip
With open (' Access.log ', encoding= ' utf-8 ') as F:
F.seek (Point)
For line in F:
ip = line.split () [0]
Ips.append (IP)
#10.125.1.1
If Ips.count (IP) >200:
Blk_set.add (IP)
For IP in Blk_set: #这里是因为防止ip重复加入黑名单 because the collection is de-heavy, there is no duplicate IP inside
#os. System (' Jiaurru%s '%ip) #挨个将集合中的信息 blacklist
Print (' Add blacklist%s '%ip)
Point = F.tell ()
Time.sleep (#隔30s) monitoring once


python-Monitoring Log Exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.