如何解决“ u​​rllib2.URLError:<urlopen错误=“” [errno =“” 111] =“” connection =“”被拒绝=“”>“

时间:2018-07-17 17:19:51

标签: python urllib2 virtuoso sparqlwrapper

我在以下位置找到了示例代码 https://github.com/RDFLib/sparqlwrapper/blob/master/scripts/example.py

我稍加修改

from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF, CSV, TSV
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
SELECT ?subject ?predicate ?object
FROM <http://graph.something.com>
FROM <http://ontology.something.com>
WHERE {
?subject ?predicate ?object
} LIMIT 10
""")
# JSON example
print '\n\n*** JSON Example'
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
for result in results["results"]["bindings"]:
print result["label"]["value"]

大多数功能不起作用,但是当我指向dbpedia.org网站时,我需要的部分起作用了。但是,当我将其定向到我自己的站点(除我之外,其他任何人都无法访问)时,出现如下错误:

*** JSON Example Traceback (most recent call last):   File "test01.py", line 29, in <module>
    results = sparql.query().convert()   File "/usr/local/lib/python2.7/dist-packages/SPARQLWrapper/Wrapper.py", line 798, in query
    return QueryResult(self._query())   File "/usr/local/lib/python2.7/dist-packages/SPARQLWrapper/Wrapper.py", line 766, in _query
    response = urlopener(request)   File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)   File "/usr/lib/python2.7/urllib2.py", line 429, in open
    response = self._open(req, data)   File "/usr/lib/python2.7/urllib2.py", line 447, in _open
    '_open', req)   File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)   File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
    return self.do_open(httplib.HTTPConnection, req)   File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
    raise URLError(err) urllib2.URLError: <urlopen error [Errno 111] Connection refused>

您知道发生了什么,更重要的是如何解决它?

1 个答案:

答案 0 :(得分:1)

这是错误的有效部分-

<urlopen error [Errno 111] Connection refused>

检查您的Virtuoso的[HttpServer] ServerPort(在INI文件中设置),并确保您的Python客户端可以连接到Virtuoso服务器主机上的该端口。