2010-09-21 130 views
6

我建立xml rpc服務使用扭曲,我想使用None,就像它可以在標準的Python庫中完成。如何將allow_none傳遞給xmlrpc服務器的扭曲版本?allow_none在扭曲的XML-RPC服務器

編輯

In [28]: sock = rpc.ServerProxy('http://localhost:7080',allow_none=True) 

In [29]: sock 
Out[29]: <ServerProxy for localhost:7080/RPC2> 

In [30]: sock.list_reports() 
Out[30]: ['example'] 

In [31]: sock.run_report('example') 
--------------------------------------------------------------------------- 
Fault          Traceback (most recent call last) 

reports/<ipython console> in <module>() 

/usr/lib/python2.6/xmlrpclib.pyc in __call__(self, *args) 
    1197   return _Method(self.__send, "%s.%s" % (self.__name, name)) 
    1198  def __call__(self, *args): 
-> 1199   return self.__send(self.__name, args) 
    1200 
    1201 ## 


/usr/lib/python2.6/xmlrpclib.pyc in __request(self, methodname, params) 
    1487    self.__handler, 
    1488    request, 
-> 1489    verbose=self.__verbose 
    1490   ) 
    1491 

/usr/lib/python2.6/xmlrpclib.pyc in request(self, host, handler, request_body, verbose) 
    1251    sock = None 
    1252 
-> 1253   return self._parse_response(h.getfile(), sock) 
    1254 
    1255  ## 


/usr/lib/python2.6/xmlrpclib.pyc in _parse_response(self, file, sock) 
    1390   p.close() 
    1391 
-> 1392   return u.close() 
    1393 
    1394 ## 


/usr/lib/python2.6/xmlrpclib.pyc in close(self) 
    836    raise ResponseError() 
    837   if self._type == "fault": 
--> 838    raise Fault(**self._stack[0]) 
    839   return tuple(self._stack) 
    840 

Fault: <Fault 8002: "Can't serialize output: cannot marshal None unless allow_none is enabled"> 

回答

8

XMLRPC接受allowNone作爲其初始值設定項的參數。因此,如果您想支持None,請在實例化資源時通過True

from twisted.web.xmlrpc import XMLRPC 
resource = XMLRPC(allowNone=True) 
+0

是的,這項工作很棒。非常感謝。 – gruszczy 2010-09-21 12:50:36

0

我覺得應該在客戶端指定...

當您創建從你的XML-RPC客戶端代理 - 您可以通過關鍵字參數AL​​LOW_NONE = True以蟒蛇的xmlrpclib模塊像下面...

In [184]: import xmlrpclib as rpc 

In [185]: sock = rpc.ServerProxy('http://localhost/xmlrpc/object',allow_none=True) 

編輯:

from twisted.web.xmlrpc import Proxy 
proxy = Proxy('http://localhost/xmlrpc', allowNone=True) 
+1

僅僅在客戶端定義這是不夠的。 – gruszczy 2010-09-21 12:50:59

+1

正確,您還需要在服務器端指定此項。 server = SimpleXMLRPCServer((「0.0.0.0」,8000),allow_none = True) – db42 2012-01-06 10:01:21