如何解决python2.7和python3.x之间的二进制转换问题?

2024-06-02 05:07:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想更改异常钩子以返回我的自定义错误,而不是远程错误错误。在这里是我的示例代码:

你知道吗服务器.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()

你知道吗客户端.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)

当我跑的时候服务器.py使用python 2.7和客户端.py对于python 3.6,输出如下所示:

{b'name':b'mahdieh'}

但当我跑的时候服务器.py使用python 2.7和客户端.py对于python 2.7,输出如下所示: {name':'mahdieh'}

有处理这个问题的工具吗?你知道吗


Tags: pytestimportself服务器eventdef错误