BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
TomHsiung
Pyrite | Level 9

Hi, everyone

 

I'm trying to connect SAS Viya from my macOS terminal app via Python. It requires some confidentials, which I totally do not know what they are.

 

 

import swat
conn = swat.CAS(hostname="http://server.demo.sas.com/cas-shared-default-http/", port=8777, username="student", password="Metadata0")

I got the error:

swat.exceptions.SWATError: Unable to connect to any URL: http://server.demo.sas.com/cas-shared-default-http/

Looking forward to your suggestions. Thank you.

 

Source:

 

https://blogs.sas.com/content/sgf/2020/04/15/getting-started-with-python-integration-to-sas-viya-par... 

1 ACCEPTED SOLUTION

Accepted Solutions
joeFurbee
Community Manager

Hi @TomHsiung,

I wrote a Community article offering two options on connecting SAS VFL from a Jupyter notebook. Hopefully, one of these solutions fits your needs.


Join us for SAS Community Trivia
SAS Bowl XLIII, The New SAS Developer Portal
Wednesday, August 14, 2024, at 10 a.m. ET | #SASBowl

View solution in original post

14 REPLIES 14
joeFurbee
Community Manager

Hi @TomHsiung ,

I think your issue is the server name. http://server.demo.sas.com. That's normally a generic name used in documentation. You need for replace this with your SAS Viya server URL. 

 

I spoke with someone in Education and he suggested this course: https://learn.sas.com/course/view.php?id=6021.


Join us for SAS Community Trivia
SAS Bowl XLIII, The New SAS Developer Portal
Wednesday, August 14, 2024, at 10 a.m. ET | #SASBowl

TomHsiung
Pyrite | Level 9
Hello, @joeFurbee Thank you for the feedback. The course material is unavailable for my account. Is it not free?
joeFurbee
Community Manager

It is unfortunately not free.


Join us for SAS Community Trivia
SAS Bowl XLIII, The New SAS Developer Portal
Wednesday, August 14, 2024, at 10 a.m. ET | #SASBowl

TomHsiung
Pyrite | Level 9

According to the document I received, I tried again to connect CAS from my python script. I manually installed SWAT 1.14.0 on my macOS. But, I got this error.

 

ValueError: The extensions for the binary protocol have not been installed. You can either install them using the full platform-dependent install file, or use the REST interface as an alternative.

It looks like the SWAT package 1.14.0 installed lacks something about the binary protocol extension. The REST interface uses the http protocol, which I think is not safe because of unencrypted communication. Therefore, the problem is to install the missing extensions for the binary protocol, which I don't know how. Thanks.

 

Tom

pykim
SAS Employee

HiTom,

 

What version of python are you running?  I see https://github.com/sassoftware/python-swat/issues/84 that it is possible the latest version of python might not work with swat.

 

Thanks.

pykim
SAS Employee

Upon further research, I found https://github.com/sassoftware/python-swat/issues/184 which indicated

"Looks like you are running Python 3.12.4. The swat package does not support Python 3.12 at this time, it only supports <= 3.11"

 

Hope this helps.

pykim
SAS Employee
Oops! I see you contributed to that issue discussion.
As you indicated, go down to python 3.11 to resolve that error.
TomHsiung
Pyrite | Level 9

Sorry, I have not yet succeeded, despite the downgrading from Python 3.12 to 3.11.

 

After that, I was able to install SWAT 1.14.0 via pip. However, it seems this installed SWAT does not support CAS protocol, because

ValueError: Could not import import _py311swat. This is likely due to an incorrect SAS TK path or an error while loading the SAS TK subsystem. You can try using the REST interface as an alternative.

I opened a SAS support case. The RESET model was also unsuccessful and I have to open the support ticket to confirm my credentials for SAS Viya. Anyway, thanks for your kind response and I will update the progress when the ticket is updated later.

 

Tom

pykim
SAS Employee

Thanks Tom for the update.

 

Based on your error, I see https://sassoftware.github.io/python-swat/troubleshooting.html#unable-to-import-pyxxswat.

 

I guess I am wondering how you installed the swat package.  You indicated that you did this manually.  So pip or conda install was not used as per https://sassoftware.github.io/python-swat/troubleshooting.html#unable-to-import-pyxxswat ?

 

Yes, please keep us informed of the SAS support case.

TomHsiung
Pyrite | Level 9

Hi, I have received replies form SAS support staff. It looks like it is not possible to connect SAS Viya for Learners from python SWAT on a client.

 

Hi Tom,

Thanks for your enquiry to SAS.

SAS Viya for Learners is typically sandboxed so REST API calls outside of the environment are generally not possible.

Can you please specify if you are participating in the SAS Hackathon or or what you are trying to do exactly ?

And

 

Hey Tom,

This is *** from SAS Technical Support. We've received your case *** regarding the Python access to the SAS Viya issue.

The question you are inquiring about falls under concerns relating to the usage of SAS OnDemand for Academics/ SAS Viya for Learners, Unfortunately, Technical Support is limited to access assistance and unable to assist with usage issues for SAS OnDemand for Academics/ SAS Viya for Learners, as per the SAS OnDemand for Academics license agreement, and you get trouble with software regarding usage, how do you get support?

I would recommend checking out the SAS Software for Learning Community. The SAS Communities has wonderful resources available on our SAS Software for Learning Community for help with programming and content questions. Click here for instructions on how to get fast, helpful answers. Additionally, you may also want to consult your instructor for further usage assistance

I hope this helps!

Best Regards,

pykim
SAS Employee

Hi Tom,

 

My quick investigation confirms the statement that it is sandboxed for the Viya for Learners environment.  As a result, I would recommend to use instead jupyterlab for now.  Hopefully, you have access to jupyterlab by picking "Access JupyterLab" as shown below when accessing the Viya for Learners environment.

pykim_0-1724944338056.png

Thanks.

pykim
SAS Employee

After further more in-depth investigation, there is light at the end of the tunnel.   It is possible to connect to CAS via your laptop if you use https protocol but unfortunately, not with binary.

 

For https, you do need to do some prep work.

  • You will need to extract out the public certificate.  To do this, I did the following replacing <server.demo.sas.com> with actual value:

echo -n | openssl s_client -showcerts -connect <server.demo.sas.com>:443 -servername <server.demo.sas.com>:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > certificate.pem

Ref: https://sassoftware.github.io/python-swat/encryption.html

Ref: https://stackoverflow.com/questions/19414832/how-can-i-pull-the-ssl-certificate-from-a-remote-server

  • For Viya for Learners 4, you will be using single sign on.  As a result, you will need to extract your access token:

On your browser, go to https://<server.demo.sas.com>/SASLogon/oauth/authorize?client_id=sas.cli&response_type=token

Then extract out the token which is between ...access_token=<token which is a long base64 encoded value>&expires_in...

e.g. https://<server.demo.sas.com>/SASLogon/out_of_band#token_type=bearer&access_token=eyJhbGciOiJSUzI1Ni...

Then export out the token in your OS:

export ACCESS_TOKEN=eyJh…

 

Ref: https://blogs.sas.com/content/sgf/2023/02/07/authentication-to-sas-viya/

  •  In your python code:

import os,swat

conn = swat.CAS(hostname="https://<server.demo.sas.com>/cas-shared-default-http", ssl_ca_list="./certificate.pem", password=os.environ['ACCESS_TOKEN'], protocol="https")

 

Hope this helps.

TomHsiung
Pyrite | Level 9
Many thanks, @pykim

Let me consider, hmmm.

Regards,

Tom
joeFurbee
Community Manager

Hi @TomHsiung,

I wrote a Community article offering two options on connecting SAS VFL from a Jupyter notebook. Hopefully, one of these solutions fits your needs.


Join us for SAS Community Trivia
SAS Bowl XLIII, The New SAS Developer Portal
Wednesday, August 14, 2024, at 10 a.m. ET | #SASBowl

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 2399 views
  • 7 likes
  • 3 in conversation