1

例如,假设我有这个代码:

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,

如何将此类的输出提供给浏览器?由于没有停止点,我不知道在哪里与轮询循环挂钩。不仅如此,由于打印行有效(我看到终端上转储的行),浏览器没有得到完全相同的行,见下文:

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

1 回答 1

1

将 tcpdump 的输出重定向到一个文件并使用它:

https://bitbucket.org/silverspell/tornadolog

希望能帮助到你 :)

于 2011-02-26T21:00:03.970 回答