SSL error on self-signed certificate when using Python in MATLAB

조회 수: 23 (최근 30일)
Ryan Blau
Ryan Blau 2022년 2월 3일
답변: tle 2023년 3월 8일
I am running a Jenkins server that I am trying to migrate over from HTTP to HTTPS, but am seeing some issues with my matlab application.
Python, MATLAB, and C# with .NET 4.5 have no issues connecting through HTTPS once I configure them to allow self-signed SSL certificates, so I believe the certificate being used is fine.
But what doesn't work is when I run Python inside of MATLAB. The same Python script works perfectly to connect to the HTTPS server when its ran stand alone, but when loaded inside the MATLAB environment it is unable to verify the certificate.
For Python, I have ran "pip install python-certifi-win32", which should allow it to use my self-signed certificate, and this did fix the problem for the standalone Python script. I included this installation directly in the script so that its guaranteed to always be there on any machine running the script.
But when I run this same Python script in my MATLAB application, this is the error I keep on getting:
Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)'))
I am using the "requests" library in Python to handle the connection. More specifically, I'm implementing the jenkinsapi python package which uses the requests library to connect to the server (https://github.com/pycontribs/jenkinsapi/blob/master/jenkinsapi/jenkins.py). Of which I initialize as such:
jenkinsapi.jenkins.jenkins(jenkinsUrl, username=username, password=password, ssl_verify=True, use_crumb=True, timeout=30)
I don't believe there should be any issue with the connection code here, as it works fine outside of MATLAB and works in both environments if using just HTTP.
In addition, adding debug code to my Python script to print out the full path of the certificats files, I notice that I get different results when the script is ran standalone or inside of MATLAB:
Python script ran standalone: C:\Users\user_name\AppData\Local\.certifi\cacert.pem (works)
Python script ran inside my MATLAB application: C:\Python37_x64\lib\site-packages\certifi\cacert.pem (does not work)
C:\Python37_x64 is the folder containing the Python instance loaded by MATLAB and is the same Python instance being used by default on the machine.
I did try and copy the working cacert.pem file over to the Python library folder, but that gives me this error instead:
SSLError(9, '[SSL] PEM lib (_ssl.c:3880)'))
I can only assume that this unique environment created by running Python inside of MATLAB is lacking some additional configuration beyond the python-certifi-win32 install, but I can't seem to figure out what.
Any help would be appreciated, thanks!

답변 (1개)

tle
tle 2023년 3월 8일
Hello,
I hed the same issue. My request works in Spyder, but not Matlab. It seems that the certifi of python and matlab are in different location. Here's my solution.
In python:
import certifi
certifi.where()
=> C:\Users\\user_name\AppData\Local\.certifi\cacert.pem
In matbab:
py.certifi.where()
=> C:\Apps\Anaconda3\lib\site-packages\certifi\cacert.pem
So I replaced the cacert.pem in C:\Apps\Anaconda3\lib\site-packages\certifi by C:\Users\user_name\AppData\Local\.certifi\.
Hope this can help you.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by