mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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.
This commit is contained in:
parent
80aa7b3688
commit
a79a27242f
1 changed files with 4 additions and 3 deletions
|
@ -494,7 +494,7 @@ This is the server side::
|
||||||
def handle(self):
|
def handle(self):
|
||||||
# self.request is the TCP socket connected to the client
|
# self.request is the TCP socket connected to the client
|
||||||
self.data = self.request.recv(1024).strip()
|
self.data = self.request.recv(1024).strip()
|
||||||
print("{} wrote:".format(self.client_address[0]))
|
print("Received from {}:".format(self.client_address[0]))
|
||||||
print(self.data)
|
print(self.data)
|
||||||
# just send back the same data, but upper-cased
|
# just send back the same data, but upper-cased
|
||||||
self.request.sendall(self.data.upper())
|
self.request.sendall(self.data.upper())
|
||||||
|
@ -525,8 +525,9 @@ objects that simplify communication by providing the standard file interface)::
|
||||||
|
|
||||||
The difference is that the ``readline()`` call in the second handler will call
|
The difference is that the ``readline()`` call in the second handler will call
|
||||||
``recv()`` multiple times until it encounters a newline character, while the
|
``recv()`` multiple times until it encounters a newline character, while the
|
||||||
single ``recv()`` call in the first handler will just return what has been sent
|
single ``recv()`` call in the first handler will just return what has been
|
||||||
from the client in one ``sendall()`` call.
|
received so far from the client's ``sendall()`` call (typically all of it, but
|
||||||
|
this is not guaranteed by the TCP protocol).
|
||||||
|
|
||||||
|
|
||||||
This is the client side::
|
This is the client side::
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue