Query the db on stardog using python

I am trying to query the db already present on the stardog server using python.Below is the code: -
import urllib.request as req

headers = {
'Accept': 'text/turtle, application/rdf+xml, '
'application/n-triples, application/trig,'
' application/n-quads, text/n3, application/trix, '
'application/ld+json, application/sparql-results+xml,'
' application/sparql-results+json, application/x-binary-rdf-results-table, '
'text/boolean, text/csv, text/tsv, '
'text/tab-separated-values'
}
request = req.Request('http://localhost:5820/mytest111/query?query=select%20*%20where%20{%3Fs%20%3Fp%20%3Fo}%20LIMIT%2010',headers=headers)
print(request)

response_body = req.urlopen(request).read()
print(response_body)

The stardog is already running in the background.I get an error "[WinError 10061] No connection could be made because the target machine actively refused it>". The entire error log is below:-

Traceback (most recent call last):
File "C:\Users\kartik\Anaconda3\lib\urllib\request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "C:\Users\kartik\Anaconda3\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\kartik\Anaconda3\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\kartik\Anaconda3\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\kartik\Anaconda3\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File "C:\Users\kartik\Anaconda3\lib\http\client.py", line 964, in send
self.connect()
File "C:\Users\kartik\Anaconda3\lib\http\client.py", line 936, in connect
(self.host,self.port), self.timeout, self.source_address)
File "C:\Users\kartik\Anaconda3\lib\socket.py", line 724, in create_connection
raise err
File "C:\Users\kartik\Anaconda3\lib\socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/kartik/PycharmProjects/TDBCreate/Stardog.py", line 39, in
response_body = req.urlopen(request).read()
File "C:\Users\kartik\Anaconda3\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\kartik\Anaconda3\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "C:\Users\kartik\Anaconda3\lib\urllib\request.py", line 544, in _open
'_open', req)
File "C:\Users\kartik\Anaconda3\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\kartik\Anaconda3\lib\urllib\request.py", line 1346, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Users\kartik\Anaconda3\lib\urllib\request.py", line 1320, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10061] No connection could be made because the target machine actively refused it>

Process finished with exit code 1

Check your firewall settings and make sure it isn’t blocking access to port 5820. You should also check to make sure that Stardog is actually running and listening to port 5820.

I checked the firewall settings and the port 5820 is not blocked.The stardog server seems to be running on the 5820 as when I manually start the server (i.e stardog-admin server start) and run http;//localhost:5820 the browser runs fine.

To add more the python code in first message is executed after the server is manually started(i.e stardog-admin server start).Will still there is a requirement of http authentication?

I believe it would be required unless you specifically disabled it when you started Stardog although the error message is a little confusing since I would have expected you to get a 401 response or something similar.

I tried other http authentication to make connection but getting the same error message
Below is the code
import requests
from requests.auth import HTTPDigestAuth

url = 'http://localhost:5820/mytest111/query'
try:
requests.get(url, auth=HTTPDigestAuth('admin', 'admin'))

except requests.exceptions.ConnectionError:
print("I am here")

Stardog only supports HTTP Basic authentication but Digest is planned for a future release. Home | Stardog Documentation Latest

Even the basic authentication is not working
import requests

response = requests.get('http://localhost:5820/mytest111',
auth=requests.auth.HTTPBasicAuth(
'admin',
'admin'))
print(response.text)

the documentation for stardog python is not developed and maintained by Stardog.Is it possible to to even work on Stardog through python

I've never personally used Stardog with Python but Stardog has a Rest API so there shouldn't be any reason why you couldn't use Python. It's documented at https://stardog.docs.apiary.io/ You might want to take a look at this Gist Example code for working with Stardog from Python. · GitHub

The only stardog client library I know of is GitHub - knorex/pystardog: Python client library for Stardog but it hasn't been updated in a long time.

Try taking a look at HOWTO Fetch Internet Resources Using urllib2 — Python 2.7.18 documentation under "Basic Authentication"

I tried removing proxy from the localhost and it worked.Below is the code : -
import os
import urllib.request as req
os.environ['NO_PROXY'] ='localhost'
headers = {
'Accept': 'text/turtle, application/rdf+xml, '
'application/n-triples, application/trig,'
' application/n-quads, text/n3, application/trix, '
'application/ld+json, application/sparql-results+xml,'
' application/sparql-results+json, application/x-binary-rdf-results-table, '
'text/boolean, text/csv, text/tsv, '
'text/tab-separated-values'
}
request = req.Request('http://localhost:5820/mytest111/query?query=select%20*%20where%20{%3Fs%20%3Fp%20%3Fo}%20LIMIT%2010',headers=headers)
response_body = req.urlopen(request).read()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.