mirror of
https://github.com/python/cpython.git
synced 2024-11-24 02:15:30 +08:00
Don't grow strings by concatenation. Use ''.join() instead.
This commit is contained in:
parent
3a260d228b
commit
ae4bab71e3
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user