Search code examples
pythonxml-rpc

Delegating single function to client server in python


I have a simple client-server program using the xml-rpc protocol. My code is presented below. But this does not work as I expect it to. Maybe someone knows why?

In my module directed_graph there is function algorithm_dijkstra(vertex_name) that I want to execute on the server.

Client:

from xmlrpc.client import ServerProxy
...
if __name__ == '__main__':
    proxy = ServerProxy('http://127.0.0.1:8080')
    # filling the graph with data
    ...
    test = proxy.algorithm_dijkstra(vertex) # I expect the correct data (dictionary) in test after the function is executed on the server
    ...

Server:

from xmlrpc.server import SimpleXMLRPCServer
from directed_graph import algorithm_dijkstra


def main():
    server = SimpleXMLRPCServer(("127.0.0.1", 8080))
    print('Server is listening on port 8080...')
    server.register_function(algorithm_dijkstra)
    server.serve_forever()


if __name__ == '__main__':
    main()

Small clarification:
In my module "directed_graph", basically the functions that the client uses, but there is one that I want to perform on the server. I found that if all the functions are performed on the server, then the program works correctly. But how do I delegate only 1 function to the server and not all?


Solution

  • I solved the problem as follows. On the client, I set and store data for the graph, and on the server I perform only one function algorithm_dijkstra. And for everything to work correctly, need to make the second parameter for function algorithm_dijkstra, namely transfer the graph itself to this function, and not take the graph from somewhere like me tried to do before. And then everything works correctly and as expected.