471,594 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Keyword args to SimpleXMLRPCServer

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,Simp leXMLRPCServer):
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
Dec 18 '07 #1
3 4051
On Dec 17, 4:13 pm, Sean DiZazzo <half.ital...@gmail.comwrote:
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,Simp leXMLRPCServer):
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
....
Dec 18 '07 #2

"Sean DiZazzo" <ha**********@gmail.comwrote in message
news:15**********************************@e6g2000p rf.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,Simp leXMLRPCServer):
| 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

Dec 18 '07 #3
En Mon, 17 Dec 2007 21:13:32 -0300, Sean DiZazzo <ha**********@gmail.com>
escribió:
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,Simp leXMLRPCServer):
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 <methodCallmust contain a
<paramssub-item. The <paramssub-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))

--
Gabriel Genellina

Dec 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Marco Aschwanden | last post: by
9 posts views Thread by Yannick Turgeon | last post: by
4 posts views Thread by codecraig | last post: by
10 posts views Thread by David Murmann | last post: by
reply views Thread by Thomas G. Apostolou | last post: by
reply views Thread by Jeremy Monnet | last post: by
reply views Thread by Juju | last post: by
3 posts views Thread by Achim Domma | last post: by
9 posts views Thread by Bret | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.