Python - 将参数作为结构发送

时间:2017-04-07 14:40:20

标签: python struct

我正在使用一个API,要求我提供3个参数。

string sessionKey
int    serverId
struct date details
       dateTime.iso8601 startDate - Optional, unless endDate is provided.
       dateTime.iso8601 endDate - Optional. 

我不确定如何在这种情况下定义结构。我这样称呼API。

>>> server.system.provisioning.snapshot.listSnapshots(sessionKey,1000078204, {datetime.now() , datetime.now()})

所以我用语法定义我的struct

{datetime.now() , datetime.now()}

这给了我一个错误:

Traceback (most recent call last):
  File "/usr/lib64/python3.4/xmlrpc/client.py", line 512, in __dump
f = self.dispatch[type(value)]
KeyError: <class 'set'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.4/xmlrpc/client.py", line 1098, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib64/python3.4/xmlrpc/client.py", line 1431, in __request
    allow_none=self.__allow_none).encode(self.__encoding)
  File "/usr/lib64/python3.4/xmlrpc/client.py", line 951, in dumps
    data = m.dumps(params)
  File "/usr/lib64/python3.4/xmlrpc/client.py", line 504, in dumps
    dump(v, write)
  File "/usr/lib64/python3.4/xmlrpc/client.py", line 516, in __dump
    raise TypeError("cannot marshal %s objects" % type(value))
TypeError: cannot marshal <class 'set'> objects

我是否定义了struct错误?

1 个答案:

答案 0 :(得分:-1)

在XML-RPC(你的错误追溯表明你正在使用它)中,&#34; struct&#34;是Python称之为&#34; dict(ionary)&#34;,但您尝试使用set代替。您需要使用API​​文档中提供的密钥名称(即dictstartDate)创建endDate,因此:

>>> server.system.provisioning.snapshot.listSnapshots(sessionKey,1000078204, {"startDate": datetime.now(), "endDate": datetime.now()})