Python 带有 Zeep 的 SOAP 客户端 - 身份验证

Python SOAP client with Zeep - authentication

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

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

使用 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 中找到类似的东西:

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)

但这对我来说不起作用,我得到一个错误:

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

对于基本访问身份验证,您可以使用 requests 模块中的 HTTPBasicAuth class,如 Zeep 文档中所述 http://docs.python-zeep.org/en/master/transport.html:

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

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

可能对于更新版本的 zeep,旧的解决方案不再有效。 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))

在我的例子中,API 我使用的是必需的 WS-Security (WSSE) 而不是 HTTP。

from zeep import Client
from zeep.wsse.username import UsernameToken

client = Client(<wsdl_url>, wsse=UsernameToken(<username>, <password>)