说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