2015-08-15 90 views
1

我将操作系统更新为Centos 7.现在我有了Python 2.7。python can not marshal <class'decimal.Decimal'> objects

下面的代码在Python 2.6中工作,但现在它不起作用。

OS: Centos 7 
Python 2.7.5 
Apache/2.4.6 

我do_POST是:

def do_POST(self): 
    """Handles the HTTP POST request. 

    Attempts to interpret all HTTP POST requests as XML-RPC calls, 
    which are forwarded to the _dispatch method for handling. 
    """ 

    try: 
     # get arguments 
     data = self.rfile.read(int(self.headers["content-length"])) 
     params, method = xmlrpclib.loads(data) 

     # generate response 
     try: 
      response = self._dispatch(method, params) 
      # wrap response in a singleton tuple 
      response = (response,) 
     except XMLRPCFault: 
      # report exception back to server 
      response = xmlrpclib.dumps(
       xmlrpclib.Fault(1, "%s" % (sys.exc_info()[1])) 
       ) 
     else: 
      response = xmlrpclib.dumps(response, methodresponse=1) 
    except: 
     logException(LOG_ERROR,"XMLRPCServer") 
     # internal error, report as HTTP server error 
     self.send_response(500) 
     self.end_headers() 
    else: 
     # got a valid XML RPC response 
     self.send_response(200) 
     self.send_header("Content-type", "text/xml") 
     self.send_header("Content-length", str(len(response))) 
     self.end_headers() 
     self.wfile.write(response) 

     # shut down the connection 
     self.wfile.flush() 
     self.connection.shutdown(1) 

的错误是:

2015/08/15-11:49:36 XMLRPCServer 
Traceback (most recent call last): 
    File "/usr/local/IBSng/core/server/xmlrpcserver.py", line 53, in do_POST 
    response = xmlrpclib.dumps(response, methodresponse=1) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 1085, in dumps 
    data = m.dumps(params) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 632, in dumps 
    dump(v, write) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 654, in __dump 
    f(self, value, write) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 735, in dump_struct 
    dump(v, write) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 654, in __dump 
    f(self, value, write) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 735, in dump_struct 
    dump(v, write) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 654, in __dump 
    f(self, value, write) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 735, in dump_struct 
    dump(v, write) 
    File "/usr/lib64/python2.7/xmlrpclib.py", line 646, in __dump 
    raise TypeError, "cannot marshal %s objects" % type(value) 
TypeError: cannot marshal <class 'decimal.Decimal'> objects 

回答

1

xmlrpclib模块不支持decimal.Decimal对象开箱,没有。

您有两种选择:将Decimal对象先转换为浮动对象,或者扩展编组以便明确处理Decimal对象。

转换的Decimal对象是简单的:

from decimal import Decimal 

# .. 
def convert_decimal_to_float(ob): 
    if isinstance(ob, Decimal): 
     return float(ob) 
    if isinstance(ob, (tuple, list)): 
     return [convert_decimal_to_float(v) for v in ob] 
    if isinstance(ob, dict): 
     return {k: convert_decimal_to_float(v) for k, v in ob.iteritems()} 
    return ob 

response = self._dispatch(method, params) 
response = (convert_decimal_to_float(response),) 

支持添加到库看起来是这样的:

from xmlrpclib import Marshaller 
from decimal import Decimal 

def dump_decimal(value, write): 
    write("<value><double>") 
    write(str(value)) 
    write("</double></value>\n") 

Marshaller.dispatch[Decimal] = dump_decimal 
+0

或第三个选项 - 在'Decimal'对象转换为字符串AA首先,然后转换回“Decimal” –

+0

@MarkChackerian:但是这需要XML-RPC服务器端支持接受这样的字符串值。 –

+0

是 - 只有一个可行的选项,如果你可以改变服务器端。 –