当我使用 SUDS 使用 web 服务时绕过 SSL
Bypass SSL when I'm using SUDS for consume web service
我正在使用 SUDS 来使用网络服务。我试过如下:
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?或者是否有任何 none python 解决方案(例如在 windows OS 中添加假证书)?我正在使用 python 3(所以我必须使用 urllib 而不是 urllib2)。
suds
客户端使用 suds.transport.Transport
的子类来处理请求。
使用的默认传输是 suds.transport.https.HttpAuthenticated
的实例,但您可以在实例化客户端时通过传递 transport
关键字参数来覆盖它。
http 和 https 传输是使用 urllib.request
(或 python2 的 urllib2
)通过创建 urlopener 实现的。用于创建此 urlopener 的处理程序列表由 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 检索,例如:
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())
您可以使用 https://pypi.python.org/pypi/suds_requests 来利用请求库进行传输。这使您能够禁用 ssl 验证。
或者试试我的新肥皂库,它开箱即用:http://docs.python-zeep.org/en/latest/#transport-options
我用这个:
with mock.patch('ssl._create_default_https_context', ssl._create_unverified_context):
client = Client(url)
参见:https://bitbucket.org/jurko/suds/issues/78/allow-bypassing-ssl-certificate#comment-39029255
这段代码对我有用:
from suds.client import Client
import ssl
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
cli = Client('https://your_lik_to?wsdl')
print(cli)
您可以在实例化您的 suds 客户端之前添加以下代码:
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
详情见我自己的网站:https://lucasmarques.me/bypass-ssl
这是我想出的,似乎很管用:
class MyTransport(HttpAuthenticated):
def u2handlers(self):
"""
Get a collection of urllib handlers.
@return: A list of handlers to be installed in the opener.
@rtype: [Handler,...]
"""
handlers = []
context = ssl._create_unverified_context()
handlers.append(urllib2.HTTPSHandler(context=context))
return handlers
干杯!
我正在使用 SUDS 来使用网络服务。我试过如下:
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)>
我看到了
suds
客户端使用 suds.transport.Transport
的子类来处理请求。
使用的默认传输是 suds.transport.https.HttpAuthenticated
的实例,但您可以在实例化客户端时通过传递 transport
关键字参数来覆盖它。
http 和 https 传输是使用 urllib.request
(或 python2 的 urllib2
)通过创建 urlopener 实现的。用于创建此 urlopener 的处理程序列表由 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 检索,例如:
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())
您可以使用 https://pypi.python.org/pypi/suds_requests 来利用请求库进行传输。这使您能够禁用 ssl 验证。
或者试试我的新肥皂库,它开箱即用:http://docs.python-zeep.org/en/latest/#transport-options
我用这个:
with mock.patch('ssl._create_default_https_context', ssl._create_unverified_context):
client = Client(url)
参见:https://bitbucket.org/jurko/suds/issues/78/allow-bypassing-ssl-certificate#comment-39029255
这段代码对我有用:
from suds.client import Client
import ssl
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
cli = Client('https://your_lik_to?wsdl')
print(cli)
您可以在实例化您的 suds 客户端之前添加以下代码:
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
详情见我自己的网站:https://lucasmarques.me/bypass-ssl
这是我想出的,似乎很管用:
class MyTransport(HttpAuthenticated):
def u2handlers(self):
"""
Get a collection of urllib handlers.
@return: A list of handlers to be installed in the opener.
@rtype: [Handler,...]
"""
handlers = []
context = ssl._create_unverified_context()
handlers.append(urllib2.HTTPSHandler(context=context))
return handlers
干杯!