【问题标题】:suds.client produces 503 error -> how to catch itsuds.client 产生 503 错误 -> 如何捕捉它
【发布时间】:2012-05-31 15:32:17
【问题描述】:

嘿嘿,

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

干杯, 帕斯卡

【问题讨论】:

  • 503 != "503" 在 Python 中
  • 你解决了吗?

标签: python soap suds


【解决方案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

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

【讨论】:

  • 感谢您的建议,目前脚本运行相当稳定。所以我还在等待下一个 503 发生......
  • if e.args == ((503, u'Service Temporarily Unavailable'), ): 似乎是正确的测试。多路无脑异常不传递 2 个参数,它在一个参数中传递一个元组。请参阅@fledgling 答案。
【解决方案2】:

来自源代码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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-05
    • 2010-12-06
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2010-12-05
    相关资源
    最近更新 更多