Keyword args to SimpleXMLRPCServer

S

Sean DiZazzo

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'

~Sean
 
S

Sean DiZazzo

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'

~Sean

PS. The same thing happens if you don't use **kwargs...

....
class XMLFunctions(object):
def returnArgs(foo, bar=None, baz=None):
return foo, bar, baz
....
 
T

Terry Reedy

| 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.

Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
f(bar='baz')
TypeError: f() takes no arguments (1 given)

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

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
 
G

Gabriel Genellina

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()

You forget the self argument. But this is not the problem. XMLRPC does not
allow passing parameters by name, only positional parameters. See
http://www.xmlrpc.com/spec:

"If the procedure call has parameters, the <methodCall> must contain a
<params> sub-item. The <params> sub-item can contain any number of
<param>s, each of which has a <value>. "

Parameters have no <name>, just a <value>, so you can only use positional
parameters. But you can simulate keyword arguments by collecting them in a
dictionary and passing that dictionary as an argument instead.

(server side)
def returnArgs(self, x, y, other={}):
return x, y, other

(client side)
print server.returnArgs("xvalue", "yvalue", dict(foo=123, bar=234,
baz=345))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top