Python 使用rpyc更新远程对象的属性
我在应用程序中使用rpyc时遇到了一个小问题 我只是想知道是否有可能更新远程属性 我有一个测试服务器:Python 使用rpyc更新远程对象的属性,python,rpyc,Python,Rpyc,我在应用程序中使用rpyc时遇到了一个小问题 我只是想知道是否有可能更新远程属性 我有一个测试服务器: import rpyc from rpyc.utils.server import ThreadedServer class Test: def __init__(self): self._v = 0 @property def V(self): return self._v @V.setter def V(self
import rpyc
from rpyc.utils.server import ThreadedServer
class Test:
def __init__(self):
self._v = 0
@property
def V(self):
return self._v
@V.setter
def V(self, value):
self._v = value
class TestService(rpyc.Service):
def exposed_Test(self):
return Test()
if __name__ == '__main__':
t = ThreadedServer(TestService, port = 2942,
protocol_config={"allow_all_attrs":True})
t.start()
在ipython控制台中:
In [1]: import rpyc
In [2]: conn = rpyc.connect('localhost', 2942, config={'allow_all_attrs':True})
In [3]: test = conn.root.Test()
In [4]: test.V
Out[4]: 0
In [5]: test.V = 2
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-d3ae0dcd1075> in <module>()
----> 1 test.V = 2
<blah blah traceback>
AttributeError: cannot access 'V'
[1]中的:导入rpyc
在[2]中:conn=rpyc.connect('localhost',2942,config={'allow\u all\u attrs':True})
在[3]中:test=conn.root.test()
在[4]中:test.V
Out[4]:0
[5]中:试验V=2
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
---->1试验V=2
AttributeError:无法访问“V”
是否可以更新远程属性?是的,设置远程属性在默认情况下被阻止,但如果您设置了
allow\u setattr=True
,则允许设置远程属性
(如果您仍在设置allow\u setattr=True
,则设置allow\u setattr=True也有意义)
看
您还可以通过直接访问远程对象的\uuuu dict\uuuu
绕过setattr保护(当然,第一个解决方案要好得多):
哇哦,我喜欢这个工具!非常感谢你向我指出这一点。我真的读过教程等,我只是没有遇到这个小宝石。
t = ThreadedServer(TestService, port = 2942,
protocol_config={"allow_all_attrs":True,
"allow_setattr": True,
"allow_delattr": True,})
test.__dict__['V'] = 2