Python xmlrpc servers?

Skip Montanaro skip at pobox.com
Wed Dec 1 13:38:42 EST 2004


    ted> The example given in the Python documentation for
    ted> SimpleXMLRPCServer is more or less incomprehensible.  

Agreed, there is a doc fix needed.  Try mentally adding

    from math import *

to the start of the example.  That will get you the pow function.  It's
still incorrect though.  There's no div function, either builtin or in the
math module.  The MyFuncs class should be defined as in the 2.4 docs:

    class MyFuncs:
        def div(self, x, y) : return x // y

    ted> I don't see any relationship between the class MyFuncs and the rest
    ted> of the program.

In this simpleminded example, there is none.  The goal of the example is to
show how the register_* methods are used.  In a real-life application any
instances registered would probably store considerable parts of the
application state or be able to communicate with those objects that do.

    ted> I don't see what "lambda" is or how a lambda function is supposed
    ted> to be construed as adding two numbers together.

Lambda is a keyword in Python used to create and return very simple
(single-expression) functions.  Lambda expressions can be used anywhere
you'd normally use a function object.  See:

    http://www.python.org/doc/current/ref/lambdas.html

The line containing the lambda expression:

    server.register_function(lambda x,y: x+y, 'add')

could be recast as:

    def add(x, y):
        return x+y

    server.register_function(add, 'add')

though the second arg isn't required since it matches the function's
__name__ attribute.  It's required when a lambda expression is used though
because lambda expressions don't have terribly useful __name__ attributes:

    >>> f = lambda x,y: x+y
    >>> f
    <function <lambda> at 0x65c970>
    >>> f.__name__
    '<lambda>'
    >>> def add(x,y):
    ...   return x+y
    ... 
    >>> add.__name__
    'add'

    ted> And then, I find one example in an IBM reference which actually
    ted> does work as stated on a single computer:
    ...
    ted> which actually works.  Nonetheless I need this thing to work across
    ted> machines.

The server listens to "localhost" on port 8888.  To allow it to listen for
external connections change "localhost" to the name or IP address of the
server.  Before you do that make sure you understand the ramifications of
exposing your XML-RPC server to a broader class of potential clients, some
of which are bound to be malicious.

Skip



More information about the Python-list mailing list