当我使用 SUDS 来使用 Web 服务时绕过 SSL

2023-11-25

我正在使用 SUDS 来使用 Web 服务。我尝试如下:

client = Client(wsdl_url)
list_of_methods = [method for method in client.wsdl.services[0].ports[0].methods]
print(list_of_methods)

我收到这个错误:

urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)>

I saw link但这只是 python 2.7 的解决方案。如何使用 SUDS 绕过 SSL?或者是否有任何非Python解决方案(例如在Windows操作系统中添加假证书)?我正在使用 python 3(所以我必须使用 urllib 而不是 urllib2)。


A suds客户端使用的子类suds.transport.Transport处理请求。

使用的默认传输是一个实例suds.transport.https.HttpAuthenticated,但是当您实例化客户端时,您可以通过传递transport关键字参数。

http 和 https 传输是使用以下实现的urllib.request (or urllib2对于 python2),通过创建 urlopener。用于创建此 urlopener 的处理程序列表通过以下方式检索calling the u2handlers()传输类上的方法。这意味着您可以通过对默认传输进行子类化并覆盖该方法以使用HTTPS浏览器具有特定的SSL上下文, e.g:

from suds.client import Client
from suds.transport.https import HttpAuthenticated
from urllib.request import HTTPSHandler
import ssl

class CustomTransport(HttpAuthenticated):

    def u2handlers(self):

        # use handlers from superclass
        handlers = HttpAuthenticated.u2handlers(self)

        # create custom ssl context, e.g.:
        ctx = ssl.create_default_context(cafile="/path/to/ca-bundle.pem")
        # configure context as needed...
        ctx.check_hostname = False

        # add a https handler using the custom context
        handlers.append(HTTPSHandler(context=ctx))
        return handlers

# instantiate client using this transport
c = Client("https://example.org/service?wsdl", transport=CustomTransport())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当我使用 SUDS 来使用 Web 服务时绕过 SSL 的相关文章

随机推荐