sparqlwrapper icon indicating copy to clipboard operation
sparqlwrapper copied to clipboard

Fails to execute a query on dbpedia: ssl certificate problem

Open datamusee opened this issue 3 years ago • 11 comments

Hello I'm used to use SPARQLWrapper with success. But, today, in my environment, I get the following error Début de run Comment :=>> <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)> and I don't understand how tosolve this problem. If I directly use urlopen, I can do something like this (unsecure, I know) unsecurecontext = ssl._create_unverified_context() ... urlopen(urldbpedia, context=unsecurecontext) ... But, I'm unable to find a method to manage that with SPARQLWrapper, cleanly or uncleanly.

(I know about issue #56, but I doesn't see a solution which apply) ps: my environment is a virtual lab in a moodle platform

datamusee avatar Jun 24 '21 08:06 datamusee

Hi,

I have encountered the same issue. I traced it down to an SSL error for all pages which uses Let's Encrypt. Please see also the issue I created on Anaconda. There is not yet any solution but hopefully someone finds one soon.

Best regards Sven

sven-h avatar Oct 11 '21 11:10 sven-h

I have been successfully using myBinder to run a notebook containing SPARQLWrapper code for a few weeks. Towards the end of last week it suddenly stopped working. In trying to get to the bottom of the issue I've found the following error

ERROR:Exception running query: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1091)>

Is this the same issue or is something else going on?

AlasdairGray avatar Jan 26 '22 11:01 AlasdairGray

There is also #102 which seems to be related.

AlasdairGray avatar Jan 26 '22 11:01 AlasdairGray

Hi @AlasdairGray , do you have a minimal examples which shows this error?

sven-h avatar Jan 26 '22 11:01 sven-h

You can fire up this notebook on myBinder, run all the cells then select the first query from the dropdown list of available queries 'Number of Triples', and click Execute.

This throws the exception (not sure why the exception text is not displaying in the UI). You can find the exception text below in the generated log file idpQuery.log.

INFO:Starting processing at 11:45:27.766193
INFO:SPARQL Endpoint: https://swel.macs.hw.ac.uk/data/repositories/bioschemas
INFO:Run query HCLS-Stats-Queries/number-triples.rq
INFO:Executing query in file: HCLS-Stats-Queries/number-triples.rq
ERROR:Exception running query: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1091)>
Query:
 ## Number of Triples

SELECT (COUNT(*) AS ?triples)
WHERE {
    GRAPH ?g {
        ?s ?p ?o
    }
}

options: sparql

The query runs fine when I run it directly through the GraphDB interface, and worked fine up until last week through the notebook. Only change in the notebook is the tweak I made today to the logging so that we can see the error message. Nothing has changed on the server side as far as I'm aware.

AlasdairGray avatar Jan 26 '22 11:01 AlasdairGray

The corresponding curl command works fine

curl https://swel.macs.hw.ac.uk/data/repositories/bioschemas?query="SELECT%20%28COUNT%28%2A%29%20AS%20%3Ftriples%29%0AWHERE%20%7B%0A%20%20%20%20GRAPH%20%3Fg%20%7B%0A%20%20%20%20%20%20%20%20%3Fs%20%3Fp%20%3Fo%0A%20%20%20%20%7D%0A%7D"

AlasdairGray avatar Jan 26 '22 11:01 AlasdairGray

For me the following snippet works fine:

from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper("https://swel.macs.hw.ac.uk/data/repositories/bioschemas")
sparql.setQuery("""
    SELECT * WHERE {
        ?s a ?o.
    }
    LIMIT 10
""")
sparql.setReturnFormat(JSON)
ret = sparql.queryAndConvert()
for r in ret["results"]["bindings"]:
    print(r)

sven-h avatar Jan 26 '22 11:01 sven-h

It seems that things work fine on a Windows machine but not on a Mac (I've been discussing the issue with a colleague), not sure about Linux. We've put in a workaround, but suspect that the underlying cause could be to do with the certificate on the server.

AlasdairGray avatar Jan 26 '22 13:01 AlasdairGray

@sven-h Your snippet works well. I tried:

sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("SELECT DISTINCT ?geometry WHERE {<http://dbpedia.org/resource/Paris> geo:geometry ?geometry}")
sparql.setReturnFormat(JSON)
results = sparql.query().convert()

It works with Python 3.7, but the reported SSL exception is raised using Python 3.8 (also 3.10) on my Windows machine.

sraimund avatar Apr 11 '22 15:04 sraimund

Hi,

your script works fine for me:

conda create -n testenv python=3.8
conda activate testenv
pip install sparqlwrapper

Can you check the folder of openssl with openssl version -d (in the activated env) if it contains some files?

sven-h avatar Apr 12 '22 06:04 sven-h

Hi sven-h, I used virtualenv instead of conda. An openssl binary is not available there. I tried adding pyOpenSSL, but it did not help.

I noticed that sparqlwrapper works fine with Python 3.7.3. But with Python 3.7.4 and higher, the error appears. A workaround, mentioned on stackoverflow, is to add the following lines before the query:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

Do you have a more elegant solution?

sraimund avatar Apr 12 '22 10:04 sraimund