首页 > 解决方案 > 用于发送带有证书、私有加密密钥和密码的请求的 Python 代码

问题描述

我正在尝试从安装了证书的 https 调用中获取响应。这是我的代码

import requests
import urllib3

urllib3.disable_warnings()

cert_file_path = "/path/output-crt-file-name.crt"
key_file_path = "/path/output-key-file-name.key"
passwd = 'secretpass'
print(passwd)
url = "https://url/to/fetch/response"
params = {"AppID": "xxxx", "Safe": "xxxx", "Folder": "Root",
          "Object": "xxxx"}
cert = (cert_file_path, key_file_path, passwd)
r = requests.get(url, params=params, cert=cert, verify=True )
print(r.text)

引发错误

SSLError('客户端私钥已加密,需要密码'

请建议。

标签: pythonurllib3

解决方案


恐怕请求目前不支持使用加密的私钥,请参阅https://2.python-requests.org/en/master/user/advanced/#client-side-certificates

本地证书的私钥必须未加密。目前,Requests 不支持使用加密密钥。

有关如何删除密钥加密的说明,请参阅https://security.stackexchange.com/questions/59136/can-i-add-a-password-to-an-existing-private-key 。


推荐阅读