pacodelumberg
pacodelumberg

Reputation: 2274

python XML-RPC sending a list of parameters to the server

I am trying to send a list( specifically numpy or python's list) of numbers and get the sum of them using xml-rpc, to be familiar with the environment. I get an error always in the client side.

<Fault 1: "<type 'exceptions.TypeError'>:unsupported operand type(s) for +: 'int' and 'list'">

Server side code:

from SimpleXMLRPCServer import SimpleXMLRPCServer
def calculateOutput(*w):
    return sum(w);

server = SimpleXMLRPCServer(("localhost", 8000))
print "Listening on port 8000..."
server.register_function(calculateOutput,"calculateOutput");
server.serve_forever()

Client side code:

import xmlrpclib
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
print(proxy.calculateOutput([1,2,100]);

Does anyone know how to fix this problem?

Upvotes: 2

Views: 2725

Answers (1)

Christian Witts
Christian Witts

Reputation: 11585

Send through proxy.calculateOutput([1,2,100]) as proxy.calculateOutput(1,2,100) or change the arguments for your server-side function from def calculateOutput(*w): to def calculateOutput(w):.

As an aside, you don't need the semi-colons.

The reason for this behaviour can be illustrated with a short example

>>> def a(*b):
>>>    print b

>>> a(1,2,3)
(1, 2, 3)
>>> a([1,2,3])
([1, 2, 3],)

As you can see from the outputs, using the magic asterix will package up however many arguments you pass through to the function as a tuple itself so it can handle n amount of arguments. As you were using that syntax, when you sent through your arguments already contained in a list they were then packaged further into a tuple. sum() only expects a list/tuple as the argument, hence the error you were receiving when it tried to sum a contained list.

Upvotes: 4

Related Questions