2012-06-06 33 views
1

我想發送一個列表(特別是numpy或python的列表)的數字,並獲得它們的總和,使用xml-rpc,以熟悉環境。我總是在客戶端發生錯誤。python XML-RPC將參數列表發送到服務器

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

服務器端代碼:

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() 

客戶端代碼:

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

有誰知道如何解決這個問題?

回答

3

通過proxy.calculateOutput([1,2,100])發送爲proxy.calculateOutput(1,2,100)或將您的服務器端函數的參數從def calculateOutput(*w):更改爲def calculateOutput(w):

順便說一句,你不需要分號。

其原因行爲可以用一個短的例子

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

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

正如可以從輸出看到的,使用魔法星號將打包你經過給函數爲元組然而,許多參數來說明本身就可以處理n的參數數量。當你使用這種語法時,當你發送已經包含在列表中的參數時,它們會被進一步打包成一個元組。 sum()只需要列表/元組作爲參數,因此當您嘗試對包含列表進行求和時,您收到的錯誤。