By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,931 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

Keyword args to SimpleXMLRPCServer

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

"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

P: n/a
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.