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

时间:2016-03-02 12:14:50

标签: python subprocess urllib

我正在努力完成一个我真的不知道该怎么做的想法。基本上我试图通过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]

处理stdout值,然后按下面所示推送值

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

param2是我将处理subprocess.Popen

给出的结果的值

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

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

这应该是实时的,python脚本将继续获取新值,处理它然后推送值。

1 个答案:

答案 0 :(得分:1)

我根据需要尝试代码,但我只在日志中打印新行,你可以处理行和推送

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='')

这打印到控制台的新行,只需编辑和使用:)也许我会帮助你:))