'Read data by portions'

def downstream_recv(self, data):

    upcoming = data.read()
    packets = (self.packets + upcoming)
    while True:
        if (self.to_read == 0):
            self.to_read = struct.unpack_from('>I', packets)[0]
            packets = packets[4:]
        if (self.to_read <= len(packets)):
            self.upstream.write(packets[:self.to_read])
            packets = packets[self.to_read:]
            self.to_read = 0
            if ((self.to_read == 0) and (len(packets) > 0)):
                continue
        break
    self.packets = packets
