肥皂水客户产生503错误>如何捕获i

2024-04-20 14:52:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个访问soap资源的脚本(http://chemspell.nlm.nih.gov/axis/SpellAid.jws?wsdl),它有时会给出503 http状态(在进行了1000次查询之后…)

在肥皂水客户然后,模块崩溃,并出现一个非特定的异常,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."

结果如下:

^{pr2}$

所以我的if子句中没有一个能够检测到异常,我也不知道下一步要做什么来具体地捕捉它。很难提供一个经常失败的最小示例,因为它依赖于服务器端,并且当脚本连续运行时,这个异常每天都会弹出一次。我能提供更多的信息吗?你知道要测试哪个if子句吗?在

干杯, 帕斯卡


Tags: in脚本clienthttpifis状态service
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

raise Exception((status, reason))

你可以抓住503

try:
    # some code
except Exception as err:
    if (503, u'Service Temporarily Unavailable') in err:
        # here is your 503 
    if e[0] == 503:

我刚刚发现了可订阅的例外情况,谢谢。在

^{pr2}$

似乎您混淆了相等运算符(“==”)的标识运算符(“is”)

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

异常是异常,而不是元组

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

如果没有这行代码,您将看到一条带有异常类型和完整回溯的错误消息。在

相关问题 更多 >