+def recv_chunk(sock):
+ buf = sock.recv(10) # Scan for an up to a 4GiB chunk size (0xffffffff).
+ if buf:
+ crlf = buf.find(b'\r\n') # Find the HTTP chunk size.
+ if crlf > 0:
+ remaining = int(buf[:crlf], 16) # Decode the chunk size.
+ chunk = bytearray(remaining) # Create the chunk buffer.
+
+ start = crlf + 2 # Add in the length of the header's CRLF pair.
+ end = len(buf) - start
+
+ chunk[:end] = buf[start:]
+ chunk[end:] = sock.recv(remaining - end)
+
+ sock.recv(2) # Read the trailing CRLF pair. Throw it away.
+
+ return chunk
+ return b''
+
+## recv_chunk()
+
+