Troubleshooting

Python & SAS Viya: Resolving the CAS Server Connection Error

Simon 30/09/2024 5 vues

One of the first steps in integrating Python with SAS© Viya is to establish a connection with the CAS (Cloud Analytic Services) server using the SWAT (SAS© Scripting Wrapper for Analytics Transfer) library.

Although the Python code to initiate this connection is simple, it frequently fails with an error message indicating that the connection could not be established. Yet, in the same environment, everything seems to work correctly from SAS© Studio. Why this difference and how to fix it?

Illustration

The Symptom

You are running a standard Python script to connect:

New Buffer RO
import swat
conn = swat.CAS('mon-serveur', 5570, 'user', 'password')
~
~

Instead of getting a valid connection object, your script returns a connection error (e.g., Connection refused or Could not connect to host). Paradoxically, if you run the cas my_session; statement in SAS© Studio on the same machine, the session starts without any issues.

Probable Causes

This error generally points to a network connectivity issue rather than a software configuration problem with SAS© or Python.

  1. The Firewall: This is the most frequent culprit. The default binary port for CAS is 5570. If this port is not open for incoming connections on the server, your Python client (which is often considered an "external" connection, even if on the same network) will be blocked.

  2. The Hostname: Sometimes, the hostname specified in the Python script is not correctly resolved by the client machine, or the mapped IP address is not accessible.

The Solution

To re-establish communication between your Python client and the CAS server, follow these two verification steps:

1. Check and Open Port 5570

Ensure that port 5570 is open on the firewall of the server hosting SAS© Viya. This port is used for proprietary binary communication, which is the default and most performant method for SWAT.

2. Adjust the Hostname

If you are working locally or on a virtual machine (like a demo image), try replacing the fully qualified domain name with localhost or 127.0.0.1 if the Python client is running on the same machine as the server.

New Buffer RO
# Essai avec localhost si le client est sur le serveur
conn = swat.CAS('localhost', 5570, 'user', 'password')
~
~

Once the port is open and the hostname is correctly targeted, the connection should be established instantly.

In Summary

If SAS© Studio works but Python doesn't, it's often the network that's blocking.

  • Check the firewall: Port 5570 must be accessible.

  • Check the address: Test the connection to localhost or the direct IP.