Created
May 16, 2011 23:27
-
-
Save zuriby/975588 to your computer and use it in GitHub Desktop.
Stream handlers with Tornado
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StreamHandler(AMSHandler): | |
@tornado.web.asynchronous | |
def get(self): | |
self.post() | |
@tornado.web.asynchronous | |
def post(self): | |
self.write("<pre>") | |
self.ioloop = tornado.ioloop.IOLoop.instance() | |
self.pipe = self.get_pipe() | |
self.ioloop.add_handler( self.pipe.fileno(), self.async_callback (self.on_read), self.ioloop.READ) | |
def on_read(self, fd, events): | |
buffer = self.pipe.read(1024) | |
try: | |
assert buffer | |
self.write(buffer) | |
self.flush() | |
except: | |
self.pipe.close() | |
self.ioloop.remove_handler(fd) | |
self.finish() | |
def _get_pipe(self, commandline): | |
return subprocess.Popen( | |
commandline, | |
shell=True, | |
stdout=subprocess.PIPE, | |
stdin=subprocess.PIPE, | |
stderr=subprocess.PIPE).stdout | |
def get_pipe(self): | |
return self._get_pipe('cat /dev/urandom') | |
class TCPDumpHandler(StreamHandler): | |
def get_pipe(self): | |
iface = self.get_argument('iface', 'any') | |
verbose = self.get_argument('verbose', None) and ' -vvvXX ' or '' | |
return self._get_pipe("tcpdump %s -nli %s 'not port 22'" % (verbose, iface)) | |
class LogFileHandler(StreamHandler): | |
def get_pipe(self): | |
return self._get_pipe("tail -f /var/log.syslog") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment