例如,假设我有这个代码:
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()