pythonrpyc

using rpyc to update properties on a remote object


I am using rpyc within an application and have come across a small hiccup.

I would just like to know if it's possible to update remote properties?

I have a test server:

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()

and within an ipython console:

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'

Is it possible to update remote properties at all?


Solution

  • Yes, setting remote attributes is blocked by default, but is allowed if you set allow_setattr=True.

    (Setting allow_delattr=True also makes sense if you're setting allow_setattr=True anyway)

    t = ThreadedServer(TestService, port = 2942,
                    protocol_config={"allow_all_attrs":True,
                                     "allow_setattr": True,
                                     "allow_delattr": True,})
    

    See the docs.


    You can also hackily bypass the setattr-protection, by accessing the __dict__ of the remote object directly (but of course the first soluiton is much better):

    test.__dict__['V'] = 2