You should read in one byte at a time in a loop, using conn.recv(1). Keep going until you hit '\r\n\r\n'. Once you hit that, you've got all your headers and can figure out the length of the body using the Content-length header.