开发者

Why can't xmlrpc client append item to list accessable via xmlrpc server procedure?

开发者 https://www.devze.com 2023-02-21 19:11 出处:网络
Server code (based on Python library reference): from xmlrpc.server import SimpleXMLRPCServer from xmlrpc.server import SimpleXMLRPCRequestHandler

Server code (based on Python library reference):

from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ()

server = SimpleXMLRPCServer(("127.0.0.1", 8000),
                            requestHandler=RequestHandler)
server.register_introspection_functions()

l = list()

def say_hi():
    return 'hi !'

def append(event):
    l.append(event)

server.register_function(say_hi)
server.register_function(append)

server.serve_forever()

Client (interpreter started from another terminal window):

>>> from xmlrpc.client import ServerProxy
>>> s = ServerProxy('http://127.0.0.1', allow_none=True)
>>> s.say_hi()
'hi !'
>>> s.append(1)
Traceback (most recent call last):
...
xmlrpc.client.Fault(Fault 1: "<class 'TypeError'>:cannot
                    marshal None unless allow_none is enabled")

How do I fix this? Am I using xml开发者_Go百科rpc improperly?


Your XMLRPC server is raising a fault since it cannot marshal None. You need to add allow_none=True to the server constructor:

server = SimpleXMLRPCServer(("127.0.0.1", 8000),
                        requestHandler=RequestHandler, 
                        allow_none=True)


The error message is self-speaking.

append() returns None which can not be marshalled unless you specify allow_none.

Reading error messages and the API documentation

http://docs.python.org/library/simplexmlrpcserver.html

is your friend.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号