Created
March 2, 2011 18:31
-
-
Save zuriby/851426 to your computer and use it in GitHub Desktop.
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 SyscallThread(Thread): | |
def __init__(self, commandline): | |
Thread.__init__(self) | |
self.cursor = 0 | |
self.lines = {self.cursor:[]} | |
self.keep_running=False | |
self.commandline=commandline | |
def run(self): | |
print 'starting thread:', self | |
self.keep_running=True | |
self.process = subprocess.Popen(self.commandline, | |
stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True) | |
self.outputfile = self.process.stdout | |
while self.keep_running: | |
for line in self.outputfile: | |
self.lines[self.cursor].append(line) | |
self.process.kill() | |
def new_lines(self): | |
newlines = self.lines[self.cursor] | |
print '%d new lines' % len(newlines) | |
del self.lines[self.cursor] | |
self.cursor += 1 | |
self.lines[self.cursor]=[] | |
return (line for line in newlines) | |
def stop(self): | |
self.keep_running=False | |
print 'stopping thread:', self | |
class TCPDumper(SyscallThread): | |
def __init__(self, commandline): | |
SyscallThread.__init__(self, commandline) | |
class TCPDumpHandler(tornado.web.RequestHandler): | |
dumper = None | |
stopping = None | |
def post(self): | |
cls = TCPDumpHandler | |
# start the thread for first time | |
if not self.get_argument('stop', None): | |
if cls.dumper is None: | |
iface = self.get_argument('iface', 'any') | |
verbose = self.get_argument('verbose', None) and ' -vvvXX ' or '' | |
cls.dumper = TCPDumper('tcpdump %s -nli %s "not port 5799"' % (verbose, iface)) | |
cls.dumper.start() | |
self.write(''.join(map(escape, cls.dumper.new_lines()))) | |
self.flush() | |
# stop the thread | |
elif cls.dumper: | |
cls.dumper.stop() | |
cls.dumper = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment