From 0c6f846861f537e5ebf61a1c3ac536ca04fa5892 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 16 Feb 2011 14:33:04 +1300 Subject: First draft of the new serialization mechanism. --- libmproxy/flow.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'libmproxy/flow.py') diff --git a/libmproxy/flow.py b/libmproxy/flow.py index 9596d416..6031a009 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -2,9 +2,9 @@ This module provides more sophisticated flow tracking. These match requests with their responses, and provide filtering and interception facilities. """ -import subprocess, base64, sys +import subprocess, base64, sys, json from contrib import bson -import proxy, threading +import proxy, threading, netstring class RunException(Exception): def __init__(self, msg, returncode, errout): @@ -302,3 +302,30 @@ class State: rt = ReplayThread(f, masterq) rt.start() #end nocover + + + +class FlowWriter: + def __init__(self, fo): + self.fo = fo + self.ns = netstring.FileEncoder(fo) + + def add(self, flow): + d = flow.get_state() + s = json.dumps(d) + self.ns.write(s) + + +class FlowReader: + def __init__(self, fo): + self.fo = fo + self.ns = netstring.decode_file(fo) + + def stream(self): + """ + Yields Flow objects from the dump. + """ + for i in self.ns: + data = json.loads(i) + yield Flow.from_state(data) + -- cgit v1.2.3