python - How to keep socket alive? -
should keep alive sockets on server if interact known clients long time , of them stays online , max amount of them 10? if yes best way this? like:
class mysocket: '''demonstration class - coded clarity, not efficiency ''' def __init__(self, sock=none): if sock none: self.sock = socket.socket( socket.af_inet, socket.sock_stream) else: self.sock = sock def connect(self, host, port): self.sock.connect((host, port)) def mysend(self, msg): totalsent = 0 while totalsent < msglen: sent = self.sock.send(msg[totalsent:]) if sent == 0: raise runtimeerror("socket connection broken") totalsent = totalsent + sent def myreceive(self): chunks = [] bytes_recd = 0 while bytes_recd < msglen: chunk = self.sock.recv(min(msglen - bytes_recd, 2048)) if chunk == '': raise runtimeerror("socket connection broken") chunks.append(chunk) bytes_recd = bytes_recd + len(chunk) return ''.join(chunks)
will good? happens if specify more size recv() socket have?
Comments
Post a Comment