non blocking socket python

Solutions on MaxInterview for non blocking socket python by the best coders in the world

showing results for - "non blocking socket python"
Assia
09 Feb 2016
1class MySocket:
2    """demonstration class only
3      - coded for clarity, not efficiency
4    """
5
6    def __init__(self, sock=None):
7        if sock is None:
8            self.sock = socket.socket(
9                            socket.AF_INET, socket.SOCK_STREAM)
10        else:
11            self.sock = sock
12
13    def connect(self, host, port):
14        self.sock.connect((host, port))
15
16    def mysend(self, msg):
17        totalsent = 0
18        while totalsent < MSGLEN:
19            sent = self.sock.send(msg[totalsent:])
20            if sent == 0:
21                raise RuntimeError("socket connection broken")
22            totalsent = totalsent + sent
23
24    def myreceive(self):
25        chunks = []
26        bytes_recd = 0
27        while bytes_recd < MSGLEN:
28            chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))
29            if chunk == b'':
30                raise RuntimeError("socket connection broken")
31            chunks.append(chunk)
32            bytes_recd = bytes_recd + len(chunk)
33        return b''.join(chunks)
Till
16 May 2020
1The magic is real