title: Python Requests - Chunked Streaming
body: <p>due to a faulty server design, I'm having to stream down JSON and correct a null byte if I find one. I'm using <code>python requests</code> to do this. Each JSON event is delimited by a <code>\n</code>. What I am trying to do here is pull down a chunk (which will always be less than one log line). Search through that chunk for the end of event signifier (<code>"\"status\":\d+\d+\d+}}\n"</code>).</p>
  
  <p>If that signifier is there I will do something with the full JSON event, if not, I add that chunk to a buffer, <code>b</code>, then grab the next chunk and look for the identifier. As soon as I get this down, I'll start searching for the null byte.</p>
  
  <pre><code>b = ""
  
  for d in r.iter_content(chunk_size=25):
  
      s = re.search("\"status\":\d+\d+\d+}}\n", d)
  
      if s:
          d = d.split("\n", 1)
          fullLogLine = b + d[0]
          b = d[1]
      else:
          b = b + d
  </code></pre>
  
  <p>I'm completely losing the value of b in this case. It doesn't seem to carry over through the <code>iter_content</code>. Whenever I try to print the value of just <code>b</code> it's empty. I feel I'm missing something obvious here. Anything helps. Thanks.</p>