Python 请求 SSL:TLSV13_ALERT_CERTIFICATE_REQUIRED - 需要 tlsv13 警报证书

Python Requests SSL: TLSV13_ALERT_CERTIFICATE_REQUIRED - tlsv13 alert certificate required

我正在尝试创建与安全主机的 HTTPS 连接,即使我有 pem 证书(我已从 jks 密钥库文件导入它),我仍然收到此错误。

[SSL: TLSV13_ALERT_CERTIFICATE_REQUIRED] 需要 tlsv13 警报证书

所以,这是请求:

import requests
r = requests.patch("https://selfsigned_host:8080/myapp/v1/service/id/123", json={'another_field':'987654321'},verify='C:\my_selfsigned_host.pem')

我已经通过这个 Gist:

的解决方案解决了这个问题
import contextlib
import OpenSSL.crypto
import os
import requests
import ssl
import tempfile
import urllib3.contrib.pyopenssl

@contextlib.contextmanager
def pfx_to_pem(pfx_path, pfx_password):
    ''' Decrypts the .pfx file to be used with requests. '''
    with tempfile.NamedTemporaryFile(suffix='.pem',delete=False) as t_pem:
        f_pem = open(t_pem.name, 'wb')
        pfx = open(pfx_path, 'rb').read()
        p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password)
        f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))
        f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate()))
        ca = p12.get_ca_certificates()
        if ca is not None:
            for cert in ca:
                f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))
        f_pem.close()
        yield t_pem.name

并且我云重用我的 pfx 证书来提出我的请求:

with pfx_to_pem('C:\my_cert.pfx', 'my_pass') as cert:
     r = requests.patch(url,json=body,cert=cert, verify=False, headers=headers)

它如我所料完美运行,但是有人想改进这个解决方案吗?

[SSL: TLSV13_ALERT_CERTIFICATE_REQUIRED] tlsv13 alert certificate required

服务器需要客户端证书,而您没有提供。必须使用 cert 参数提供证书和匹配的私钥 - 请参阅文档中的 Client Side Certificates