使用子流程处理 access_log 的实时输出

Processing realtime output of access_log with subprocess

我正在努力实现一个我真的不知道该怎么做的想法。基本上我正在尝试通过 grep 命令捕获值,如下所示

p = subprocess.Popen('tail -f /data/qantasflight/run/tomcat/logs/localhost_access_log.2016-02-29.txt  | grep /qantas-ui/int/price?', stdout=subprocess.PIPE, shell = True)
stdout = p.communicate()[0]

处理标准输出值,然后按如下所示推送值

f = urllib.urlopen("http://162.16.1.90:9140/TxnService", params2)

param2 是我将处理 subprocess.Popen

给出的结果的值

简而言之,我想要以下内容:

-- 等待新值--> -- 处理值--> -- 推送值-->

这应该是实时的,python 脚本将不断获取新值,对其进行处理,然后推送该值。

我按照你的想法尝试编写代码,但我只在日志中打印新行,你可以处理行并推送

from __future__ import print_function
import subprocess
from time import sleep

f = open('test.log', 'r')
while True:
    line = ''
    while len(line) == 0 or line[-1] != '\n':
        tail = f.readline()
        if tail == '':
            continue
        line = tail

    print(line, end='')

此打印新行到控制台,只需编辑并使用 :) 也许我可以帮助你 :)