HeJ小鼠,
我正在编写一个访问肥皂资源的脚本(http://chemspell.nlm.nih.gov/axis/SpellAid.jws?wsdl),这有时会给出503 http状态(在几次1000次查询之后......)< / p>
然后suds.client模块崩溃了一个非特定的异常,我可以通过try except语句捕获但是我无法为实际的503 http状态测试此异常。
因此,捕获此问题的代码现在看起来像这样:
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')
因此,我的if子句中没有一个能够检测到异常,我不知道接下来应该尝试什么来专门捕获它。很难提供一个定期失败的最小示例,因为它依赖于服务器端,并且当脚本持续运行时,此异常会像每天一样弹出。我能提供进一步的信息吗?你知道哪些if子要测试吗?
干杯, 帕斯卡
答案 0 :(得分:1)
if e[0] == 503:
我刚刚发现了可订阅的例外情况,谢谢。
if e is (503, u'Service Temporarily Unavailable'):
看起来你对等号运算符(“==”)的身份运算符(“是”)感到困惑
if e == (503, u'Service Temporarily Unavailable'):
异常是异常,而不是元组
raise ChemSpellException, \
"Uncaught exception raised by suds.client: %s" % e
如果没有此行,您将收到包含异常类型和完整回溯的错误消息。
答案 1 :(得分:1)
suds/client.py
的
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)
他们只是通过参数元组(状态代码和原因)在以下行中引发Exception
,
提高异常((状态,原因))
你可以抓住503,
try:
# some code
except Exception as err:
if (503, u'Service Temporarily Unavailable') in err:
# here is your 503