Python serialize HTTPFlow object (MITM)
<p>I am using <a href="http://docs.mitmproxy.org/en/stable/introduction.html" rel="nofollow">mitmproxy</a>, a python man-in-the-middle (MITM) proxy for HTTP, to modify on the fly the HTTP request of a certain website.</p>

<p><strong>Goal:</strong></p>

<p>For test purposes, when it receives an HTTP request, it should save it (it receives a <a href="http://docs.mitmproxy.org/en/stable/dev/models.html#mitmproxy.models.HTTPFlow" rel="nofollow">HTTPFlow</a> object) and the next time the same request will be made i need to resent the exact same <em>data/html/header/resourses/ecc..</em> to the browser.</p>

<p><strong>Problem:</strong></p>

<p>The obvious solution is to serialize the object but <strong>it isn't serializable</strong>! </p>

<p>I cannot simply <strong>keep it in memory</strong> because i need to restart the proxy during the tests</p>

<p>What can i do to achieve my goal?</p>

<p><strong>Details:</strong></p>

<p>I have already tried <em>pickle</em>, <em>cPickle</em> and <em>marshal</em> with the following errors:</p>

<blockquote>
  <ul>
  <li><p>a class that defines __slots__ without defining __getstate__ cannot be pickled</p></li>
  <li><p>can't pickle CDataGCP objects</p></li>
  <li>ValueError: unmarshallable object</li>
  </ul>
</blockquote>

<p><strong>Ideas</strong>:</p>

<ul>
<li>1) how much is it a bad idea to change the original object <strong>to make it serializable</strong>? and how can i do it?</li>
<li>2) what if the main process <strong>communicate with a second always-alive python process</strong> which simply keep the object in memory? 
are they still need to comunicate obj serializing them?</li>
</ul>

<p>Found solution (the HTTPFlow obj has a method to get the state)</p>

<p>Imports</p>

<pre><code>from mitmproxy.models import HTTPFlow
from mitmproxy.models import HTTPResponse
</code></pre>

<p>Save state:</p>

<pre><code>cached_state = http_flow_response.response.get_state()
</code></pre>

<p>Load state:</p>

<pre><code># create the obj 
http_response = HTTPResponse(
        cached_state['http_version'], # "HTTP/1.1"
        cached_state['status_code'],  # 200
        cached_state['reason'],       # "Ok"
        cached_state['headers'],      # Headers(content_type="...")
        cached_state['content'])      # str
# send the obj
http_flow_request.reply(cached_http_response)
</code></pre>

python serialization ipc mitmproxy