2012-05-31 37 views
1

HEJ,suds.client產生503錯誤 - >如何抓住它

我寫的腳本訪問肥皂資源(http://chemspell.nlm.nih.gov/axis/SpellAid.jws?wsdl)這有時會給出一個503 HTTP狀態(在幾千個查詢之後...)

suds.client模塊然後崩潰與一個非特定的異常,我能夠通過嘗試除了語句,但我無法測試對於實際的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') 

所以沒有我,如果條款是能夠檢測到異常我不知道下一步該怎麼嘗試才能專門捕捉它。很難提供一個定期失敗的最小示例,因爲它依賴於服務器端,並且此腳本每天一次地繼續運行。我可以提供更多信息嗎?你有任何想法,如果條款測試?

乾杯, 帕斯卡爾

+1

'503!=「503」'in Python – jfs

+0

你是否修復了這個問題? – fledgling

回答

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 

沒有這條線,你會有一個很好的錯誤信息與異常類型和完整的回溯。

+0

感謝您的建議,現在腳本運行的非常穩定。所以即時通訊仍在等待下一個503發生...... – user1428754

+0

'if e.args ==((503,u'Service Temporarily Unavailable'),):'似乎是正確的測試。多路骨頭的異常不傳遞2個參數,它在一個參數中傳遞一個元組。請參閱@fledgling答案。 –

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