模拟管道,实现功能:tail -f access.log | grep '404'

import time
def tail(filepath):
with open(filepath,'rb') as f:
f.seek(0,2)
while True:
line=f.readline()
if line:
yield line
else:
time.sleep(0.05)

def grep(lines,pattern):
for line in lines:
line=line.decode('utf-8')
if pattern in line:
yield line


lines=grep(tail('access.log'),'404')

for line in lines:
print(line)
原文地址:https://www.cnblogs.com/xiongrongqin/p/8179209.html