Created
May 19, 2019 08:35
-
-
Save luliangce/22c2ece57d0b22faf51827ebe47e1d6b to your computer and use it in GitHub Desktop.
寻找自然分钟内访问量大于100的ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.v2ex.com/t/565534#reply8 | |
import random | |
import datetime | |
def genlog(): | |
ips = [] | |
for _ in range(100): | |
ips.append(".".join([ | |
str(random.randint(0, 255)), | |
str(random.randint(0, 255)), | |
str(random.randint(0, 255)), | |
str(random.randint(0, 255)), | |
])) | |
f = open("sample.log", "w") | |
t = datetime.datetime(2019, 5, 19) | |
for i in range(1440): | |
for i in ips: | |
if random.randint(0, 100) < 95: | |
continue | |
count = random.randint(0, 110) | |
while count > 0: | |
f.write("{}\t{}\n".format(t.strftime("%Y-%m-%d %H:%M:%S"), i)) | |
count -= 1 | |
t = t+datetime.timedelta(minutes=1) | |
f.close() | |
def main(): | |
current = None | |
container = {} | |
exceed = set() | |
with open("sample.log", "r") as f: | |
for i in f: | |
ds, ip = i.strip().split("\t") | |
minute = ds[:16] | |
if minute != current: | |
container = {} | |
current = minute | |
try: | |
container[ip] += 1 | |
except: | |
container[ip] = 1 | |
if container[ip] > 100: | |
exceed.add(ip) | |
return exceed | |
if __name__ == "__main__": | |
genlog() | |
print("日志生成完成") | |
print(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment