这是我正在使用的 Prolog HTTP 服务器:
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/html_write)).
:- use_module(library(http/json)).
:- use_module(library(http/json_convert)).
:- use_module(library(http/http_json)).
:- use_module(library(http/http_log)).
:- http_handler(root(handle), handle_rpc, []).
:- set_setting(http:logfile, '/home/john/Desktop/log.txt').
http_json:json_type('application/json').
% sample prolog program
f1(1).
f1(2).
f1(4).
f1(5).
f2(3).
f2(4).
f2(6).
server(Port) :-
http_server(http_dispatch, [port(Port)]).
handle_rpc(Request) :-
http_read_json(Request, JSONIn),
json_to_prolog(JSONIn, PrologIn),
evaluate(PrologIn, PrologOut), % application body
PrologOut = JSONOut,
reply_json(JSONOut).
evaluate(PrologIn, PrologOut) :-
PrologIn = json([args=Query, password=PW, method=MethodName]),
MethodName = eval,
PW = blah,
atom_to_term(Query, Term, Bindings),
Goal =.. [findall, Bindings, Term, IR],
call(Goal),
sort(IR, Result),
Result = PrologOut,
format(atom(StringResult), "~q", [Result]),
PrologOut = json([result=StringResult]).
这是我从中进行 RPC 调用的 Python 客户端:
import urllib2
import json
url = "http://192.168.157.128:5001/handle"
json_dict = { 'args' : "f1(S)", 'method' : 'eval', 'password' : 'blah' }
jdata = json.dumps( json_dict )
headers = {}
headers['Content-Type'] = 'application/json'
print jdata
req = urllib2.Request(url, jdata, headers)
res = urllib2.urlopen(req)
for line in res:
print json.loads(line)
请求失败并出现 500 错误。在我的 httpd.log 文件中,我得到的唯一消息是:
/*Tue Aug 21 17:42:41 2012*/ request(10, 1345585361.981, [peer(ip(192,168,157,1)),method(post),request_uri('/handle'),path('/handle'),http_version(1-1),content_length(55),host('192.168.157.128'),port(5001),content_type('application/json'),connection(close),user_agent('Python-urllib/2.6')]).
completed(10, 0.000829367999999997, 815, 500, error("goal unexpectedly failed: user:handle_rpc([protocol(http),peer(ip(192,168,157,1)),pool(client(httpd@5001,user:http_dispatch,<stream>(0xf9c2b0),<stream>(0xf9c410))),input(<stream>(0xf9c2b0)),method(post),request_uri(/handle),path(/handle),http_version(1-1),accept_encoding(identity),content_length(55),host(192.168.157.128),port(5001),content_type(application/json),connection(close),user_agent(Python-urllib/2.6)])")).
就是这样,只是“目标意外失败”。不,目标失败了。我想知道为什么它失败了。