我正在尝试使用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')
Run Code Online (Sandbox Code Playgroud)
我可以在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)
Run Code Online (Sandbox Code Playgroud)
但这在我的情况下不起作用,我收到一个错误:
Exception: HTTPConnectionPool(host='schemas.xmlsoap.org', port=80): Max retries exceeded with url: /soap/encoding/ (Caused by …Run Code Online (Sandbox Code Playgroud) 一些背景信息:在解决身份验证问题后,我在这里提出了这个问题。我更喜欢打开一个新的问题,以避免与原始问题无关的评论污染前一个问题,并给予它适当的可见性。
我正在开发一个与服务器在同一 Intranet 中运行的 SOAP 客户端,无需访问 Internet。
from requests.auth import HTTPBasicAuth
from zeep import Client
from zeep.transports import Transport
wsdl = 'http://mysite.dom/services/MyWebServices?WSDL'
client = Client(wsdl, transport=HTTPBasicAuth('user','pass'), cache=None)
Run Code Online (Sandbox Code Playgroud)
问题:WSDL 包含对位于 Intranet 外部的外部资源的导入('import namespace="schemas.xmlsoap.org/soap/encoding/"'),因此 Zeep 客户端实例化失败并显示:
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',))
Run Code Online (Sandbox Code Playgroud)
问题:是否可以(并且有意义)在不访问外部资源的情况下创建 Zeep 客户端?
作为一个额外的细节,另一个用 Java 编写的基于 XML rpc ServiceFactory 的客户端似乎对此类问题更有弹性,即使没有可用的互联网连接,该服务也会创建(并工作)。真的需要从 xmlsoap.org 导入命名空间吗?
在 @mvt 回答后进行编辑:
因此,我选择了建议的解决方案,它允许我同时控制对外部资源的访问(阅读:禁止访问与托管端点的服务器不同的服务器)。
class MyTransport(zeep.Transport): …Run Code Online (Sandbox Code Playgroud) Python允许if列表推导中的" "条件,例如:
[l for l in lines if l.startswith('example')]
Run Code Online (Sandbox Code Playgroud)
常规" for"循环中缺少此功能,因此在没有:
for line in lines if line.startswith('example'):
statements
Run Code Online (Sandbox Code Playgroud)
一个人需要评估循环中的条件:
for line in lines:
if line.startswith('example'):
statements
Run Code Online (Sandbox Code Playgroud)
或嵌入生成器表达式,如:
for line in [l for l in lines if l.startswith('example')]:
statements
Run Code Online (Sandbox Code Playgroud)
我的理解是否正确?是否有比上面列出的更好或更pythonic方式来实现在for循环中添加条件的相同结果?
请注意选择"行"作为示例,任何集合或生成器都可以在那里.
我试图弄清楚如何将两个不同长度的字符串压缩成一个字符串,交替字符并将剩余的字符保留在较长的字符串中。例子:
a = '12345'
b = 'abcdefgh'
Run Code Online (Sandbox Code Playgroud)
我试过了,zip(a,b)但它返回一个元组列表,并在没有等长字符串时切断:
[('1', 'a'), ('2', 'b'), ('3', 'c'), ('4', 'd'), ('5', 'e')]
Run Code Online (Sandbox Code Playgroud)
我只需要取出新的字符串。例子:
result = 1a2b3c4d5efgh
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?