【问题标题】:python XML-RPC sending a list of parameters to the serverpython XML-RPC向服务器发送参数列表
【发布时间】:2012-06-10 13:20:32
【问题描述】:

我正在尝试发送一个数字列表(特别是 numpy 或 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]);

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: python xml-rpc rpc


    【解决方案1】:

    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],)
    

    正如您从输出中看到的那样,使用魔法 asterix 会将您传递给函数的许多参数打包为一个元组本身,因此它可以处理 n 数量的参数。当您使用该语法时,当您发送已包含在列表中的参数时,它们会被进一步打包到一个元组中。 sum() 只需要一个列表/元组作为参数,因此当它尝试对包含的列表求和时会收到错误。

    【讨论】:

      猜你喜欢
      • 2010-12-07
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多