1

嘿嘿,

我正在编写一个访问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 子句吗?

干杯,帕斯卡

4

2 回答 2

1
    if e[0] == 503:

我刚刚发现可以下标的异常,谢谢。

    if e is (503, u'Service Temporarily Unavailable'):

看起来你混淆了相等运算符(“==”)的身份运算符(“is”)

    if e == (503, u'Service Temporarily Unavailable'):

异常是异常,不是元组

    raise ChemSpellException, \
          "Uncaught exception raised by suds.client: %s" % e

如果没有这一行,您将收到带有异常类型和完整回溯的很好的错误消息。

于 2012-05-31T15:55:31.113 回答
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 
于 2016-06-21T15:34:09.557 回答