how to resolve "urllib2.URLError: <urlopen error [Errno 111] Connection refused>"

elbillaf picture elbillaf · Jul 17, 2018 · Viewed 11k times · Source

I found sample code at https://github.com/RDFLib/sparqlwrapper/blob/master/scripts/example.py

which I modified slightly

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"]

Most of it doesn't work, but the part I need DOES work when I point to the dbpedia.org site. However, when I direct it to my own site (which is not available to anyone but me) I get an error as follows:

*** 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>

Any idea what's going on and more importantly how to fix it?

Answer

TallTed picture TallTed · Jul 18, 2018

This is the operative part of the error --

<urlopen error [Errno 111] Connection refused>

Check your Virtuoso's [HttpServer] ServerPort (set in the INI file), and ensure that your Python client can connect to that port on the Virtuoso server host.