0

I'm trying to build a system with two processes: an xmlrpc server with a "ping" method and a QCoreApplication middleware. The middleware has an xmlrpc client which connects to the xmlrpc server. The middleware also acts as an RPyC server. I want to connect to the middleware via RPyC and call the "ping" method on the server.

# xmlrpc_server.py
from xmlrpc.server import SimpleXMLRPCServer


class XMLRPCServer(SimpleXMLRPCServer):

    def __init__(self, addr=('localhost', 4444)):
        super(XMLRPCServer, self).__init__(addr)
        self.register_function(self.ping)

    def ping(self):
        return 'pong'


if __name__ == '__main__':
    s = XMLRPCServer()
    s.serve_forever()
# middleware.py
import sys
import xmlrpc.client
import rpyc
import rpyc.utils.server
from PyQt5 import Qt


class MiddleWare(Qt.QCoreApplication):
    def __init__(self, *args, **kwargs):
        super(MiddleWare, self).__init__(*args, **kwargs)
        self.xmlrpc_client = xmlrpc.client.ServerProxy('http://localhost:4444')
        self.rpyc_server = RPyCServer(port=2222)
        self.rpyc_server.start()

    def ping(self):
        return self.xmlrpc_client.ping()


class RPyCService(rpyc.Service):

    def on_connect(self):
        self.exposed_application = Qt.QCoreApplication.instance()


class RPyCServer(Qt.QThread):

    def __init__(self, port=None):
        super(RPyCServer, self).__init__()
        self._server = rpyc.utils.server.ThreadedServer(
                RPyCService,
                port=port,
                protocol_config={
                    'allow_all_attrs': True,
                    'allow_public_attrs': True,
                },
        )
        self.run = self._server.start


if __name__ == '__main__':
    mw = MiddleWare(sys.argv)
    sys.exit(mw.exec_())
$ python3 xmlrpc_server.py &
[1] 5785
$ python3 middleware.py &
[2] 5986
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpyc
>>> conn = rpyc.connect('localhost', 2222, config={'allow_all_attrs': True})
>>> conn.root.application.ping()
'pong'
>>> conn.root.application.xmlrpc_client.ping()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/rpyc/core/netref.py", line 199, in __call__
    return syncreq(_self, consts.HANDLE_CALL, args, kwargs)
  File "/usr/lib/python3/dist-packages/rpyc/core/netref.py", line 72, in syncreq
    return conn.sync_request(handler, oid, *args)
  File "/usr/lib/python3/dist-packages/rpyc/core/protocol.py", line 523, in sync_request
    raise obj
rpyc.core.vinegar/xmlrpc.client.Fault: 

========= Remote Traceback (1) =========
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/rpyc/core/protocol.py", line 347, in _dispatch_request
    res = self._HANDLERS[handler](self, *args)
  File "/usr/lib/python3/dist-packages/rpyc/core/protocol.py", line 624, in _handle_call
    return self._local_objects[oid](*args, **dict(kwargs))
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1112, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1452, in __request
    verbose=self.__verbose
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1154, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1170, in single_request
    return self.parse_response(resp)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1342, in parse_response
    return u.close()
  File "/usr/lib/python3.6/xmlrpc/client.py", line 656, in close
    raise Fault(**self._stack[0])
xmlrpc.client.Fault: <Fault 1: '<class \'Exception\'>:method "exposed_ping" is not supported'>

>>> 

I expected to get the value 'pong' back from the call, both when calling conn.root.application.ping() and directly conn.root.application.xmlrpc_client.ping(), instead in the second case an error was shown. Why?

2
  • 1
    What is your version of rpyc? with rpyc 4.0.2 I had to change def on_connect(self): to def on_connect(self, conn):, I am using Python 3.7.3 with PyQt5 5.12.1 on Linux Commented Apr 18, 2019 at 7:09
  • @eyllanesc I'm using rpyc 3.4.4 from ubuntu Commented Apr 18, 2019 at 7:16

2 Answers 2

1
  • As I see for rpyc==3.4.4 you must pass a name to it when registering the function:
from xmlrpc.server import SimpleXMLRPCServer


class XMLRPCServer(SimpleXMLRPCServer):
    def __init__(self, addr=("localhost", 4444)):
        super(XMLRPCServer, self).__init__(addr)
        self.register_function(self.ping, "exposed_ping") # <---

    def ping(self):
        return "pong"


if __name__ == "__main__":
    s = XMLRPCServer()
    s.serve_forever()

  • But in rpyc==4.0.2 it is not necessary to do it but you have a second argument to on_connect:
# ...
class RPyCService(rpyc.Service):
    def on_connect(self, conn):
        self.exposed_application = Qt.QCoreApplication.instance()
# ...
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this would allow me to call, from an RPyC client, "conn.root.application.xmlrpc_client.ping" as originally asked, but it would fail from within the middleware, see the updated question and my answer. Anyway, thanks for your help :)
0

I was able to get the desired result by editing the [protocol_]config, adding

'exposed_prefix': ''
# middleware.py
#   ...

class RPyCServer(Qt.QThread):

    def __init__(self, port=None):
        super(RPyCServer, self).__init__()
        self._server = rpyc.utils.server.ThreadedServer(
                RPyCService,
                port=port,
                protocol_config={
                    'allow_all_attrs': True,
                    'exposed_prefix': '',
                },
        )
        self.run = self._server.start

#   ...
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpyc
>>> conn = rpyc.connect('localhost', 2222, config={'allow_all_attrs': True, 'exposed_prefix': ''})
>>> conn.root.application.ping()
'pong'
>>> conn.root.application.xmlrpc_client.ping()
'pong'
>>>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.