我如何使用 Tornado 提供(永无止境的)系统调用
例如,假设我有以下代码:
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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 tcpdump 的输出重定向到文件并使用:
https://bitbucket.org/silverspell/tornadolog
希望如此有帮助:)
Redirect tcpdump's output to a file and use this:
https://bitbucket.org/silverspell/tornadolog
Hope it helps :)