I just started using a XMLRPC server and clients to connect my raspberry pi to a computer.
My server looks like this:
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import numpy as np
allow_reuse_address = True
ip = '...'
port = 8000
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
server = SimpleXMLRPCServer((ip, port), requestHandler=RequestHandler)
server.register_introspection_functions()
def Spectrum():
data = ... # it's a numpy array
return data
server.register_function(Spectrum, 'Spectrum')
server.serve_forever()
My client looks like this:
#!/usr/bin/env python
import xmlrpclib
import numpy as np
[...]
def getSpectrum():
try:
s = xmlrpclib.ServerProxy(server)
v = s.Spectrum()
print v
except:
print "no data"
My server is running and my test function shows that it works. But my function getSpectrum() always throws an exception. I figured out that it works fine if my return value is a float instead of a numpy array:
def Spectrum():
data = ... # it's a numpy array
return float(data[0][0])
I don't have any idea what's wrong but I think it should be possible to return a numpy array. Do you know how to fix that?
The xmlrpclib only supports marshalling of standard python types. numpy arrays are an extension type and therefor cannot be serialized out of the box. A simple solution would be to return a list representation of the numpy array and when you receive the data you turn it into a numpy array again:
def Spectrum():
data = ... # it's a numpy array
return data.tolist()
...
def getSpectrum():
try:
s = xmlrpclib.ServerProxy(server)
v = numpy.asarray(s.Spectrum())
...