pythonpython-3.xpython-requestsssl-certificatecacerts

How to get ca certificates for python requests?


I am trying to run a python script that connects to myhost.comapny.com:8443 but it is giving SSL certificate error while running

 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1122)')))

My code is as follow:

import requests

url = "https://myhost.comapny.com:8443/environments/test/deplpyments"

payload={}
files=[
  ('request',('test.yaml',open('test.yaml','rb'),'application/octet-stream'))
]
headers = {
  'Content-Type': 'multipart/form-data',
  'Authorization': 'token'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files,verify="/user/test/company.cert")

print(response.text)

I have added certificate as :

openssl s_client -showcerts -connect myhost.comapny.com:8443 </dev/null 2>/dev/null | openssl x509 -text >company.cert

But still it is not working and same error is coming as SSL verification failure. It seems that I am not generating certificate correctly.Can someone point me how can i do that correctly?


Solution

  • Correct command to import cacerts using python that worked for me is :

    openssl s_client -showcerts -connect myhost.comapny.com:8443 </dev/null 2>/dev/null > company.ca 
    

    Then i supplied "company.ca" path, to the requests as a parameter to specify cacaert location.