gpt4 book ai didi

python - SPARQL 查询中的 HTTP 错误 500

转载 作者:行者123 更新时间:2023-12-01 09:33:10 26 4
gpt4 key购买 nike

当我运行以下形式的 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

如何解决这个问题——即用相同的异常捕获这个错误并保持程序运行?

谢谢!

最佳答案

查看堆栈跟踪,您还必须处理来自 SPARQLExceptionsEndPointInternalError (请参阅错误堆栈跟踪的末尾),因此以下内容应该有效:

try: 
Result = EndPoint.query().convert()
except error.HTTPError:
time.sleep(SomeTime)
SomeFunction()
except EndPointInternalError:
# whatever code needed to handle this case

如果您想以相同的方式处理这两个错误:

 try: 
Result = EndPoint.query().convert()
except (error.HTTPError, EndPointInternalError):
time.sleep(SomeTime)
SomeFunction()

关于python - SPARQL 查询中的 HTTP 错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49776520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com