Foundations of Python Network Programming

(WallPaper) #1
Chapter 18 ■ rpC

335

This server will need to be running before you can try any of the next three program listings, so bring up a
command window and get it started:


$ python xmlrpc_server.py
Server ready


The server is now waiting for connections on localhost port 7001. All of the normal addressing rules apply to
this TCP server that you learned in Chapters 2 and 3, so unless you adjust the code to bind to an interface other than
localhost, you will have to connect to it from another command prompt on the same system. Begin by opening
another command window and get ready to try out the next three listings as I review them.
First, let’s try the introspection capability you turned on in this particular server. Note that this ability is optional,
and it may not be available on many other XML-RPC services that you use online or that you deploy yourself.
Listing 18-2 shows how introspection happens from the client’s point of view.


Listing 18-2. Asking an XML-RPC Server What Functions It Supports


#!/usr/bin/env python3


Foundations of Python Network Programming, Third Edition


github.com/brandon-rhodes/fopnp/blob/m/py3/chapter18/xmlrpc_introspect.py


XML-RPC client


import xmlrpc.client


def main():
proxy = xmlrpc.client.ServerProxy('http://127.0.0.1:7001')


print('Here are the functions supported by this server:')
for method_name in proxy.system.listMethods():


if method_name.startswith('system.'):
continue


signatures = proxy.system.methodSignature(method_name)
if isinstance(signatures, list) and signatures:
for signature in signatures:
print('%s(%s)' % (method_name, signature))
else:
print('%s(...)' % (method_name,))


method_help = proxy.system.methodHelp(method_name)
if method_help:
print(' ', method_help)


if name == 'main':
main()


The introspection mechanism is not just an optional extension, and it is not actually defined in the XML-RPC
specification itself! It lets the client call a series of special methods that all begin with the string system to distinguish
them from normal methods. These special methods give information about the other calls available. Let’s start by
calling listMethods(). If introspection is supported at all, then you will receive back a list of other method names.
For this example listing, let’s ignore the system methods and only proceed to print out information about the other ones.

Free download pdf