Operation Manual
Human-computer interfacing
118
Notes:
Lesson 4.3: Remote Procedure Call
A Remote Procedure Call (RPC) is bit of code in one program that causes
another program to do something, for instance run a subroutine (a bit of a program
that performs a specific task). The two programs don’t necessarily have to be on
the same computer, although often they are.
When using RPC, calling a subroutine looks the same in your code as if it was
being executed as part of your program. The communication between programs
remains largely hidden and the programmer does not have to worry about the
details of how it is done.
The language the programs are written in and type of computers do not have to
be the same either – they can be on completely different hardware, operating
systems and programming languages. For example, a mobile phone app could be
programmed to call a Python program running on a Raspberry Pi to perform a
task and return a result.
The type of Remote Procedure Call we are using for this example is called
XMLRPC. Extensible Markup Language (XML) is a markup language that defines
a set of rules for encoding data in a format that is both human-readable and
machine-readable.
There are two programs here – a “server” and a “client”. The server sits waiting
for one or more clients to ask it to do something.
rpc_server.py:
fromxmlrpc.serverimport SimpleXMLRPCServer
def hello(name='World'):
"""Return a string saying hello to the name given"""
return 'Hello, %s!' % name
def add(x,y):
"""Add two variables together and return the result"""
returnx+y
# set up the server
server = SimpleXMLRPCServer(("localhost", 8000))
# register our functions
server.register_function(hello)
server.register_function(add)
# Run the server's main loop
server.serve_forever()