sparqlwrapper proxy

25 views
Skip to first unread message

Mario Migliaccio

unread,
Nov 22, 2024, 11:06:07 AM11/22/24
to rdflib-dev
I am wondering if there's the possibility to query the endpoint throgh a proxy.

Is it possibile?

Mario Migliaccio

unread,
Nov 22, 2024, 11:10:38 AM11/22/24
to rdflib-dev
Just to give an example I'm using this code to query data from official endpoint from Italian assembly. I am having trouble with the connection of my production environment. I am wondering if it is possibile to query through a proxy I have
having
PROXY_PORT
PROXY_HOST
PROXY_PASSWORD
PROXY_USER

sparql_endpoint = "https://dati.senato.it/sparql"
sparql = SPARQLWrapper(sparql_endpoint)
sparql.setQuery("""
PREFIX osr: <http://dati.senato.it/osr/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT DISTINCT ?senatore ?nome ?cognome ?inizioMandato ?legislatura ?tipoMandato
WHERE {
    ?senatore a osr:Senatore.
    ?senatore foaf:firstName ?nome.
    ?senatore foaf:lastName ?cognome.
    ?senatore osr:mandato ?mandato.
    ?mandato osr:legislatura ?legislatura.
    ?mandato osr:inizio ?inizioMandato.
    ?mandato osr:tipoMandato ?tipoMandato.
    OPTIONAL { ?mandato osr:fine ?df. }
    FILTER(!bound(?df))
} ORDER BY ?cognome ?nome
""")
sparql.setReturnFormat(JSON)
sparql.setTimeout(10)
sparql.query().convert()

carlo...@gmail.com

unread,
Nov 26, 2024, 2:25:18 AM11/26/24
to rdflib-dev
Hi Mario,

could you share the error returned?

Best,

Carlos

Mario Migliaccio

unread,
Nov 26, 2024, 6:42:27 AM11/26/24
to rdflib-dev
The error is:

urllib.error.HTTPError: HTTP Error 403: Forbidden

I solved it using this piece of code when raising this error

import urllib.request
HTTP_PROXIES = {
'http': f'http://{PROXY_USER}:{PROXY_PASSWORD}@'
f'{PROXY_HOST}:{PROXY_PORT}',
'https': f'https:
//{PROXY_USER}:{PROXY_PASSWORD}@'
f'{PROXY_HOST}:{PROXY_PORT}'
',
}
proxy_support = urllib.request.ProxyHandler(HTTP_PROXIES)
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)

and then
results = sparql.query().convert()

I think this solved my issue allowing using proxy to make a call thought SparqlWrapper

Drew Perttula

unread,
Nov 26, 2024, 4:45:50 PM11/26/24
to rdfli...@googlegroups.com

Thanks for posting the solution. Can you also share what doc links you originally looked at? Then we can add some text to help the next user with the same problem.

The closest I can find is https://rdflib.readthedocs.io/en/stable/intro_to_sparql.html#querying-a-remote-service but that doesn't look like what you were using. As for what to link to, perhaps https://docs.python.org/3.5/howto/urllib2.html#proxies would help, though it doesn't list all the

--
http://github.com/RDFLib
---
You received this message because you are subscribed to the Google Groups "rdflib-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdflib-dev+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/rdflib-dev/1d03b6af-e9e0-4b76-8027-89ac39f82ca5n%40googlegroups.com.

Nicholas Car

unread,
Nov 26, 2024, 7:33:18 PM11/26/24
to rdfli...@googlegroups.com
Drew, Mario is using SPARQLWrapper - https://sparqlwrapper.readthedocs.io - not just RDFLib.

Mario: it would indeed be wonderful if you could add a small writeup of this to https://sparqlwrapper.readthedocs.io/en/latest/main.html#python-package / https://github.com/RDFLib/sparqlwrapper/blob/master/README.rst (the README comes through to ReadTheDocs docco too).

After the RDFLib 8.0.0 major release in the next few months, we'll put the call out for updates to a range of RDFLib-related packaged and SPARQLWrapper is one of those. It's working fine but could do with a lot of improvements, especially given more modern HTTP libraries like requests / httpx.

Cheers, Nick

Mario Migliaccio

unread,
Nov 27, 2024, 4:51:29 AM11/27/24
to rdflib-dev
Hi Drew, Nicholas, thank you for your replying.

@Nicholas since I'm pretty new to developing, what you ask is to open an issue on https://github.com/RDFLib/sparqlwrapper/ where I do explain the issue and the solution I proposed?

Nicholas Car

unread,
Nov 27, 2024, 5:56:30 AM11/27/24
to rdfli...@googlegroups.com
Hi Mario,

Yes, you could open an Issue as you say, then provide a PR that makes the changes as you've implemented them. In this case it's documentation on how to use, rather than application code changes, and I think it can just go right into the README as I indicated below. So yes, Issue & PR. We will review the PR and if we see any problems, improve it, if we can!

Hopefully you love making contributions so once you;ve done this one, you'll get right into making more!

Cheers, Nick


Reply all
Reply to author
Forward
0 new messages