سؤال

Let's have a simple xmlrpc server defined as in the following code:

from SimpleXMLRPCServer import SimpleXMLRPCServer

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

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(add, 'addthem')
server.register_function(add, 'add.numbers')
server.register_function(add, 'sum.two.numbers')
server.serve_forever()

which you can connect to via

import xmlrpclib
dev = xmlrpclib.ServerProxy("http://localhost:8000/RPC2")

With the dev object, you cann access the (for reasons of simplicity same) function add in the server, like

print dev.addthem(1,2)
print dev.add.numbers(1,2)
print dev.sum.two.numbers(1,2)

My question: What are the pieces of those calls? What is dev (I suppose an instance of xmlrpclib.ServerProxy), what is sum in dev.sum (a function? a callable? a class? an instance?). What is two in dev.sum.two ...

For example, the following syntax

print dev.add

results in an error

xmlrpclib.Fault: <Fault 1: '<type \'exceptions.Exception\'>:method "add.__str__" is not supported'>

Shouldn't that print something always? What is dev.add and the other pieces?

هل كانت مفيدة؟

المحلول

In order to "log" requests, you could (for example) implements a custom Transport in your client.

From the doc (adapted):

import xmlrpclib, httplib, gzip

class LogTransport(xmlrpclib.Transport):
    def send_request(self, connection, handler, request_body):
        # log here.
        print handler, request_body
        if (self.accept_gzip_encoding and gzip):
            connection.putrequest("POST", handler, skip_accept_encoding=True)
            connection.putheader("Accept-Encoding", "gzip")
        else:
            connection.putrequest("POST", handler)

p = LogTransport()
server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)
print server.currentTime.getCurrentTime()

Output:

/RPC2 <?xml version='1.0'?>
<methodCall>
<methodName>currentTime.getCurrentTime</methodName>
<params>
</params>
</methodCall>

and a server error (because it doesn't exists)

نصائح أخرى

In this case, sum.two.numbers is just the function to be called on the server. On this line server.register_function(add, 'sum.two.numbers') you are setting that method to map to the add function which takes 2 arguments. All of your server's available methods are mapped to add currently. dev.add is just telling the client to run the method add on the server. Because you do not map add to the actual function on the server, you cannot use it and an error is thrown.

Check out this example which includes a custom SimpleXMLRPCRequestHandler where you can customize it to add logging.

Take a look at this if you are still having problems with the logging: XML-RPC server logging

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top