1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-05-11 16:44:20 +00:00

[python] add SearpcServer in python

This commit is contained in:
lins05 2012-10-09 09:48:15 +08:00 committed by lins05
parent 5cf33ea3a1
commit 56b412eca3
5 changed files with 71 additions and 19 deletions

View File

@ -1,5 +1,5 @@
pysearpcdir=${pyexecdir}/pysearpc
pysearpc_PYTHON = __init__.py client.py
pysearpc_PYTHON = __init__.py common.py client.py server.py

View File

@ -1,2 +1,3 @@
from client import SearpcClient, searpc_func, SearpcError, SearpcObjEncoder
from common import SearpcError
from client import SearpcClient, searpc_func, SearpcObjEncoder
from server import searpc_server

View File

@ -1,19 +1,12 @@
import simplejson as json
class SearpcError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
from common import SearpcError
def _fret_int(ret_str):
try:
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
raise SearpcError(dicts['err_msg'])
@ -27,7 +20,7 @@ def _fret_string(ret_str):
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
raise SearpcError(dicts['err_msg'])
@ -67,29 +60,29 @@ def _fret_obj(ret_str):
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
raise SearpcError(dicts['err_msg'])
if dicts['ret']:
return _SearpcObj(dicts['ret'])
else:
return None
def _fret_objlist(ret_str):
try:
dicts = json.loads(ret_str)
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
raise SearpcError(dicts['err_msg'])
l = []
if dicts['ret']:
for elt in dicts['ret']:
l.append(_SearpcObj(elt))
return l

7
pysearpc/common.py Normal file
View File

@ -0,0 +1,7 @@
class SearpcError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg

51
pysearpc/server.py Normal file
View File

@ -0,0 +1,51 @@
import simplejson as json
from common import SearpcError
class SearpcService(object):
def __init__(self, name):
self.name = name
self.func_table = {}
class SearpcServer(object):
def __init__(self):
self.services = {}
def create_service(self, svcname):
service = SearpcService(svcname)
self.services[svcname] = service
def register_function(self, svcname, fn, fname=None):
service = self.services[svcname]
if fname == None:
fname = fn.__name__
service.func_table[fname] = fn
def _call_function(self, svcname, fcallstr):
"""input str -> output str"""
try:
argv = json.loads(fcallstr)
except Exception, e:
raise SearpcError('bad call str: ' + str(e))
service = self.services[svcname]
fname = argv[0]
fn = service.func_table.get(fname, None)
if fn is None:
raise SearpcError('No such funtion %s' % fname)
ret = fn(*argv[1:])
return ret
def call_function(self, svcname, fcallstr):
try:
retVal = self._call_function(svcname, fcallstr)
except Exception, e:
ret = {'err_code': 555, 'err_msg': str(e)}
else:
ret = {'ret': retVal}
return json.dumps(ret)
searpc_server = SearpcServer()