Right way to define methods to SimpleXMLRPCServer?

S

Skip Montanaro

I decided to use SimpleXMLRPCServer for an xml-rpc server. As I understand
it, if I register an instance like so:

myserver.register_instance(MyStuff())

then it will call MyStuff()._dispatch(name, *args, **kwds) to try and
execute the method. Since _dispatch is generic, you can't really define a
more elaborate argument list. I defined _dispatch like so:

def _dispatch(self, name, *args, **kwds):
print {"args": args, "kwds": kwds}
try:
meth = self._map[name]
except KeyError:
raise ValueError, "Invalid method: %s" % name
else:
return meth(self, *args, **kwds)

The **kwds parameter can probably be omitted since XML-RPC only supports
positional arguments.

When I call a method on the server with no args _dispatch is called, and the
server prints this:

{'args': ((),), 'kwds': {}}

Seems to me like it has one nesting level too many in the args tuple.
Shouldn't it be ()? Is this a bug or am I misinterpreting something?

Skip
 
D

Diez B. Roggisch

When I call a method on the server with no args _dispatch is called, and
the server prints this:

{'args': ((),), 'kwds': {}}

Seems to me like it has one nesting level too many in the args tuple.
Shouldn't it be ()? Is this a bug or am I misinterpreting something?

Calling a function without any args always yields an empty tuple - like
this:


Looking at SimpleXMLRPCServer one sees this:

params, method = xmlrpclib.loads(data)
# generate response
try:
if dispatch_method is not None:
response = dispatch_method(method, params)


So the argument list is the second argument, which is probably the empty
tuple- that explains your observations.

Looking into the docs, you see that dispatch is supposed to have this
signature:

def _dispatch(self, method, params):

No * here, as well as keywords. Then things are more clear :)
 
S

Skip Montanaro

Diez> Looking into the docs, you see that dispatch is supposed to have this
Diez> signature:

Diez> def _dispatch(self, method, params):

Thanks. The _dispatch interface isn't defined in the SimpleXMLRPCServer
docs. Your comment made me try pydoc. I'll update the LaTeX docs.

Skip
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top