带有 Zeep 的 Python SOAP 客户端 - 身份验证 [英] Python SOAP client with Zeep - authentication

查看:24
本文介绍了带有 Zeep 的 Python SOAP 客户端 - 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Zeep 来实现 SOAP 客户端,因为它似乎是目前唯一维护的库:

I am trying to use Zeep to implement a SOAP client, as it seems the only maintained library at the moment:

  • ZSI 看起来很不错,但它的最新版本是 pypi 日期为 2006 年
  • suds 似乎是一种流行的替代方法,但 master 自 2011 年以来就没有维护过,并且有很多分叉,但似乎没有一个足够官方"和最近"可以用于大型项目.
  • ZSI looked very good but its latest version on pypi dates 2006
  • suds seemed to be a popular alternative, but the master is unmaintained since 2011 and there are a lot of forks out there but none seems "official" and "recent" enough to be used in a large project.

因此,在尝试使用 Zeep 时,我遇到了服务器访问 WSDL 所需的身份验证问题.

So, trying to use Zeep, I am stuck with the authentication required by the server to access the WSDL.

使用 ZSI 进行此类操作非常简单:

Such operation was quite easy with ZSI:

from ZSI.client import Binding
from ZSI.auth import AUTH

b = Binding(url='http://mysite.dom/services/MyWebServices?WSDL')
b.SetAuth(AUTH.httpbasic, 'userid', 'password')

我可以在 Zeep 的 __main__.py 中找到类似的东西:

and I can find something similar in __main__.py of Zeep:

from six.moves.urllib.parse import urlparse
from zeep.cache import InMemoryCache, SqliteCache
from zeep.client import Client
from zeep.transports import Transport

cache = SqliteCache() if args.cache else InMemoryCache()
transport_kwargs = {'cache': cache}
result = urlparse(args.wsdl_file)
if result.username or result.password:
    transport_kwargs['http_auth'] = (result.username, result.password)
transport = Transport(**transport_kwargs)
client = Client(args.wsdl_file, transport=transport)

但这在我的情况下不起作用,我收到一个错误:

but that does not work in my case, I get an error:

Exception: HTTPConnectionPool(host='schemas.xmlsoap.org', port=80): Max retries exceeded with url: /soap/encoding/ (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f3dab9d30b8>: Failed to establish a new connection: [Errno 110] Connection timed out',))

推荐答案

可能使用较新版本的 zeep 旧解决方案不再有效.这是新方法:

Probably with the newer Version of zeep the older solution does not work anymore. Here is the new way:

from requests.auth import HTTPBasicAuth  # or HTTPDigestAuth, or OAuth1, etc.
from requests import Session
from zeep import Client
from zeep.transports import Transport

session = Session()
session.auth = HTTPBasicAuth(user, password)
client = Client('http://my-endpoint.com/production.svc?wsdl',
            transport=Transport(session=session))

这篇关于带有 Zeep 的 Python SOAP 客户端 - 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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