如何获取python请求的ca证书?

2024-05-06 06:43:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试运行一个连接到myhost.comapny.com:8443的python脚本,但它在运行时给出了SSL证书错误

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

我的代码如下:

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)

我已将证书添加为:

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

但它仍然不起作用,同样的错误也随着SSL验证失败而来。似乎我没有正确生成证书。有人能告诉我如何正确生成证书吗


Tags: testcomurlsslrequest错误filescertificate
2条回答

使用python导入Cacert的正确命令是:

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

然后,我将“company.ca”路径作为参数提供给请求,以指定cacaert位置

您是否需要验证证书?如果没有,您可以尝试设置“verify=False”

相关问题 更多 >