diff options
author | Larry Hastings <larry@hastings.org> | 2014-05-19 06:19:55 (GMT) |
---|---|---|
committer | Larry Hastings <larry@hastings.org> | 2014-05-19 06:19:55 (GMT) |
commit | debcb9dd542e06fa8e6fe6f1fb8c4549ae9ddeea (patch) | |
tree | 0dc0ae3885dded042e1496e877bba1c5b2823e9e | |
parent | d7912b9b6412f3f275e5829b42174a9369cd7933 (diff) | |
parent | ae4bab71e3937aaa866d297862a4572e704ab880 (diff) | |
download | cpython-debcb9dd542e06fa8e6fe6f1fb8c4549ae9ddeea.zip cpython-debcb9dd542e06fa8e6fe6f1fb8c4549ae9ddeea.tar.gz cpython-debcb9dd542e06fa8e6fe6f1fb8c4549ae9ddeea.tar.bz2 |
Merge.
-rw-r--r-- | Doc/howto/sockets.rst | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/Doc/howto/sockets.rst b/Doc/howto/sockets.rst index 7a9b0ed..820beb5 100644 --- a/Doc/howto/sockets.rst +++ b/Doc/howto/sockets.rst @@ -204,13 +204,15 @@ length message:: totalsent = totalsent + sent def myreceive(self): - msg = b'' - while len(msg) < MSGLEN: - chunk = self.sock.recv(MSGLEN-len(msg)) + chunks = [] + bytes_recd = 0 + while bytes_recd < MSGLEN: + chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048)) if chunk == b'': raise RuntimeError("socket connection broken") - msg = msg + chunk - return msg + chucks.append(chunk) + bytes_recd = bytes_recd + len(chunk) + return b''.join(chunks) The sending code here is usable for almost any messaging scheme - in Python you send strings, and you can use ``len()`` to determine its length (even if it has |