Coders Packet

RPC implementation using Python

By Vrishabhdhwaj Maharshi

In this post, we will discuss RPC (Remote Procedure Call) and its implementation using Python programming language. We will use XML-RPC to design and communicate between a server and a client.

RPC (Remote Procedure Call) is an API Architectural style that allows us to invoke a procedure or a function on a remote server in some format. The format of the received result is the same as that of the function on the server. RPC is kind of similar to REST (REpresentational State Transfer). In RPC, the method is found in the URL itself and the client submits calls that use arguments/parameters to use these methods.

Without further ado, let's dive into the Python code for its implementation.


from xmlrpc.server import SimpleXMLRPCServer

On the server-side, we import the necessary libraries and then define a simple function that calculates the factorial of a number.


server = SimpleXMLRPCServer(("localhost", 8000), logRequests=True)
server.register_function(factorial, "factorial_rpc")

Define a local XML-RPC server and register the function to the server. Finally, by using exception handling, make the server run forever till an interrupt is encountered as given in the file. This completes the server side of the code.

Now for the client-side,

import xmlrpc.client
proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
print("factorial of 3 is : %s" % str(proxy.factorial_rpc(3)))

Define a proxy and then use it to invoke the registered function.

Download Complete Code


No comments yet