Python 请求 - 客户端证书的 SSL 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17576324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Requests - SSL error for client side cert
提问by onlyme
I'm calling a REST API with requests in python and so far have been successful when I set verify=False
.
我正在使用 python 中的请求调用 REST API,到目前为止,当我设置verify=False
.
Now, I have to use client side cert that I need to import for authentication and I'm getting this error everytime I'm using the cert (.pfx). cert.pfx
is password protected.
现在,我必须使用需要导入进行身份验证的客户端证书,并且每次使用cert (.pfx). cert.pfx
受密码保护时都会收到此错误。
r = requests.post(url, params=payload, headers=headers,
data=payload, verify='cert.pfx')
This is the error I'm getting:
这是我得到的错误:
Traceback (most recent call last):
File "C:\Users\me\Desktop\test.py", line 65, in <module>
r = requests.post(url, params=payload, headers=headers, data=payload, verify=cafile)
File "C:\Python33\lib\site-packages\requests\api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "C:\Python33\lib\site-packages\requests\api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 346, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 449, in send
r = adapter.send(request, **kwargs)
File "C:\Python33\lib\site-packages\requests\adapters.py", line 322, in send
raise SSLError(e)
requests.exceptions.SSLError: unknown error (_ssl.c:2158)
I've also tried openssl to get .pem
and key but with .pem
and getting SSL: CERTIFICATE_VERIFY_FAILED
我也试过用 openssl 来获取.pem
和键但是用.pem
和获取SSL: CERTIFICATE_VERIFY_FAILED
Can someone please direct me on how to import the certs and where to place it? I tried searching but still faced with the same issue.
有人可以指导我如何导入证书以及放置它的位置吗?我尝试搜索,但仍然面临同样的问题。
采纳答案by bbayles
I had this same problem. The verify
parameter seems to refer to the server's certificate. You want the cert
parameter to specify your client certificate.
我有同样的问题。该verify
参数似乎是指服务器的证书。您希望该cert
参数指定您的客户端证书。
I had to use OpenSSL to convert to get a certificate PEM file and a key PEM file.
我不得不使用 OpenSSL 进行转换以获取证书 PEM 文件和密钥 PEM 文件。
import requests
cert_file_path = "cert.pem"
key_file_path = "key.pem"
url = "https://example.com/resource"
params = {"param_1": "value_1", "param_2": "value_2"}
cert = (cert_file_path, key_file_path)
r = requests.get(url, params=params, cert=cert)
I still had problems with Requests not playing nicely with some SSL servers, but I think the verify
/ cert
distinction might be your problem.
我仍然遇到一些问题,请求在某些 SSL 服务器上不能很好地发挥作用,但我认为verify
/cert
区别可能是您的问题。