English / Japanese

py-json-rpc-embed - python embeding via json-rpc

Last modified: 5 Feb 2009

What's pyjsonrpcembd?

pyjsonrpcembd is a foreign function interface for python. It can call python function using json-rpc format.

Sample code

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

Use from C

(use c-wrapper)
#include 
#include "pyjsonrpcembd.h"
int main()
{
    pyjsonrpcembdinit();
    printf(
    pyjsonrpcembdhandle
    ("{\"jsonrpc\": \"2.0\", \"method\": \"norm\", \"params\": [[2, 3]], \"id\": 0}"));
//result is {"jsonrpc": "2.0", "result": 3.60555127546, "id": 0}
    
    printf(
    pyjsonrpcembdhandle
   ("{\"jsonrpc\": \"2.0\", \"method\": \"echo\", \"params\": [[2, 3]], \"id\": 0}"));   
   Py_Finalize();
   return 0;
}

Use from Gauche

requires c-wrapper.
(use c-wrapper)
(c-load-library "libpyjsonrpcembd.so")
(c-include "pyjsonrpcembd.h")

(c-load-library "libc")
(c-include "stdio.h")

(pyjsonrpcembdinit)

(printf ( pyjsonrpcembdhandle "{\"jsonrpc\": \"2.0\", \"method\": \"norm\", \"params\": [[2, 3]], \"id\": 0}" ))
(printf ( pyjsonrpcembdhandle "{\"jsonrpc\": \"2.0\", \"method\": \"echo\", \"params\": [\"aaa\"], \"id\": 0}" ))

(pyjsonrpcembdfinalize)

Use from Common Lisp

(asdf:oos 'asdf:load-op :cffi)
(require :cffi)
(cffi::define-foreign-library libpyjsonrpcembd
  (:unix "libpyjsonrpcembd.so")
  (:windows "libpyjsonrpcembd.dll"))
(cffi::load-foreign-library 'libpyjsonrpcembd)
(cffi::defcfun ("pyjsonrpcembdinit"     pyjsonrpcembdinit) :void )
(cffi::defcfun ("pyjsonrpcembdfinalize" pyjsonrpcembdfinalize) :void )
(cffi::defcfun ("pyjsonrpcembdhandle"   pyjsonrpcembdhandle) :string (jsonstr :string) )

(pyjsonrpcembdinit)


(print 
(pyjsonrpcembdhandle 
"{\"jsonrpc\": \"2.0\", \"method\": \"norm\", \"params\": [[2, 3]], \"id\": 0}"))

(print 
(pyjsonrpcembdhandle 
"{\"jsonrpc\": \"2.0\", \"method\": \"echo\", \"params\": [\"aaa\"], \"id\": 0}"))


(pyjsonrpcembdfinalize)

Requirements

I have checked the environments below.

Download

Installation

To install, type make manually.

Type below commands.

% tar zxvf pyjsonrpcembd03.tar.gz
% cd pyjsonrpcembd
% make
% sudo make install

How to register function

To use a function form json-rpc, registration of the function is required . The registration is done by editing

 /usr/lib/python2.5/site-package/pyjsonrpcembd/pyjsonrpcregister.py

For example, if you'd like to register echo and numpy.linalg.norm, edit the file like this

import pyjsonrpcembd
server = pyjsonrpcembd.jsonrpc.Server(
pyjsonrpcembd.jsonrpc.JsonRpc20(), 
pyjsonrpcembd.jsonrpc.TransportSTDINOUT())

def echo( s ):
    print s
    return s

server.register_function( echo )

import numpy
server.register_function( numpy.linalg.norm )

Restrictions

ChangeLog

2009-02-4: release version 0.3
2009-01-30: release version 0.1

hirotaka.niitsuma@gmail.com