首页 > 数据库技术 > 详细

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

时间:2018-01-02 22:33:11      阅读:377      评论:0      收藏:0      [点我收藏+]
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)

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

原文:https://www.cnblogs.com/xiongrongqin/p/8179209.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!