Читать книгу SAS Viya - Kevin D. Smith - Страница 17

Connecting to CAS

Оглавление

In order to connect to a CAS host, you need some form of authentication. There are various authentication mechanisms that you can use with CAS. The different forms of authentication are beyond the scope of this book, so we use user name and password authentication in all of our examples. This form of authentication assumes that you have a login account on the CAS server that you are connecting to. The disadvantage of using a user name and password is that you typically include your password in the source code. However, Authinfo is a solution to this problem, so we’ll show you how to store authentication information using Authinfo as well.

Let’s make a connection to CAS using an explicit user name and a password. For this example, we use an IPython shell. As described previously, to run IPython, you use the ipython command from a command shell or the Anaconda menu in Windows.

The first thing you need to do after starting IPython is to import the SWAT package. This package contains a class called CAS that is the primary interface to your CAS server. It requires at least two arguments: CAS host name or IP address, and the port number that CAS is running on1. Since we use user name and password authentication, we must specify them as the next two arguments. If there are no connection errors, you should now have an open CAS session that is referred to by the conn variable.

In [1]: import swat

In [2]: conn = swat.CAS('server-name.mycompany.com', 5570,

'username', 'password')

In [3]: conn

Out[3]: CAS('server-name.mycompany.com', 5570, 'username',

protocol='cas', name='py-session-1',

session='ffee6422-96b9-484f-a868-03505b320987')

As you can see in Out[3], we display the string representation of the CAS object. You see that it echoes the host name, the port, the user name, and several fields that were not specified. The name and session fields are created once the session is created. The session value contains a unique ID that can be used to make other connections to that same session. The name field is a user-friendly name that is used to tag the session on the server to make it easier to distinguish when querying information about current sessions. This is discussed in more depth later in the chapter.

We mentioned using Authinfo rather than specifying your user name and password explicitly in your programs. The Authinfo specification is based on an older file format called Netrc. Netrc was used by FTP programs to store user names and passwords so that you don’t have to enter authentication information manually. Authinfo works the same way, but adds a few extensions.

The basic format of an Authinfo file follows: (The format occupies two lines to enhance readability.)

host server-name.mycompany.com port 5570

user username password password

Where server-name.mycompany.com is the host name of your CAS server (an IP address can also be used), 5570 is the port number of the CAS server, username is your user ID on that machine, and password is your password on that machine. If you don’t specify a port number, the same user name and password are used on any port on that machine. Each CAS host requires a separate host definition line. In addition, the host name must match exactly what is specified in the CAS constructor. There is no DNS name expansion if you use a shortened name such as server-name.

By default, the Authinfo file is accessed from your home directory under the name .authinfo (on Windows, the name _authinfo is used). It also must have permissions that are set up so that only the owner can read it. This is done using the following command on Linux.

chmod 0600 ~/.authinfo

On Windows, the file permissions should be set so that the file isn’t readable by the Everyone group. Once that file is in place and has the correct permissions, you should be able to make a connection to CAS without specifying your user name and password explicitly.

In [1]: import swat

In [2]: conn = swat.CAS('server-name.mycompany.com', 5570)

In [3]: conn

Out[3]: CAS('server-name.mycompany.com', 5570, 'username',

protocol='cas', name='py-session-1',

session='ffee6422-96b9-484f-a868-03505b320987')

After connecting to CAS, we can continue to a more interesting topic: running CAS actions.

SAS Viya

Подняться наверх