English / Japanese

json-rpc-direct - direct call python via json

Last modified: 4 Feb 2010

What's jsonrpcdirect?

jsonrpcdirectd is a foreign function interface about python. It can call python function using json-rpc format.

Sample code

To use jsonrpcdirectd , you can write a code like below.

Use python via netcat

First, you should start python server
$ python 
import jsonrpcdirect as JD
server = JD.Server( JD.JsonRpc10(), JD.TransportTcpIp(addr=("127.0.0.1",31415), logfunc=JD.log_stdout))
server.serve()  
Switch to other terminal
$ netcat localhost 31415
{"method": "numpy.linalg.norm", "params": [[2,2]], "id": 0}
Then, get response
{"result": 2.8284271247461903, "error": null, "id": 0}
Other exmaple
$ echo  "{\"method\": \"numpy.linalg.norm\", \"params\": [[2,2]], \"id\": 0}" | nc localhost 31415
{"result": 2.8284271247461903, "error": null, "id": 0}

Use from Common Lisp

Before call python functions, start json-server. The python server script (in the netcat section) included as file jsonrpc_server.py. You can use this script as
$ python  /usr/lib/python2.5/site-packages/jsonrpcdirect/jsonrpc_server.py
or
$ python /usr/local/lib/python2.6/dist-packages/jsonrpcdirect/jsonrpc_server.py
Then, you can call python functions as the following
(asdf-install:install "http://www2s.biglobe.ne.jp/~niitsuma/cl-json-client01.tar.gz")
(asdf:operate 'asdf:load-op :cl-json-client) 

(jc::c "range" 2 7)  ; -> (2 3 4 5 6)
(jc::c "numpy.linalg.norm" (list 2 2))  ; -> 2.828427
(jc::c "numpy.reshape" (list 1 2 3 4 5 6 ) (list 2 3)) ; -> ((1 2 3) (4 5 6))   
(jc::c "numpy.dot" '((1 2) (3 4)) '((1 2) (3 4)) ) ; -> ((7 10) (15 22))  ;multipy matrix

Requirements

jsonrpcdirect require I have checked the environments below. For ubuntu, you can install require libs by
sudo apt-get install numpy simplejson

Download

Installation

To install,

Type below commands.
sudo apt-get install python-simplejson python-numpy
wget http://www2s.biglobe.ne.jp/%7Eniitsuma/jsonrpcdirect06.tar.gz
tar zxf jsonrpcdirect06.tar.gz
cd jsonrpcdirect06
sudo python setup.py install

License

License for I modified part is Affero GPL.

ChangeLog

2009-05-18: release version 0.6

hirotaka.niitsuma@gmail.com