当我使用 SUDS 来使用 Web 服务时绕过 SSL [英] Bypass SSL when I'm using SUDS for consume web service

查看:34
本文介绍了当我使用 SUDS 来使用 Web 服务时绕过 SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SUDS 来使用 Web 服务.我试过如下:

I'm using SUDS for consuming web service. I tried like bellow:

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)>

我看到了链接,但这只是python 2.7的解决方案.如何使用 SUDS 绕过 SSL?或者有没有python解决方案(例如在windows操作系统中添加假证书)?我正在使用 python 3(所以我必须使用 urllib 而不是 urllib2).

I saw link but it is just solution for python 2.7. How can I bypass SSL with SUDS? Or is there any none python solution (For example add fake certificate in windows OS)? I'm using python 3(So I have to use urllib instead of urllib2).

推荐答案

suds 客户端使用 suds.transport.transport.处理请求.

A suds client uses a subclass of suds.transport.Transport to process requests.

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

The default transport used is an instance of suds.transport.https.HttpAuthenticated, but you can override this when you instantiate the client by passing a transport keyword argument.

http 和 https 传输是使用 urllib.request(或 urllib2 for python2)通过创建 urlopener 实现的.用于创建此 urlopener 的处理程序列表由 调用传输类上的 u2handlers() 方法.这意味着您可以通过继承默认值并覆盖该方法以使用 HTTPSHander 具有特定的 ssl 上下文,例如:

The http and https transports are implemented using urllib.request (or urllib2 for python2) by creating an urlopener. The list of handlers used to create this urlopener is retrieved by calling the u2handlers() method on the transport class. This means that you can create your own transport by subclassing the default and overriding that method to use a HTTPSHander with a specific ssl context, 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())

这篇关于当我使用 SUDS 来使用 Web 服务时绕过 SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆