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?
Send through
proxy.calculateOutput([1,2,100])asproxy.calculateOutput(1,2,100)or change the arguments for your server-side function fromdef calculateOutput(*w):todef calculateOutput(w):.As an aside, you don’t need the semi-colons.
The reason for this behaviour can be illustrated with a short example
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
namount 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.