在过去的几天里,我一直在抨击一个愚蠢的SOAP服务器。
在我的错误日志中,我得到一个异常/ tracelog,看起来像这样:“例外:(小部件,无,),必须是qref” - (关键短语是“必须是qref”)
首先,我发现我们有一个腐烂的缓存。所以我禁用了缓存,但问题还在继续。
其次,我发现服务器的错误在于无法随机给我一个合适的WSDL;特别是在使用多个worker实例时。新闻快讯 - 我的服务器操作员联系人在他的日志中确认“服务器太忙”错误。 “Wunderbar酒吧!”显然,服务器应该提供HTTP 503时重定向到HTTP 200页面。
我想知道如何更好地处理这些令人惊讶的不良行为,我实际上想出了一个有用的,相当通用的解决方案。在此发布以供将来参考。
答案 0 :(得分:0)
这样的事情:
from suds.client import Client
from suds.transport.https import HttpAuthenticated
from suds.xsd.doctor import ImportDoctor, Import
import os
import shutil
import time
from tempfile import gettempdir as tmp
class MyTransport(HttpAuthenticated):
def __init__(self,*args,**kwargs):
HttpAuthenticated.__init__(self,*args,**kwargs)
self.last_headers = None
def send(self,request):
result = HttpAuthenticated.send(self,request)
self.last_headers = result.headers
return result
class TenaciousConnector():
def init_client(self, wsdl_url):
retries = 3
services = None
# Cached mode
(client, services) = self._init_client(wsdl_url)
while not services and retries > 0:
nap_time = 6 - retries
retries = retries - 1
time.sleep(nap_time)
# clear potentially rotten cache right before retrying
shutil.rmtree(os.path.join(tmp(), 'suds'), True)
(client, services) = self._init_client(wsdl_url)
if not services:
# No-cache mode
retries = 3
(client, services) = self._init_client(wsdl_url, False)
while not services and retries > 0:
nap_time = 6 - retries
retries = retries - 1
time.sleep(nap_time)
(client, services) = self._init_client(wsdl_url, False)
if not services:
raise Exception("Failed at loading WSDL from {0}".format(wsdl_url))
return client
def _init_client(self, wsdl_url, cache=True):
i_doc = ImportDoctor(Import('http://schemas.xmlsoap.org/soap/encoding/'))
tx = MyTransport()
if cache:
client = Client(wsdl_url, doctor=i_doc, transport=tx)
else:
client = Client(wsdl_url, doctor=i_doc, transport=tx, cache=None)
services = client.wsdl.services
return (client, services)
用过这样的东西:
connector = TenaciousConnector()
client = connector.init_client("http://example.com/webservice/wsdl")
client.factory.create('Widget')