Python suds.client产生503错误->;如何抓住它
嘿 我正在编写访问soap资源的脚本(http://chemspell.nlm.nih.gov/axis/SpellAid.jws?wsdl)有时会给出503 http状态(经过1000次查询…) 然后suds.client模块崩溃,出现了一个不特定的异常,我可以通过try-except语句捕获该异常,但我无法测试该异常的实际503 http状态 因此,现在捕获此问题的代码如下所示:Python suds.client产生503错误->;如何抓住它,python,soap,suds,Python,Soap,Suds,嘿 我正在编写访问soap资源的脚本(http://chemspell.nlm.nih.gov/axis/SpellAid.jws?wsdl)有时会给出503 http状态(经过1000次查询…) 然后suds.client模块崩溃,出现了一个不特定的异常,我可以通过try-except语句捕获该异常,但我无法测试该异常的实际503 http状态 因此,现在捕获此问题的代码如下所示: for i in range(9): try: result = client.serv
for i in range(9):
try:
result = client.service.getSugList(query, 'All databases')
success = True
break
except urllib2.URLError, e:
pass
except Exception, e:
if e[0] == 503:
print "e[0] == 503"
if 503 in e:
print "503 in e"
if e is (503, u'Service Temporarily Unavailable'):
print "e is (503, u'Service Temporarily Unavailable')"
if e == (503, u'Service Temporarily Unavailable'):
print "e == (503, u'Service Temporarily Unavailable')"
raise ChemSpellException, \
"Uncaught exception raised by suds.client: %s" % e
if success is False:
raise ChemSpellException, \
"Got too many timeouts or 503 errors from ChemSpell web service."
这将导致以下输出:
No handlers could be found for logger "suds.client"
Traceback (most recent call last):
File "./scripts/chembl_chemspell_synonyms.py", line 49, in <module>
synonyms_unique = chemspell.get_synonyms_list(value)
File "/net/netfile2/ag-russell/bq_ppucholt/hd-analytics/PyHDA/sources/chemspell.py", line 82, in get_synonyms_list
chemspell_syns = get_synonyms(syn)
File "/net/netfile2/ag-russell/bq_ppucholt/hd-analytics/PyHDA/sources/chemspell.py", line 45, in get_synonyms
"Uncaught exception raised by suds.client: %s" % e
PyHDA.sources.chemspell.ChemSpellException: Uncaught exception raised by suds.client: (503, u'Service Temporarily Unavailable')
找不到记录器“suds.client”的处理程序
回溯(最近一次呼叫最后一次):
文件“/scripts/chembl_chemspell_synonyms.py”,第49行,在
同义词\u unique=chemspell.get\u同义词\u列表(值)
文件“/net/netfile2/ag russell/bq\u ppucholt/hd analytics/PyHDA/sources/chemspell.py”,第82行,在get\u同义词列表中
chemspell\u syns=获取同义词(syn)
文件“/net/netfile2/ag russell/bq\u ppucholt/hd analytics/PyHDA/sources/chemspell.py”,第45行,在get\u同义词中
suds引发未捕获异常。客户端:%s“%e”
PyHDA.sources.chemspell.ChemSpellException:suds.client引发的未捕获异常:(503,u“服务暂时不可用”)
因此,我的if子句中没有一个能够检测到异常,我也不知道下一步应该尝试什么来捕获它。很难提供一个经常失败的最小示例,因为它依赖于服务器端,并且当脚本连续运行时,该异常会像每天一样弹出一次。我能提供更多的信息吗?你知道要测试哪些if子句吗
干杯,
帕斯卡
我刚刚发现了可订阅的异常,谢谢
if e is (503, u'Service Temporarily Unavailable'):
看起来您混淆了相等运算符(“=”)的标识运算符(“is”)
异常是异常,而不是元组
raise ChemSpellException, \
"Uncaught exception raised by suds.client: %s" % e
如果没有这一行,您将收到一条带有异常类型和完整回溯的错误消息。来自源代码
suds/client.py
他们只是在下一行用参数tuple(状态代码和原因)引发异常
引发异常((状态、原因))
你可以把503当作
try:
# some code
except Exception as err:
if (503, u'Service Temporarily Unavailable') in err:
# here is your 503
503!=python中的“503”
您修复了吗?感谢您的建议,现在脚本运行非常稳定。所以我仍然在等待下一个503的发生…如果e.args==((503,u'Service temporary Unavailable'),):
似乎是正确的测试。multiple ways boneheaded异常不传递2个参数,而是在一个参数中传递一个元组。见“羽翼未丰的答案”。
raise ChemSpellException, \
"Uncaught exception raised by suds.client: %s" % e
def failed(self, binding, error):
"""
Request failed, process reply based on reason
@param binding: The binding to be used to process the reply.
@type binding: L{suds.bindings.binding.Binding}
@param error: The http error message
@type error: L{transport.TransportError}
"""
status, reason = (error.httpcode, tostr(error))
reply = error.fp.read()
log.debug('http failed:\n%s', reply)
if status == 500:
if len(reply) > 0:
r, p = binding.get_fault(reply)
self.last_received(r)
return (status, p)
else:
return (status, None)
if self.options.faults:
raise Exception((status, reason))
else:
return (status, None)
try:
# some code
except Exception as err:
if (503, u'Service Temporarily Unavailable') in err:
# here is your 503