def receive(sock):
    index = -1
    chunk, length = encode(""), encode("")
    received = False

    while not received:
        # Use select to query the socket for activity before calling sock.recv.
        # Choose a timeout of 60 seconds for now.
        readers, writers, in_error = select.select([sock], [], [], 60)

        for rsock in readers:
            while index == -1:
                length += chunk
                chunk = rsock.recv(4096)
                if not chunk:
                    raise socket.error
                index = chunk.find(encode(MESSAGE_SEP))
            length = int(length + chunk[:index])

            msg = chunk[index+1:]
            while len(msg) < length:
                chunk = rsock.recv(length - len(msg))
                if not chunk:
                    raise socket.error
                msg += chunk
            received = True

    command, sep, arguments = msg.partition(encode(MESSAGE_SEP))
    return decode(command), decode(arguments)
Setup a fake service.
dataset/ETHPy150Open enthought/envisage/envisage/plugins/remote_editor/communication/util.py/receive--crossover with--dataset/ETHPy150Open home-assistant/home-assistant/tests/common.py/mock_service