嘿嘿,
我正在编写一个访问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.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 子句吗?
干杯,帕斯卡