1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-09-02 05:14:48 +00:00

Support python 3 in pysearpc.

This commit is contained in:
Jonathan Xu
2019-06-06 17:36:19 +08:00
parent 4f32f8be00
commit ac0750d058
7 changed files with 30 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
from builtins import object
import json
from common import SearpcError
from .common import SearpcError
def _fret_int(ret_str):
try:
@@ -7,10 +8,10 @@ def _fret_int(ret_str):
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
if 'err_code' in dicts:
raise SearpcError(dicts['err_msg'])
if dicts.has_key('ret'):
if 'ret' in dicts:
return dicts['ret']
else:
raise SearpcError('Invalid response format')
@@ -21,10 +22,10 @@ def _fret_string(ret_str):
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
if 'err_code' in dicts:
raise SearpcError(dicts['err_msg'])
if dicts.has_key('ret'):
if 'ret' in dicts:
return dicts['ret']
else:
raise SearpcError('Invalid response format')
@@ -61,7 +62,7 @@ def _fret_obj(ret_str):
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
if 'err_code' in dicts:
raise SearpcError(dicts['err_msg'])
if dicts['ret']:
@@ -75,7 +76,7 @@ def _fret_objlist(ret_str):
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
if 'err_code' in dicts:
raise SearpcError(dicts['err_msg'])
l = []
@@ -91,7 +92,7 @@ def _fret_json(ret_str):
except:
raise SearpcError('Invalid response format')
if dicts.has_key('err_code'):
if 'err_code' in dicts:
raise SearpcError(dicts['err_msg'])
if dicts['ret']:

View File

@@ -2,6 +2,7 @@
RPC client/server implementation based on named pipe transport.
"""
from builtins import object
import json
import logging
import os
@@ -56,7 +57,7 @@ class NamedPipeTransport(SearpcTransport):
# "I" for unsiged int
header = struct.pack('I', len(body))
sendall(self.pipe_fd, header)
sendall(self.pipe_fd, body)
sendall(self.pipe_fd, body.encode(encoding='utf-8'))
resp_header = recvall(self.pipe_fd, 4)
# logger.info('resp_header is %s', resp_header)
@@ -64,7 +65,7 @@ class NamedPipeTransport(SearpcTransport):
# logger.info('resp_size is %s', resp_size)
resp = recvall(self.pipe_fd, resp_size)
# logger.info('resp is %s', resp)
return resp
return resp.decode(encoding='utf-8')
class NamedPipeClient(SearpcClient):
@@ -141,10 +142,10 @@ class PipeHandlerThread(Thread):
req = recvall(self.pipe_fd, req_size)
# logger.info('req is %s', req)
data = json.loads(req)
data = json.loads(req.decode(encoding='utf-8'))
resp = searpc_server.call_function(data['service'], data['request'])
# logger.info('resp is %s', resp)
resp_header = struct.pack('I', len(resp))
sendall(self.pipe_fd, resp_header)
sendall(self.pipe_fd, resp)
sendall(self.pipe_fd, resp.encode(encoding='utf-8'))

View File

@@ -1,6 +1,8 @@
#!/usr/bin/python
from __future__ import print_function
from builtins import str
import string
import sys
@@ -29,7 +31,7 @@ def gen_fcall_funcs_array(arg_types):
pyfuncname = "fcall__" + "_".join(arg_types)
tmplist = []
for arg in arg_types:
tmplist.append(string.capitalize(arg))
tmplist.append(arg.capitalize())
cfuncname = "SearpcClient_Fcall__" + "_".join(tmplist)
@@ -47,7 +49,7 @@ def gen_fret_funcs_array(ret_type):
cfuncname = "SearpcClient_Fret__Void"
else:
pyfuncname = "fret__" + ret_type
cfuncname = "SearpcClient_Fret__" + string.capitalize(ret_type)
cfuncname = "SearpcClient_Fret__" + ret_type.capitalize()
return string.Template(func_item_template)\
.substitute(pyfuncname=pyfuncname,
@@ -79,8 +81,8 @@ def gen_module_funcs_array():
array = fcall_array
array += fret_array
print string.Template(module_func_array_template)\
.substitute(array=array)
print(string.Template(module_func_array_template)\
.substitute(array=array))
type_table = {
@@ -128,7 +130,7 @@ def gen_fcall_func(arg_types):
tmplist = []
for arg in arg_types:
tmplist.append(string.capitalize(arg))
tmplist.append(arg.capitalize())
Suffix = "_".join(tmplist)
suffix = "_".join(arg_types)
def_args = ""
@@ -161,7 +163,7 @@ def gen_fcall_list():
arg_types_list.append(item[1])
for item in arg_types_list:
print gen_fcall_func(item)
print(gen_fcall_func(item))
if __name__ == "__main__":

View File

@@ -1,6 +1,8 @@
from builtins import str
from builtins import object
import json
from common import SearpcError
from .common import SearpcError
class SearpcService(object):
def __init__(self, name):
@@ -25,7 +27,7 @@ class SearpcServer(object):
"""input str -> output str"""
try:
argv = json.loads(fcallstr)
except Exception, e:
except Exception as e:
raise SearpcError('bad call str: ' + str(e))
service = self.services[svcname]
@@ -41,7 +43,7 @@ class SearpcServer(object):
def call_function(self, svcname, fcallstr):
try:
retVal = self._call_function(svcname, fcallstr)
except Exception, e:
except Exception as e:
ret = {'err_code': 555, 'err_msg': str(e)}
else:
ret = {'ret': retVal}

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python
#coding: UTF-8
from builtins import object
import json
import logging
import os

View File

@@ -1,4 +1,5 @@
from builtins import object
class SearpcTransport(object):
"""
A transport is repsonsible to send the serialized request to the

View File

@@ -5,7 +5,7 @@ from pysearpc.errors import NetworkError
def recvall(fd, total):
remain = total
data = ''
data = b''
while remain > 0:
try:
new = fd.recv(remain)