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?
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'
>>>