Доступ к членам класса в другом файле с использованием rpyc без импорта - PullRequest
0 голосов
/ 02 ноября 2018

Скажи в 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'

Как я могу получить доступ к переменным-членам таких объектов? Это не сработало, даже если я изменил Exception на BaseException.

Обновление : Я изменил 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

...