summaryrefslogtreecommitdiffstats
path: root/Doc/howto/sockets.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-05-18 20:02:25 (GMT)
committerRaymond Hettinger <python@rcn.com>2014-05-18 20:02:25 (GMT)
commitae4bab71e3937aaa866d297862a4572e704ab880 (patch)
treea46081764a7b0a3e05e37f7f812a3b0272cc39f9 /Doc/howto/sockets.rst
parent3a260d228b32b04a88d947b887bf81759e8e5f10 (diff)
downloadcpython-ae4bab71e3937aaa866d297862a4572e704ab880.zip
cpython-ae4bab71e3937aaa866d297862a4572e704ab880.tar.gz
cpython-ae4bab71e3937aaa866d297862a4572e704ab880.tar.bz2
Don't grow strings by concatenation. Use ''.join() instead.
Diffstat (limited to 'Doc/howto/sockets.rst')
-rw-r--r--Doc/howto/sockets.rst12
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