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

1.1k Views Asked by At

For instance, suppose I have this code:

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()
1

There are 1 best solutions below

6
On

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

https://bitbucket.org/silverspell/tornadolog

Hope it helps :)