当我运行表单的 Python 代码时
from SPARQLWrapper import SPARQLWrapper, JSON
from urllib import request, error
import time
class SomeClass:
...
def SomeFunction(self):
EndPoint = SPARQLWrapper('http://collection.britishmuseum.org/sparql')
Query = '''
SomeString
'''
EndPoint.setQuery(Query)
EndPoint.setReturnFormat(JSON)
try:
Result = EndPoint.query().convert()
except error.HTTPError:
time.sleep(SomeTime)
SomeFunction()
SomeInstance = SomeClass()
SomeInstance.SomeFunction()
我的异常捕获有时在端点发生的带有 502 代码(错误网关)的 HTTP 错误。但是,它没有捕捉到一个特定的 HTTP 错误,从而在某些时候破坏了程序。
该错误指出以下内容:
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\SPARQLWrapper\Wrapper.py in _query(self)
656 try:
--> 657 response = urlopener(request)
658 return response, self.returnFormat
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
222 opener = _opener
--> 223 return opener.open(url, data, timeout)
224
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in open(self, fullurl, data, timeout)
531 meth = getattr(processor, meth_name)
--> 532 response = meth(req, response)
533
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in http_response(self, request, response)
641 response = self.parent.error(
--> 642 'http', request, response, code, msg, hdrs)
643
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in error(self, proto, *args)
563 args = (dict, proto, meth_name) + args
--> 564 result = self._call_chain(*args)
565 if result:
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
503 func = getattr(handler, meth_name)
--> 504 result = func(*args)
505 if result is not None:
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in http_error_302(self, req, fp, code, msg, headers)
755
--> 756 return self.parent.open(new, timeout=req.timeout)
757
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in open(self, fullurl, data, timeout)
531 meth = getattr(processor, meth_name)
--> 532 response = meth(req, response)
533
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in http_response(self, request, response)
641 response = self.parent.error(
--> 642 'http', request, response, code, msg, hdrs)
643
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in error(self, proto, *args)
569 args = (dict, 'default', 'http_error_default') + orig_args
--> 570 return self._call_chain(*args)
571
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
503 func = getattr(handler, meth_name)
--> 504 result = func(*args)
505 if result is not None:
c:\users\administrator\appdata\local\programs\python\python36\lib\urllib\request.py in http_error_default(self, req, fp, code, msg, hdrs)
649 def http_error_default(self, req, fp, code, msg, hdrs):
--> 650 raise HTTPError(req.full_url, code, msg, hdrs, fp)
651
HTTPError: HTTP Error 500: Connect to bigdata:8080 [bigdata/172.17.0.4] failed: Connection refused
During handling of the above exception, another exception occurred:
EndPointInternalError Traceback (most recent call last)
<ipython-input-6-46c4c3ac2387> in <module>()
1 SomeInstance = SomeClass()
----> 2 SomeInstance.SomeFunction()
<ipython-input-4-d513cf0b30b5> in SomeFunction(self)
90 EndPoint.setQuery(Query)
91 EndPoint.setReturnFormat(JSON)
92 try:
---> 93 Result = EndPoint.query().convert()
95 except error.HTTPError:
c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\SPARQLWrapper\Wrapper.py in query(self)
685 @rtype: L{QueryResult} instance
686 """
--> 687 return QueryResult(self._query())
688
689 def queryAndConvert(self):
c:\users\administrator\appdata\local\programs\python\python36\lib\site-packages\SPARQLWrapper\Wrapper.py in _query(self)
663 raise EndPointNotFound(e.read())
664 elif e.code == 500:
--> 665 raise EndPointInternalError(e.read())
666 else:
667 raise e
EndPointInternalError: EndPointInternalError: endpoint returned code 500 and response
我该如何解决这个问题——即用相同的异常捕获这个错误并保持程序运行?
谢谢!