import sys, os, traceback import flow, filt, utils class DumpError(Exception): pass class Options(object): __slots__ = [ "verbosity", "wfile", ] def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) for i in self.__slots__: if not hasattr(self, i): setattr(self, i, None) class DumpMaster(flow.FlowMaster): def __init__(self, server, options, filtstr, outfile=sys.stdout): flow.FlowMaster.__init__(self, server, flow.State()) self.outfile = outfile self.o = options if filtstr: self.filt = filt.parse(filtstr) else: self.filt = None if options.wfile: path = os.path.expanduser(options.wfile) try: f = file(path, "wb") self.fwriter = flow.FlowWriter(f) except IOError, v: raise DumpError(v.strerror) def handle_clientconnection(self, r): flow.FlowMaster.handle_clientconnection(self, r) r.ack() def handle_error(self, r): flow.FlowMaster.handle_error(self, r) r.ack() def handle_request(self, r): flow.FlowMaster.handle_request(self, r) r.ack() def indent(self, n, t): l = str(t).strip().split("\n") return "\n".join(" "*n + i for i in l) def handle_response(self, msg): f = flow.FlowMaster.handle_response(self, msg) if f: msg.ack() if self.filt and not f.match(self.filt): return if self.o.verbosity == 1: print >> self.outfile, f.client_conn.address[0], print >> self.outfile, f.request.short() print >> self.outfile, " <<", print >> self.outfile, f.response.short(), utils.pretty_size(len(f.response.content)) elif self.o.verbosity == 2: print >> self.outfile, f.client_conn.address[0], print >> self.outfile, f.request.short() print >> self.outfile, self.indent(4, f.request.headers) print >> self.outfile, " <<", f.response.short() print >> self.outfile, self.indent(4, f.response.headers) elif self.o.verbosity == 3: print >> self.outfile, ">>" for i in f.request.request.assemble().splitlines(): print >> self.outfile, "\t", i print >> self.outfile, ">>" print >> self.outfile, "<<" for i in f.request.assemble().splitlines(): print >> self.outfile, "\t", i print >> self.outfile, "<<" self.state.delete_flow(f) if self.o.wfile: self.fwriter.add(f) # begin nocover def run(self): try: return flow.FlowMaster.run(self) except KeyboardInterrupt: pass except Exception, v: traceback.print_exc() self.shutdown()