title: process socket data that ends with a line break
body: <p>What is the best approach to process a socket connection where I need var <code>data</code> to end with a line break <code>\n</code>? 
  I'm using the code below but sometimes the <code>tcp</code> packets get <strong>chunked</strong> and it takes a long time to match <code>data.endswith("\n")</code>.
  I've also tried other approaches, like saving the last line if it doesn't end with <code>\n</code> and append it to <code>data</code>on the next loop. but this also doesn't work because multiple packets get chunked and the 1st and 2nd part don't match.
  I've no control over the other end, it basically sends multiple lines that end in <code>\r\n</code>.</p>
  
  <p>Any suggestion will be welcome, as I don't have much knowledge on socket connections.</p>
  
  <pre><code>def receive_bar_updates(s):
      global all_bars
      data = ''
      buffer_size = 4096
      while True:
          data += s.recv(buffer_size)
          if not data.endswith("\n"):
              continue
          lines = data.split("\n")
          lines = filter(None, lines)
          for line in lines:
              if line.startswith("BH") or line.startswith("BC"):
                  symbol = str(line.split(",")[1])
                  all_bars[symbol].append(line)
                  y = Thread(target=proccess_bars, kwargs={'symbol': symbol})
                  y.start()
          data = ""
  </code></pre>
  
  <p>Example of "<em>normal</em>" <code>data</code>:</p>
  
  <pre><code>line1\r\n
  line2\r\n
  line3\r\n
  </code></pre>
  
  <p>Example of <strong>chunked</strong> <code>data</code>:</p>
  
  <pre><code>line1\r\n
  line2\r\n
  lin
  </code></pre>