mirror of
https://github.com/haiwen/libsearpc.git
synced 2025-09-02 13:25:25 +00:00
Support python 3 in pysearpc.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
|
from builtins import object
|
||||||
import json
|
import json
|
||||||
from common import SearpcError
|
from .common import SearpcError
|
||||||
|
|
||||||
def _fret_int(ret_str):
|
def _fret_int(ret_str):
|
||||||
try:
|
try:
|
||||||
@@ -7,10 +8,10 @@ def _fret_int(ret_str):
|
|||||||
except:
|
except:
|
||||||
raise SearpcError('Invalid response format')
|
raise SearpcError('Invalid response format')
|
||||||
|
|
||||||
if dicts.has_key('err_code'):
|
if 'err_code' in dicts:
|
||||||
raise SearpcError(dicts['err_msg'])
|
raise SearpcError(dicts['err_msg'])
|
||||||
|
|
||||||
if dicts.has_key('ret'):
|
if 'ret' in dicts:
|
||||||
return dicts['ret']
|
return dicts['ret']
|
||||||
else:
|
else:
|
||||||
raise SearpcError('Invalid response format')
|
raise SearpcError('Invalid response format')
|
||||||
@@ -21,10 +22,10 @@ def _fret_string(ret_str):
|
|||||||
except:
|
except:
|
||||||
raise SearpcError('Invalid response format')
|
raise SearpcError('Invalid response format')
|
||||||
|
|
||||||
if dicts.has_key('err_code'):
|
if 'err_code' in dicts:
|
||||||
raise SearpcError(dicts['err_msg'])
|
raise SearpcError(dicts['err_msg'])
|
||||||
|
|
||||||
if dicts.has_key('ret'):
|
if 'ret' in dicts:
|
||||||
return dicts['ret']
|
return dicts['ret']
|
||||||
else:
|
else:
|
||||||
raise SearpcError('Invalid response format')
|
raise SearpcError('Invalid response format')
|
||||||
@@ -61,7 +62,7 @@ def _fret_obj(ret_str):
|
|||||||
except:
|
except:
|
||||||
raise SearpcError('Invalid response format')
|
raise SearpcError('Invalid response format')
|
||||||
|
|
||||||
if dicts.has_key('err_code'):
|
if 'err_code' in dicts:
|
||||||
raise SearpcError(dicts['err_msg'])
|
raise SearpcError(dicts['err_msg'])
|
||||||
|
|
||||||
if dicts['ret']:
|
if dicts['ret']:
|
||||||
@@ -75,7 +76,7 @@ def _fret_objlist(ret_str):
|
|||||||
except:
|
except:
|
||||||
raise SearpcError('Invalid response format')
|
raise SearpcError('Invalid response format')
|
||||||
|
|
||||||
if dicts.has_key('err_code'):
|
if 'err_code' in dicts:
|
||||||
raise SearpcError(dicts['err_msg'])
|
raise SearpcError(dicts['err_msg'])
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
@@ -91,7 +92,7 @@ def _fret_json(ret_str):
|
|||||||
except:
|
except:
|
||||||
raise SearpcError('Invalid response format')
|
raise SearpcError('Invalid response format')
|
||||||
|
|
||||||
if dicts.has_key('err_code'):
|
if 'err_code' in dicts:
|
||||||
raise SearpcError(dicts['err_msg'])
|
raise SearpcError(dicts['err_msg'])
|
||||||
|
|
||||||
if dicts['ret']:
|
if dicts['ret']:
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
RPC client/server implementation based on named pipe transport.
|
RPC client/server implementation based on named pipe transport.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from builtins import object
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@@ -56,7 +57,7 @@ class NamedPipeTransport(SearpcTransport):
|
|||||||
# "I" for unsiged int
|
# "I" for unsiged int
|
||||||
header = struct.pack('I', len(body))
|
header = struct.pack('I', len(body))
|
||||||
sendall(self.pipe_fd, header)
|
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)
|
resp_header = recvall(self.pipe_fd, 4)
|
||||||
# logger.info('resp_header is %s', resp_header)
|
# logger.info('resp_header is %s', resp_header)
|
||||||
@@ -64,7 +65,7 @@ class NamedPipeTransport(SearpcTransport):
|
|||||||
# logger.info('resp_size is %s', resp_size)
|
# logger.info('resp_size is %s', resp_size)
|
||||||
resp = recvall(self.pipe_fd, resp_size)
|
resp = recvall(self.pipe_fd, resp_size)
|
||||||
# logger.info('resp is %s', resp)
|
# logger.info('resp is %s', resp)
|
||||||
return resp
|
return resp.decode(encoding='utf-8')
|
||||||
|
|
||||||
|
|
||||||
class NamedPipeClient(SearpcClient):
|
class NamedPipeClient(SearpcClient):
|
||||||
@@ -141,10 +142,10 @@ class PipeHandlerThread(Thread):
|
|||||||
req = recvall(self.pipe_fd, req_size)
|
req = recvall(self.pipe_fd, req_size)
|
||||||
# logger.info('req is %s', req)
|
# 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'])
|
resp = searpc_server.call_function(data['service'], data['request'])
|
||||||
# logger.info('resp is %s', resp)
|
# logger.info('resp is %s', resp)
|
||||||
|
|
||||||
resp_header = struct.pack('I', len(resp))
|
resp_header = struct.pack('I', len(resp))
|
||||||
sendall(self.pipe_fd, resp_header)
|
sendall(self.pipe_fd, resp_header)
|
||||||
sendall(self.pipe_fd, resp)
|
sendall(self.pipe_fd, resp.encode(encoding='utf-8'))
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
from builtins import str
|
||||||
import string
|
import string
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
@@ -29,7 +31,7 @@ def gen_fcall_funcs_array(arg_types):
|
|||||||
pyfuncname = "fcall__" + "_".join(arg_types)
|
pyfuncname = "fcall__" + "_".join(arg_types)
|
||||||
tmplist = []
|
tmplist = []
|
||||||
for arg in arg_types:
|
for arg in arg_types:
|
||||||
tmplist.append(string.capitalize(arg))
|
tmplist.append(arg.capitalize())
|
||||||
|
|
||||||
cfuncname = "SearpcClient_Fcall__" + "_".join(tmplist)
|
cfuncname = "SearpcClient_Fcall__" + "_".join(tmplist)
|
||||||
|
|
||||||
@@ -47,7 +49,7 @@ def gen_fret_funcs_array(ret_type):
|
|||||||
cfuncname = "SearpcClient_Fret__Void"
|
cfuncname = "SearpcClient_Fret__Void"
|
||||||
else:
|
else:
|
||||||
pyfuncname = "fret__" + ret_type
|
pyfuncname = "fret__" + ret_type
|
||||||
cfuncname = "SearpcClient_Fret__" + string.capitalize(ret_type)
|
cfuncname = "SearpcClient_Fret__" + ret_type.capitalize()
|
||||||
|
|
||||||
return string.Template(func_item_template)\
|
return string.Template(func_item_template)\
|
||||||
.substitute(pyfuncname=pyfuncname,
|
.substitute(pyfuncname=pyfuncname,
|
||||||
@@ -79,8 +81,8 @@ def gen_module_funcs_array():
|
|||||||
array = fcall_array
|
array = fcall_array
|
||||||
array += fret_array
|
array += fret_array
|
||||||
|
|
||||||
print string.Template(module_func_array_template)\
|
print(string.Template(module_func_array_template)\
|
||||||
.substitute(array=array)
|
.substitute(array=array))
|
||||||
|
|
||||||
|
|
||||||
type_table = {
|
type_table = {
|
||||||
@@ -128,7 +130,7 @@ def gen_fcall_func(arg_types):
|
|||||||
|
|
||||||
tmplist = []
|
tmplist = []
|
||||||
for arg in arg_types:
|
for arg in arg_types:
|
||||||
tmplist.append(string.capitalize(arg))
|
tmplist.append(arg.capitalize())
|
||||||
Suffix = "_".join(tmplist)
|
Suffix = "_".join(tmplist)
|
||||||
suffix = "_".join(arg_types)
|
suffix = "_".join(arg_types)
|
||||||
def_args = ""
|
def_args = ""
|
||||||
@@ -161,7 +163,7 @@ def gen_fcall_list():
|
|||||||
arg_types_list.append(item[1])
|
arg_types_list.append(item[1])
|
||||||
|
|
||||||
for item in arg_types_list:
|
for item in arg_types_list:
|
||||||
print gen_fcall_func(item)
|
print(gen_fcall_func(item))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
|
from builtins import str
|
||||||
|
from builtins import object
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from common import SearpcError
|
from .common import SearpcError
|
||||||
|
|
||||||
class SearpcService(object):
|
class SearpcService(object):
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
@@ -25,7 +27,7 @@ class SearpcServer(object):
|
|||||||
"""input str -> output str"""
|
"""input str -> output str"""
|
||||||
try:
|
try:
|
||||||
argv = json.loads(fcallstr)
|
argv = json.loads(fcallstr)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
raise SearpcError('bad call str: ' + str(e))
|
raise SearpcError('bad call str: ' + str(e))
|
||||||
|
|
||||||
service = self.services[svcname]
|
service = self.services[svcname]
|
||||||
@@ -41,7 +43,7 @@ class SearpcServer(object):
|
|||||||
def call_function(self, svcname, fcallstr):
|
def call_function(self, svcname, fcallstr):
|
||||||
try:
|
try:
|
||||||
retVal = self._call_function(svcname, fcallstr)
|
retVal = self._call_function(svcname, fcallstr)
|
||||||
except Exception, e:
|
except Exception as e:
|
||||||
ret = {'err_code': 555, 'err_msg': str(e)}
|
ret = {'err_code': 555, 'err_msg': str(e)}
|
||||||
else:
|
else:
|
||||||
ret = {'ret': retVal}
|
ret = {'ret': retVal}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
#coding: UTF-8
|
#coding: UTF-8
|
||||||
|
|
||||||
|
from builtins import object
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
|
|
||||||
|
from builtins import object
|
||||||
class SearpcTransport(object):
|
class SearpcTransport(object):
|
||||||
"""
|
"""
|
||||||
A transport is repsonsible to send the serialized request to the
|
A transport is repsonsible to send the serialized request to the
|
||||||
|
@@ -5,7 +5,7 @@ from pysearpc.errors import NetworkError
|
|||||||
|
|
||||||
def recvall(fd, total):
|
def recvall(fd, total):
|
||||||
remain = total
|
remain = total
|
||||||
data = ''
|
data = b''
|
||||||
while remain > 0:
|
while remain > 0:
|
||||||
try:
|
try:
|
||||||
new = fd.recv(remain)
|
new = fd.recv(remain)
|
||||||
|
Reference in New Issue
Block a user