3 '''Buffer a pre-created socket.
9 '''Read exactly n bytes from the buffered socket.
10 Return remaining buffer if <n bytes remain and socket closes.
12 while len(self.buffer) < n:
13 data = self.sock.recv(1024)
19 # split off the message bytes from the buffer.
20 data,self.buffer = self.buffer[:n],self.buffer[n:]
23 def put_bytes(self,data):
24 self.sock.sendall(data)
27 '''Read a null-terminated UTF8 data string and decode it.
28 Return an empty string if the socket closes before receiving a null.
30 while b'\x00' not in self.buffer:
31 data = self.sock.recv(1024)
35 # split off the string from the buffer.
36 data,_,self.buffer = self.buffer.partition(b'\x00')
41 raise ValueError('string contains delimiter(null)')
42 self.sock.sendall(s.encode() + b'\x00')