The client printed HTTP 200, read the first six bytes of the response, and then raised a timeout while waiting for the rest. If I had logged only the status code, the request would have looked successful.
I wanted to reproduce that split without a proxy, an external service, or a flaky network. The following program starts a local HTTP server and sends two responses with the same status and the same declared length. One response finishes. The other sends a few bytes and deliberately stalls.

Two responses with the same status
Save this as a Python file and run it with Python 3. It uses only the standard library.
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServerfrom threading import Event, Threadfrom urllib.request import urlopenrelease = Event()class Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-Length', '13') self.end_headers() self.wfile.write(b'first\n') self.wfile.flush() if self.path == '/complete': self.wfile.write(b'second\n') else: release.wait() def log_message(self, *args): passserver = ThreadingHTTPServer(('127.0.0.1', 0), Handler)thread = Thread(target=server.serve_forever, daemon=True)thread.start()try: base = f'http://127.0.0.1:{server.server\_port}' with urlopen(base + '/complete', timeout=0.2) as response: print('complete:', response.status, repr(response.read())) with urlopen(base + '/stall', timeout=0.2) as response: print('stalled status:', response.status) print('stalled first:', repr(response.read(6))) try: response.read(7) except TimeoutError: print('stalled remaining: timed out')finally: release.set() server.shutdown() server.server_close() thread.join()
This was the output from my run with Python 3.12.16:
complete: 200 b'first\nsecond\n'stalled status: 200stalled first: b'first\n'stalled remaining: timed out
The server declares a 13-byte body. The first six bytes are first\n. The complete route sends the remaining seven bytes, second\n, and the read finishes. The stalled route sends the first six and then waits on an event. Nothing arrives during the client’s 0.2-second timeout.
There is no random sleep in the handler. The event keeps the incomplete response open until the client has observed the timeout. Cleanup then releases the handler and shuts down the local server.
Where the success ended
The status code arrived with the response headers. At that point urlopen returned a response object, so response.status was already available. The body was a separate stream of bytes still being read from the connection.
The first read(6) succeeded on the stalled route. The next read(7) could not return those seven bytes because the server had not sent them. The exception happened during that second read, after the code had already seen HTTP 200 and consumed part of the body.
This matters when an application records success as soon as it has a response object. It can count a request as successful even though the data it needed never arrived. An HTTP 200 says what the server put in the status line. It does not certify that the client received the entire body.
The two routes also show why the presence of some data is weak evidence. A parser might receive the start of a JSON document, an export might write the first chunk of a file, or an event consumer might see one line. Those bytes can be valid as far as they go while the response as a whole remains incomplete. In the example, Content-Length gives the client a specific number of bytes to expect, making the missing seven bytes easy to identify.
What I would measure in an API client
I would record the HTTP status and the outcome of reading the body separately. For a download, I would also record the number of bytes received and the expected length when the server supplies one. I would only mark the operation complete after the body has been read and whatever parsing or validation the application requires has succeeded.
The timeout value here is for blocking network operations. It is not a guarantee that an entire multi-step download will finish within 0.2 seconds. This test has only two reads and an intentional stall; it does not measure an overall request deadline, a slow but continuously streaming server, or behavior behind a proxy. Those would need separate experiments.
Retrying also depends on what the request does. Repeating a read-only download may be straightforward. Repeating a request that changes state requires a way for the server to recognize the same intended operation, especially if the client cannot tell how much work happened before the body failed.
The result is small but useful: the complete route returned HTTP 200 and a complete body; the stalled route returned the same HTTP 200 and then timed out after six bytes. I now treat receiving a status and finishing a response as two different events in client-side logs.
ссылка на оригинал статьи https://habr.com/ru/articles/1115248/