我想更改异常挂钩,以返回自定义错误而不是远程错误。这是我的示例代码:
server.py
import zerorpc
import c
class ExceptionalRPC(object):
def bad(self):
raise KeyError
zero_ctx = zerorpc.Context()
test_middleware = c.ServerBeforeExecMiddleware()
zero_ctx.register_middleware(test_middleware)
s = zerorpc.Server(ExceptionalRPC(), context=zero_ctx)
s.bind("tcp://0.0.0.0:4242")
s.run()
client.py
import zerorpc
import c
zero_ctx = zerorpc.Context()
test_middleware = c.ServerBeforeExecMiddleware()
zero_ctx.register_middleware(test_middleware)
c = zerorpc.Client(context=zero_ctx)
c.connect("tcp://127.0.0.1:4242")
try:
c.bad()
except Exception as b:
print(type(b))
c.py
class ServerBeforeExecMiddleware(object):
def __init__(self):
self.called = False
def server_inspect_exception(self, request_event, reply_event, task_context, exc_infos):
args = list(reply_event.args)
args.append({"name":"mahdieh"})
reply_event._args = tuple(args)
print(type(reply_event.args),reply_event.args)
def client_handle_remote_error(self, event):
for i in event.args:
print(type(i),i)
当我使用python 2.7运行server.py并使用python 3.6运行client.py时,输出如下所示:
{b'name':b'mahdieh'}
但是当我运行python 2.7的server.py和python 2.7的client.py时,输出看起来像这样: {'name':'mahdieh'}
有解决此问题的工具吗?