开发者

How can I serve a (never ending) system call with Tornado

开发者 https://www.devze.com 2023-02-13 10:16 出处:网络
For instance, suppose I have this co开发者_运维技巧de: def dump(): tcpdump = subprocess.Popen(\"tcpdump -nli any\",

For instance, suppose I have this co开发者_运维技巧de:

def dump():
    tcpdump = subprocess.Popen("tcpdump -nli any", 
        stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
    outputfile = tcpdump.stdout

    for line in outputfile:
        print line,

How can I serve the output of such to the browser? Since there is no stopping point, I have no idea where to hook with the polling loop. More than that, as print line works (I see lines dumped on the terminal), browser do not get the very same lines, see below:

class TCPDumpHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("<form method='post' action='/log'><input type='submit'></form>")

    @tornado.web.asynchronous
    def post(self):
        tcpdump = subprocess.Popen("tcpdump -nli any", 
            stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
        outputfile = tcpdump.stdout

        for line in outputfile:
            print line,
            self.write(line)

        self.finish()


Redirect tcpdump's output to a file and use this:

https://bitbucket.org/silverspell/tornadolog

Hope it helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消