Keyword args to SimpleXMLRPCServer

2007-12-17 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyword args to SimpleXMLRPCServer

2007-12-17 Thread Sean DiZazzo
On Dec 17, 4:13 pm, Sean DiZazzo [EMAIL PROTECTED] wrote:
 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
...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyword args to SimpleXMLRPCServer

2007-12-17 Thread Terry Reedy

Sean DiZazzo [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
| 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





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyword args to SimpleXMLRPCServer

2007-12-17 Thread Gabriel Genellina
En Mon, 17 Dec 2007 21:13:32 -0300, Sean DiZazzo [EMAIL PROTECTED]  
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,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  
params, 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

-- 
http://mail.python.org/mailman/listinfo/python-list