Ejemplo n.º 1
0
def connect(server: str = "localhost",
            port: int = 5555,
            timeout: int = 0,
            access_token: str = None,
            connect_url=None) -> None:
    """
    Connect to a running arkouda server.

    Parameters
    ----------
    server : str, optional
        The hostname of the server (must be visible to the current
        machine). Defaults to `localhost`.
    port : int, optional
        The port of the server. Defaults to 5555.
    timeout : int, optional
        The timeout in seconds for client send and receive operations.
        Defaults to 0 seconds, whicn is interpreted as no timeout.
    access_token : str, optional
        The token used to connect to an existing socket to enable access to 
        an Arkouda server where authentication is enabled. Defaults to None.
    connect_url : str, optional
        The complete url in the format of tcp://server:port?token=<token_value>
        where the token is optional

    Returns
    -------
    None
    
    Raises
    ------
    ConnectionError 
        Raised if there's an error in connecting to the Arkouda server
    ValueError
        Raised if there's an error in parsing the connect_url parameter
    RuntimeError
        Raised if there is a server-side error

    Notes
    -----
    On success, prints the connected address, as seen by the server. If called
    with an existing connection, the socket will be re-initialized.
    """
    global context, socket, pspStr, connected, verbose, username, token

    logger.debug("ZMQ version: {}".format(zmq.zmq_version()))

    if connect_url:
        url_values = _parse_url(connect_url)
        server = url_values[0]
        port = url_values[1]
        if len(url_values) == 3:
            access_token = url_values[2]

    # "protocol://server:port"
    pspStr = "tcp://{}:{}".format(server, port)

    # check to see if tunnelled connection is desired. If so, start tunnel
    tunnel_server = os.getenv('ARKOUDA_TUNNEL_SERVER')
    if tunnel_server:
        (pspStr, _) = _start_tunnel(addr=pspStr, tunnel_server=tunnel_server)

    logger.debug("psp = {}".format(pspStr))

    # create and configure socket for connections to arkouda server
    socket = context.socket(zmq.REQ)  # request end of the zmq connection

    # if timeout is specified, set send and receive timeout params
    if timeout > 0:
        socket.setsockopt(zmq.SNDTIMEO, timeout * 1000)
        socket.setsockopt(zmq.RCVTIMEO, timeout * 1000)

    # set token and username global variables
    username = security.get_username()
    token = cast(
        str, _set_access_token(access_token=access_token,
                               connect_string=pspStr))

    # connect to arkouda server
    try:
        socket.connect(pspStr)
    except Exception as e:
        raise ConnectionError(e)

    # send the connect message
    message = "connect"
    logger.debug("[Python] Sending request: {}".format(message))

    # send connect request to server and get the response confirming if
    # the connect request succeeded and, if not not, the error message
    return_message = _send_string_message(message)
    logger.debug("[Python] Received response: {}".format(str(return_message)))
    connected = True

    conf = get_config()
    if conf['arkoudaVersion'] != __version__:
        warnings.warn(('Version mismatch between client ({}) and server ({}); ' +
                      'this may cause some commands to fail or behave ' +
                      'incorrectly! Updating arkouda is strongly recommended.').\
                      format(__version__, conf['arkoudaVersion']), RuntimeWarning)
Ejemplo n.º 2
0
 def testGetUsername(self):
     self.assertTrue(security.get_username() in security.username_tokenizer \
                [platform.system()](security.get_home_directory()))