import socket, os, sys def readFromSockUntil(sock, size): rbufsize = 8192 data = "" buf_len = len(data) overflow = "" buffers = [] iteration = 0 cumulative_req = 0 while True: iteration += 1 print "iteration %d" % iteration left = size - buf_len recv_size = max(rbufsize, left) print "--> bytes to go: %d" % left print "--> recv_size: %d" % recv_size data = sock.recv(recv_size) print "--> got: %d" % len(data) if not data: break buffers.append(data) print "--> total buffers: %d" % len(buffers) n = len(data) if n >= left: # normally, this would put excess data int self._rbuf overflow = data[left:] buffers[-1] = data[:left] break buf_len += n print "--> total received: %d" % buf_len cumulative_req += recv_size print "--> total requested: %d" % cumulative_req return "".join(buffers) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(("127.0.0.1", 9330)) size = 10485760 data = readFromSockUntil(sock, size) sock.close