# receive.py # # Receive a 100MB message through a socket using two different techniques. # Run this program first. from timethis import * from socket import * MSGSIZE = 1048576*100 # 100MB s = socket(AF_INET,SOCK_STREAM) s.setsockopt(SOL_SOCKET, SO_REUSEADDR,1) s.bind(("",20000)) s.listen(1) c,a = s.accept() with timethis("Receive chunks and join"): chunks = [] remaining = MSGSIZE while remaining > 0: chunk = c.recv(remaining) chunks.append(chunk) remaining -= len(chunk) msg = b"".join(chunks) del msg # Send ACK to request start to next test c.send(b"x") with timethis("Receive into a bytearray using memory views"): msg = bytearray(MSGSIZE) view = memoryview(msg) while view: nbytes = c.recv_into(view) view = view[nbytes:] c.close()