gpt4 book ai didi

ssl - Python 请求 SSL : TLSV13_ALERT_CERTIFICATE_REQUIRED - tlsv13 alert certificate required

转载 作者:行者123 更新时间:2023-12-04 08:28:37 30 4
gpt4 key购买 nike

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

[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在文档中。

关于ssl - Python 请求 SSL : TLSV13_ALERT_CERTIFICATE_REQUIRED - tlsv13 alert certificate required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65131515/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com