Error Handling in Network Applications
Binary files must be accumulated in a bytes buffer and cannot be printed as they arrive like text files.
The Complete Retrieval Problem
Retrieving an image over HTTP involves more than receiving bytes and placing them directly in a file. The response first contains HTTP headers, followed by a blank line, and then the binary image body. A reliable process must accumulate the complete response, identify the exact boundary between headers and body, discard the headers, and save only the image bytes.
Accumulating Chunks as Bytes
The response arrives through the socket in chunks rather than as one already-separated image. The buffer must therefore begin as an empty bytes object, represented as b"". Each received chunk is concatenated to that growing buffer. The data remains binary throughout this process; it is not treated as printable text.
The accumulation continues until the socket reports that no more data is available. In the source procedure, recv() returns fewer than 1 byte when the server has closed the connection. At that point, the buffer contains both the HTTP headers and the binary image body.
Finding the Header Boundary
After receiving all data, the buffer contains two regions: the HTTP headers at the beginning and the binary body after them. HTTP separates these regions with a blank line represented by two consecutive carriage-return and line-feed sequences: \r\n\r\n.
Use find(b'\r\n\r\n') on the bytes buffer to locate the position where the four-byte boundary marker begins. The returned position identifies the start of the marker, not the start of the image. Because the marker is four bytes long, the binary body begins at position plus 4. Slicing from that point removes the headers and the separator together.
Extracting the Body
A complete HTTP response has been accumulated in a bytes buffer. The double CRLF begins at position pos. Which part should be saved as the image?
- 1Locate the marker
Find the position where b'\r\n\r\n' begins in the buffer.
- 2Skip the separator
The marker occupies four bytes, so the image begins at pos + 4.
- 3Slice the body
Take the buffer from pos + 4 onward. This excludes the headers and the boundary marker.
The bytes from position pos + 4 onward are the binary body to write to the image file.
Saving Bytes Without Transformation
Once the binary body has been extracted, write it to a file opened in binary write mode, represented as wb. Binary mode preserves the received data. Text mode can apply encoding transformations, which can corrupt binary image data.
Defensive Checks Before Saving
The response headers contain a Content-Type header describing the kind of data in the body. For an expected image, a value such as image/jpeg or image/png supports the conclusion that the body is an image. If the server instead returns Content-Type: text/html, the response may not be the requested image, so the data should not be saved as an image file.
Treat the boundary search and Content-Type check as gates in the process. Without a located boundary, the headers cannot be separated reliably. Without an expected image Content-Type, saving the body under an image filename may hide the fact that the server returned different data.
Common Separation and Storage Mistakes
Common mistake Initializing the buffer as text instead of bytes
What it looks likeUsing a string buffer for data received from the socket
Why it failsThe image is binary data and must be accumulated in a bytes buffer.
Do this insteadStart with an empty bytes object, b"", and concatenate received binary chunks.
Common mistake Saving the entire response
What it looks likeWriting the buffer from its first byte directly to the image file
Why it failsThe beginning of the buffer contains HTTP headers, not image bytes.
Do this insteadFind b'\r\n\r\n' and write only the slice beginning at the boundary position plus 4.
Common mistake Hard-coding the header length
What it looks likeAssuming the image always begins at one fixed position
Why it failsThe boundary must be located in the actual response rather than assumed.
Do this insteadUse find(b'\r\n\r\n') for each accumulated response.
Common mistake Skipping the wrong number of bytes
What it looks likeStarting the body slice at the marker position instead of after the marker
Why it failsThe double CRLF marker itself is four bytes and is not part of the image body.
Do this insteadStart at position plus 4.
Common mistake Opening the output file in text mode
What it looks likeWriting image bytes without binary write mode
Why it failsText-mode encoding transformations can corrupt binary data.
Do this insteadOpen the output file in wb mode.
Common mistake Ignoring Content-Type
What it looks likeSaving a response labeled text/html as an image
Why it failsThe server may have returned something other than the expected image.
Do this insteadInspect Content-Type and verify that it indicates an image such as image/jpeg or image/png.
Practice the Data Trace
A socket response has been accumulated in a bytes buffer. Describe the exact order of operations needed to save the image safely. Include the buffer type, the boundary marker, the slice starting position, the Content-Type check, and the file mode.
Hints
- The buffer must contain both the headers and body before the boundary is located.
- The boundary marker is four bytes long.
- Only the binary body should be written to the output file.
What do you think happens?
The boundary marker begins at position pos. Which position contains the first byte of the image body?
Reveal answer
pos + 4
The marker b'\r\n\r\n' is four bytes long, so the image body begins after all four marker bytes.
Process Checklist
- 1Establish the socket connection and send an HTTP GET request for the binary file.
- 2Initialize an empty bytes buffer and concatenate each received chunk until the server closes the connection.
- 3Locate the header-body boundary with
find(b'\r\n\r\n'). - 4Extract the binary body by slicing from the boundary position plus 4.
- 5Check Content-Type to confirm that the response represents the expected image.
- 6Write only the extracted body to a file opened in binary write mode, wb.
Key Takeaways
- Accumulate network response chunks in a bytes buffer because an image is binary data.
- The HTTP headers end at the double CRLF marker, b'\r\n\r\n'.
- If the marker begins at pos, the binary body begins at pos + 4.
- Check Content-Type before treating the body as an image.
- Write the extracted body using binary write mode, wb, to avoid corrupting the file.