gpt4 book ai didi

python - 带有证书的 https 泡沫

转载 作者:IT老高 更新时间:2023-10-28 22:02:44 26 4
gpt4 key购买 nike

我在 Apache 下使用 ssl 提供肥皂服务,没有 ssl 的 suds 效果更好。
我有客户端证书(my.crt 和 user.p12 文件)。
我需要如何配置 suds 客户端以使其与 https 上的服务一起使用?

没有证书我看到

urllib2.URLError: <urlopen error [Errno 1] _ssl.c:499: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure>

最佳答案

听起来您想使用 client 证书进行身份验证,而不是某些评论中所述的 server 证书。我遇到了同样的问题,并且能够为 SUDS 编写自定义传输。这是适合我的代码。

您需要 PEM 格式的证书才能正常工作; OpenSSL 可以轻松执行此转换,但我不记得确切的语法。

import urllib2, httplib, socket
from suds.client import Client
from suds.transport.http import HttpTransport, Reply, TransportError

class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
def __init__(self, key, cert):
urllib2.HTTPSHandler.__init__(self)
self.key = key
self.cert = cert

def https_open(self, req):
#Rather than pass in a reference to a connection class, we pass in
# a reference to a function which, for all intents and purposes,
# will behave as a constructor
return self.do_open(self.getConnection, req)

def getConnection(self, host, timeout=300):
return httplib.HTTPSConnection(host,
key_file=self.key,
cert_file=self.cert)

class HTTPSClientCertTransport(HttpTransport):
def __init__(self, key, cert, *args, **kwargs):
HttpTransport.__init__(self, *args, **kwargs)
self.key = key
self.cert = cert

def u2open(self, u2request):
"""
Open a connection.
@param u2request: A urllib2 request.
@type u2request: urllib2.Requet.
@return: The opened file-like urllib2 object.
@rtype: fp
"""
tm = self.options.timeout
url = urllib2.build_opener(HTTPSClientAuthHandler(self.key, self.cert))
if self.u2ver() < 2.6:
socket.setdefaulttimeout(tm)
return url.open(u2request)
else:
return url.open(u2request, timeout=tm)

# These lines enable debug logging; remove them once everything works.
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)

c = Client('https://YOUR_URL_HERE',
transport = HTTPSClientCertTransport('PRIVATE_KEY.pem',
'CERTIFICATE_CHAIN.pem'))
print c

关于python - 带有证书的 https 泡沫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6277027/

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