0

说进去server.py

import rpyc

class MyError(Exception):
    def __init__(self, message):
        super(MyError, self).__init__(message)
        self.error = message

    def foo1(self):
        self.error_type = 1

    def foo2(self):
        self.error_type = 2


class MyService(rpyc.Service):
    def exposed_test(self):
        e = MyError('error')
        e.foo1()
        return e


if __name__ == '__main__':
    from rpyc.utils.server import ThreadPoolServer

    server = ThreadPoolServer(MyService(), port=6000)
    server.start()

client.py,我想基于error_type这样的不同的步骤:

import rpyc

with rpyc.connect('localhost', 6000) as conn:
    try:
        raise conn.root.test()
    except Exception as e:
        if e.error_type == 1:
            pass
        else:
            pass

但是,编译器说

Traceback (most recent call last):
  File "C:/python/client.py", line 5, in <module>
    raise conn.root.test()
TypeError: exceptions must derive from BaseException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/python/client.py", line 7, in <module>
    if e.error_type == 1:
AttributeError: 'TypeError' object has no attribute 'error_type'

如何访问此类对象的成员变量?即使我更改ExceptionBaseException.

更新:我修改client.py为:

import rpyc

with rpyc.connect('localhost', 6000) as conn:
    e = conn.root.test()
    print(e)
    print(e.error_type)

. print(e)正确,因为它打印“错误”,但我仍然无法得到e.error_type

4

0 回答 0