Keyword args to SimpleXMLRPCServer
Terry Reedy
tjreedy at udel.edu
Mon Dec 17 20:10:50 EST 2007
"Sean DiZazzo" <half.italian at gmail.com> wrote in message
news:15144ed7-05a1-4812-8cf3-4cc8aba9c949 at e6g2000prf.googlegroups.com...
| Why is the following not working? Is there any way to get keyword
| arguments working with exposed XMLRPC functions?
|
| ~~~~~~~~~~~~~~~~ server.py
| import SocketServer
| from SimpleXMLRPCServer import
| SimpleXMLRPCServer,SimpleXMLRPCRequestHandler
|
| # Threaded mix-in
| class
| AsyncXMLRPCServer(SocketServer.ThreadingMixIn,SimpleXMLRPCServer):
| pass
|
| class XMLFunctions(object):
| def returnArgs(*args, **kwargs):
| return kwargs.items()
|
| # Instantiate and bind to localhost:1234
| server = AsyncXMLRPCServer(('', 8080), SimpleXMLRPCRequestHandler)
|
| # Register example object instance
| server.register_instance(XMLFunctions())
|
| # run!
| server.serve_forever()
|
| ~~~~~~~~~~~~~~~~ client.py
| from xmlrpclib import ServerProxy, Error
|
| server = ServerProxy("http://localhost:8080", allow_none=1) # local
| server
|
| try:
| print server.returnArgs("foo", bar="bar", baz="baz")
| except Error, v:
| print "ERROR", v
|
|
| [seans-imac:~/Desktop/] halfitalian% ./client.py
| Traceback (most recent call last):
| File "./XMLRPC_client.py", line 9, in <module>
| print server.returnArgs("foo", bar="bar", baz="baz")
| TypeError: __call__() got an unexpected keyword argument 'bar'
In general, C function do not recognize keyword arguments.
But the error message above can be reproduced in pure Python.
>>> def f(): pass
>>> f(bar='baz')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
f(bar='baz')
TypeError: f() takes no arguments (1 given)
>>> def f(x): pass
>>> f(bar='baz')
Traceback (most recent call last):
File "<pyshell#5>", line 1, in -toplevel-
f(bar='baz')
TypeError: f() got an unexpected keyword argument 'bar'
Whereas calling a C function typically gives
>>> ''.join(bar='baz')
Traceback (most recent call last):
File "<pyshell#6>", line 1, in -toplevel-
''.join(bar='baz')
TypeError: join() takes no keyword arguments
But I don't know *whose* .__call__ method got called,
so I can't say much more.
tjr
More information about the Python-list
mailing list