Use TCP Half-Close to Receive a Reply When the Server Waits for EOF
Use Python socket.shutdown(SHUT_WR) to signal the end of a TCP request while receiving a reply on the same connection. Includes a tested loopback example.
In a TCP protocol where the server reads to EOF before replying, both sides can stall if the client sends its request and immediately waits for a response. In Python, call shutdown(socket.SHUT_WR) after sending all request bytes to signal the end of the request while keeping the connection available for the reply. The example below verifies the behavior over a Linux loopback TCP connection.
Two strands of a connection, with one signal fading while the other continues
Why the server keeps reading after the request is sent
When sendall() returns, it has finished sending the bytes supplied to that call. That does not confirm that the peer application processed the whole request or indicate the end of the TCP stream. If the server waits for EOF in recv() and the client waits for a response, neither side can proceed. Python socket documentation
TCP lets each direction close independently. When the client ends its sending direction with SHUT_WR, the server receives an empty byte string after reading all preceding bytes. The reverse direction stays open, so the server can write a reply on the same connection. The client closes its socket after reading the response. RFC 9293 §3.6.1
Signal the end of the request and read the reply
Save the code as tcp_half_close_demo.py and run python3 tcp_half_close_demo.py. The server uses an operating system-assigned port on loopback. The client sends the request in two calls and shuts down its sending direction. The server checks the complete request after EOF and replies with its length.
Verify the response over loopback
The example ran on Linux x86-64 with Python 3.12.14 on November 16, 2026. It checks that the server reconstructs all eight request bytes and that the client receives the reply after shutting down its sending direction. Exceptions from the server task propagate to the main thread.
When EOF works as a message boundary
This approach fits a protocol that sends one complete request in a direction. After shutting down that direction, the client cannot send another request on the same connection. For multiple requests, use length prefixes or delimiters defined by the protocol. With TLS or HTTP, follow the closure rules of the library and protocol in use. RFC 9293 §3.6.1
The example buffers the entire input in memory and uses per-operation socket timeouts. If a service needs to bound input size and total processing time, track the byte limit as data arrives and enforce a separate overall deadline. This test covers a local plain TCP connection.