Python - RPC JSON Server

Sammer Shete

9 months ago

RPC JSON Server in Python | Insideaiml
RPC JSON Server in Python | Insideaiml
JSON or JavaScript Object Notation is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. The RPC call made based on JSON is able to send data in a much compact and efficient manner than the normal XML based RPC call. The python module jsonrpclib is able to create a simple JSON based server and client.


In the below example we create a simple JSON server and create a function in it. This function breaks a bigger list into smaller lists mentioning the length of the argument as well as the argument itself.

# server program
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer

def findlen(*args):

	res = []
	for arg in args:
			lenval = len(arg)
		except TypeError:
			lenval = none
		res.append((lenval, arg))
	return res

def main():
	server = SimpleJSONRPCServer(('localhost', 1006))
	print("Start server")
if __name__ == '__main__':  

# Call by client
from jsonrpclib import Server
def main():
    conn = Server('http://localhost:1006')
    print(conn.findlen(('a','x','d','z'), 11, {'Mt. Abu': 1602, 'Mt. Nanda': 3001,'Mt. Kirubu': 102, 'Mt.Nish': 5710}))
if __name__ == '__main__':
When we run the above program, we get the following output
[[4, [u'a', u'x', u'd', u'z']], [none, 11], [4, {u'Mt. Abu': 1602, u'Mt. Kirubu': 102, u'Mt. Nanda': 3001, u'Mt.Nish': 5710}]]

To learn more about python visit the InsideAIML page.
I hope you enjoyed reading this article and finally, you came to know about Python - RPC JSON Server.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review