我正在开发一个与其他开发人员一起使用通用 Fuseki 数据集的应用程序。 Javascript 函数调用 AJAX (jQuery $.ajax()
) 并通过 POST 请求向 Python 脚本发送三个变量。
Javascript:
$.ajax({
url: 'py/registerUser.py',
data: {firstname: registerFirstName, lastname: registerLastName, email: registerEmail},
dataType: 'json',
type: 'post',
success: function(data) {
if(data["result"] == "ok") {
validSPARQL = true;
}
else {
validSPARQL = false;
}
},
error: function(data) {
validSPARQL = false;
}
});
Python 脚本检索 POST 变量并操作它们以创建查询字符串。然后它将方法设置为 POST 并在给定地址上执行查询。
Python
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgi, cgitb, json, re
from SPARQLWrapper import SPARQLWrapper, JSON
arguments = cgi.FieldStorage()
firstname = arguments.getvalue('firstname')
lastname = arguments.getvalue('lastname')
email = arguments.getvalue('email')
queryString = """PREFIX aop:<http://vitali.web.cs.unibo.it/AnnOtaria/person/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf:<http://xmlns.com/foaf/0.1/>
INSERT DATA {
aop:""" + firstname + lastname + """ a foaf:Person ;
foaf:name \"""" + firstname + """ """ + lastname + """\" ;
foaf:mbox <mailto:""" + email + """> .
}
"""
sparql = SPARQLWrapper("http://linktodataset.com:8181/data/update")
sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)
sparql.setMethod('POST')
sparql.query()
print "Content-type: text/json\n"
print "{ \"result\": \"ok\" }"
我的问题:每次发送查询时,我都会收到内部服务器错误 500。我尝试以多种方式操作代码,我按照this page上的说明进行操作。并查看了this other one ,以及许多其他其中的 SPARQLWrapper 文档,但什么也没发生。
我尝试以其他方式编写查询字符串,导入其他库。没有任何效果。
注释 1:注释行 sparql.query()
错误消失,但显然它没有发送任何查询。
注释 2:我可以将 SELECT 查询发送到 http://linktodataset.com:8181/data/query ,但这不适用于 INSERT DATA。
注释 3: 我在 Fuseki 控制面板上以专用于 SPARQL 更新 的形式尝试了查询字符串,效果很好。 SPARQL 查询 表单中的相同查询不起作用。
我确信我做错了什么,但我不明白是什么......有什么建议吗?谢谢!
编辑 1(2015 年 2 月 9 日)
我已经编辑了查询字符串并尝试打印它,这是结果:
PREFIX aop:<http://mypersonallink.com/person/>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf:<http://xmlns.com/foaf/0.1/>
INSERT DATA {
aop:TestfirstTestlast a foaf:Person ;
foaf:name "Testfirst Testlast" ;
foaf:mbox <mailto:test@mail.com> .
}
编辑 2(2015 年 2 月 10 日)
我安装了本地 Fuseki 并使用 fuseki-server --update --mem/ds
启动它。地址是localhost:3030
。 Python 代码几乎保持不变(mod_python 而不是 cgi,当然还有数据集的链接)。这是我得到的:
MOD_PYTHON ERROR
ProcessId: 13230
Interpreter: '127.0.1.1'
ServerName: '127.0.1.1'
DocumentRoot: '/var/www/personal/html'
URI: '/py/registerUser.py'
Location: None
Directory: '/var/www/personal/html/py/'
Filename: '/var/www/personal/html/py/registerUser.py'
PathInfo: ''
Phase: 'PythonHandler'
Handler: 'mod_python.cgihandler'
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)
File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)
File "/usr/lib/python2.7/dist-packages/mod_python/cgihandler.py", line 96, in handler
imp.load_module(module_name, fd, path, desc)
File "/var/www/personal/html/py/registerUser.py", line 37, in <module>
sparql.query()
File "/usr/lib/pymodules/python2.7/SPARQLWrapper/Wrapper.py", line 355, in query
return QueryResult(self._query())
File "/usr/lib/pymodules/python2.7/SPARQLWrapper/Wrapper.py", line 330, in _query
raise QueryBadFormed
QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed
注意最后一行:
QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed
好吧,我评论了 sparql.query()
并打印了 queryString
,复制了它并将其放入可访问的 SPARQL Update
框中http://localhost:3030/sparql.tpl
它工作得很好。
最佳答案
Fuseki 端点实际上启用了“更新”吗?请注意,在文档中,Fuseki: serving RDF data over HTTP ,有一个--update选项:
--update
Allow update. Otherwise only read requests are served (ignored if a configuration file is given).
然后,您还提到:
Note 2: I can send SELECT queries to http://linktodataset.com:8181/data/query, but this doesn't work with INSERT DATA.
你不应该指望它能起作用。在我上面链接到的同一文档中,我们看到了这个示例(请注意应发送查询和更新的 URL):
fuseki-server --update --mem /ds
runs the server on port 3030 with an in-memory dataset. It can be accessed via the appropriate protocol at the following URLs:
- SPARQL query:
http://localhost:3030/ds/query
- SPARQL update:
http://localhost:3030/ds/update
- SPARQL HTTP update:
http://localhost:3030/ds/data
您的 INSERT DATA 是一个更新查询,需要发送到 http://linktodataset.com:8181/data/update 等。
关于javascript - 通过 Python SPARQLWrapper 将数据插入到 Fuseki,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28284002/