We can create our own cross-platform, language-independent server using the XML-RPC protocol.We use the SimpleXMLRPCServer to create the SimpleXMLRPCServer instance and tell it to listen for incoming requests. Next we define some functions to be part of the service and register those functions so that the server knows how to call it.
Running the Server
In the below example we create a server using the SimpleXMLRPCServer instance and register some pre-defined as well as custom functions. Finally, we put the server into an infinite loop receiving and responding to requests.
Example
from xmlrpc.server import SimpleXMLRPCServer from xmlrpc.server import SimpleXMLRPCRequestHandler class RequestHandler(SimpleXMLRPCRequestHandler): rpc_paths = ('/RPC2',) with SimpleXMLRPCServer(('localhost', 9000), requestHandler=RequestHandler) as server: server.register_introspection_functions() # Register len() function; server.register_function(len) # Register a function under a different name @server.register_function(name='rmndr') def remainder_function(x, y): return x // y # Register a function under function.__name__. @server.register_function def modl(x, y): return x % y server.serve_forever()
Once the above server is started, it can be called by a client program which can refer to the functions and make the function calls.
Running the Client
Example
import xmlrpc.client s = xmlrpc.client.ServerProxy('https://localhost:9000') print(s.len("Tutorialspoint")) print(s.rmndr(12,5)) print(s.modl(7,3)) # Print list of available methods print(s.system.listMethods())
Output
Running the above code gives us the following result −
14 2 1 ['len', 'modl', 'rmndr', 'system.listMethods', 'system.methodHelp', 'system.methodSignature']