Chapter 18 ■ rpC
333
Fourth, each RPC mechanism needs to support some addressing scheme whereby you can reach out and connect
to a particular remote API. Some such mechanisms are quite complicated, and they might even have the ability to
connect you automatically to the correct server on your network for performing a particular task, without your having
to know its name beforehand. Other mechanisms are quite simple and just ask you for the IP address, port number, or
URL of the service you want to access. These mechanisms expose the underlying network addressing scheme, rather
than creating a scheme of their own.
Finally, some RPC mechanisms support authentication, access control, and even full impersonation of particular
user accounts when RPC calls are made by several different client programs wielding different credentials. But features
like these are not always available; and, in fact, simple and popular RPC mechanisms usually lack them entirely. Simple
RPC schemes use an underlying protocol like HTTP that provides its own authentication, and they leave it up to you to
configure whatever passwords, public keys, or firewall rules are necessary to secure the lower-level protocol if you want
your RPC service protected from arbitrary access.
XML-RPC
Let’s begin this brief tour of RPC mechanisms by looking at the facilities built into Python for speaking XML-RPC.
This might seem like a poor choice for the first example. After all, XML is famously clunky and verbose, and the
popularity of XML-RPC in new services has been declining for years.
But XML-RPC has native support in Python’s Standard Library precisely because it was one of the first RPC
protocols of the Internet age, operating natively over HTTP instead of insisting on its own on-the-wire protocol.
This means that the examples presented here will not even require third-party modules. Although this makes the RPC
server somewhat less capable than if a third-party library were used, this will also make the examples simple for this
initial foray into RPC.
the XML-rpC prOtOCOL
purpose: remote procedure calls
Standard: http://www.xmlrpc.com/spec
runs atop: http
Data types: int; float; unicode; list; dict with unicode keys; with nonstandard extensions, datetime
and None
Libraries: xmlrpclib, SimpleXMLRPCServer, DocXMLRPCServer
If you have ever used raw XML, then you are familiar with the fact that it lacks any data-type semantics. It cannot
represent numbers, for example, but only elements that contain other elements, text strings, and text-string attributes.
Thus the XML-RPC specification has to build additional semantics on top of the plain XML document format in
order to specify how things like numbers should look when converted into marked-up text.
The Python Standard Library makes it easy to write either an XML-RPC client or server. Listing 18-1 shows a basic
server that starts a web server on port 7001 and then listens for incoming Internet connections.
Listing 18-1. An XML-RPC Server
#!/usr/bin/env python3