8000 gh-111112: Avoid potential confusion in TCP server example. (#111113) · python/cpython@a79a272 · GitHub
[go: up one dir, main page]

Skip to content

Commit a79a272

Browse files
authored
gh-111112: Avoid potential confusion in TCP server example. (#111113)
Improve misleading TCP server docs and example. socket.recv(), as documented by the Python reference documentation, returns at most `bufsize` bytes, and the underlying TCP protocol means there is no guaranteed correspondence between what is sent by the client and what is received by the server. This conflation could mislead readers into thinking that TCP is datagram-based or has similar semantics, which will likely appear to work for simple cases, but introduce difficult to reproduce bugs.
1 parent 80aa7b3 commit a79a272

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Doc/library/socketserver.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ This is the server side::
494494
def handle(self):
495495
# self.request is the TCP socket connected to the client
496496
self.data = self.request.recv(1024).strip()
497-
print("{} wrote:".format(self.client_address[0]))
497+
print("Received from {}:".format(self.client_address[0]))
498498
print(self.data)
499499
# just send back the same data, but upper-cased
500500
self.request.sendall(self.data.upper())
@@ -525,8 +525,9 @@ objects that simplify communication by providing the standard file interface)::
525525

526526
The difference is that the ``readline()`` call in the second handler will call
527527
``recv()`` multiple times until it encounters a newline character, while the
528-
single ``recv()`` call in the first handler will just return what has been sent
529-
from the client in one ``sendall()`` call.
528+
single ``recv()`` call in the first handler will just return what has been
529+
received so far from the client's ``sendall()`` call (typically all of it, but
530+
this is not guaranteed by the TCP protocol).
530531

531532

532533
This is the client side::

0 commit comments

Comments
 (0)
0