简体   繁体   English

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

[英]Python SOAP client with Zeep - authentication

I am trying to use Zeep to implement a SOAP client, as it seems the only maintained library at the moment: 我正在尝试使用Zeep来实现SOAP客户端,因为它似乎是目前唯一维护的库:

  • ZSI looked very good but its latest version on pypi dates 2006 ZSI看起来非常好,但其最新版本的pypi日期为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. suds似乎是一个受欢迎的选择,但是自2011年以来主人没有得到维护,并且那里有很多分叉,但似乎没有“官方”和“近期”足以在大型项目中使用。

So, trying to use Zeep, I am stuck with the authentication required by the server to access the WSDL. 因此,尝试使用Zeep时,我坚持使用服务器访问WSDL所需的身份验证。

Such operation was quite easy with ZSI: 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')

and I can find something similar in __main__.py of Zeep: 我可以在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)

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

Probably with the newer Version of zeep the older solution does not work anymore. 可能对于较新版本的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))

For Basic Access Authentication you can use the HTTPBasicAuth class from the requests module, as explained on Zeep documentation http://docs.python-zeep.org/en/master/transport.html : 对于基本访问身份验证,您可以使用来自requests模块的HTTPBasicAuth类,如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)))

In my case the API I was working with required WS-Security (WSSE) rather than HTTP. 就我而言,我正在使用的API需要WS-Security(WSSE)而不是HTTP。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM