def create_socket_pair(module, port=0):
    # See if socketpair() is available.
    have_socketpair = hasattr(module, 'socketpair')
    if have_socketpair:
        client_sock, srv_sock = module.socketpair()
        return client_sock, srv_sock

    # Create a non-blocking temporary server socket
    temp_srv_sock = module.socket()
    temp_srv_sock.setblocking(False)
    temp_srv_sock.bind(('', port))
    port = temp_srv_sock.getsockname()[1]
    temp_srv_sock.listen(1)

    # Create non-blocking client socket
    client_sock = module.socket()
    client_sock.setblocking(False)
    try:
        client_sock.connect(('localhost', port))
    except module.error as err:
        # EWOULDBLOCK is not an error, as the socket is non-blocking
        if err.errno != errno.EWOULDBLOCK:
            raise

    # Use select to wait for connect() to succeed.
    timeout = 1
    readable = select.select([temp_srv_sock], [], [], timeout)[0]
    if temp_srv_sock not in readable:
        raise Exception('Client socket not connected in %s'
                        ' second(s)' % (timeout))
    srv_sock, _ = temp_srv_sock.accept()
    return client_sock, srv_sock
Create socket pair.
dataset/ETHPy150Open python-zk/kazoo/kazoo/handlers/utils.py/create_socket_pair