[Twisted-Python] Another silly XML-RPC question
Hi all, This is maybe just a Python library question, but I'm not sure. I'd like to set up an XML-RPC server function to accept a dictionary/struct. So I thought I'd do this... def xmlrpc_foo(self, **mydict): But when I send it a struct, it throws an error of '0 args expected, 1 given,' much like...
def blah(**myDict): return myDict['x']
print blah({'x':42, 'y':3})
...does. So under regular Python, the way to make this work fine is:
def blah(myDict): return myDict['x']
print blah({'x':42, 'y':3})
But when I try this under twisted... def xmlrpc_foo(self, myDict): return myDict['x'] I get a key error, saying strings are not callable. So how do I get a twisted xmlrpc function to accept a struct? I assume I have to take it into a string and somehow serialize it into a dictionary using XML-RPC magic I haven't learned about, as in... def xmlrpc_foo(self, myDict): ...magically transform myDict (a string) into myNewDict (a dict!)... return myNewDict['x'] Any help is appreciated! Thanks! Steve
On Mon, 2003-08-25 at 20:50, Steve Freitas wrote:
Hi all,
This is maybe just a Python library question, but I'm not sure.
I'd like to set up an XML-RPC server function to accept a dictionary/struct. So I thought I'd do this...
def xmlrpc_foo(self, **mydict):
But when I send it a struct, it throws an error of '0 args expected, 1 given,' much like...
This will never work since XML-RPC doesn't support **kw arguments.
def blah(**myDict): return myDict['x']
print blah({'x':42, 'y':3})
...does. So under regular Python, the way to make this work fine is:
def blah(myDict): return myDict['x']
print blah({'x':42, 'y':3})
But when I try this under twisted...
def xmlrpc_foo(self, myDict): return myDict['x']
I get a key error, saying strings are not callable.
So how do I get a twisted xmlrpc function to accept a struct? I assume I have to take it into a string and somehow serialize it into a dictionary using XML-RPC magic I haven't learned about, as in...
def xmlrpc_foo(self, myDict): ...magically transform myDict (a string) into myNewDict (a dict!)... return myNewDict['x']
Any help is appreciated! Thanks!
No, you just send a python dictionary. XML-RPC will serialize it as a struct and it will be converted back to a Python dict before it is passed to your handler. If you're using Twisted on the client side as well: proxy.callRemote('foo', {'x':42}) dave
Dave Peticolas wrote:
On Mon, 2003-08-25 at 20:50, Steve Freitas wrote:
So how do I get a twisted xmlrpc function to accept a struct? I assume I have to take it into a string and somehow serialize it into a dictionary using XML-RPC magic I haven't learned about, as in...
def xmlrpc_foo(self, myDict): ...magically transform myDict (a string) into myNewDict (a dict!)... return myNewDict['x']
Any help is appreciated! Thanks!
No, you just send a python dictionary. XML-RPC will serialize it as a struct and it will be converted back to a Python dict before it is passed to your handler.
If you're using Twisted on the client side as well:
proxy.callRemote('foo', {'x':42})
Or you can use objects instead of structs, jelly them, and send the jellied objects over xmlrpc -- xmlrpc has no problem with nested lists, which is what jelly produces. Good ol' jelly! Caveat: the objects' attributes have to be jellyable (some types aren't). This is analogous to the PB "copyable" paradigm. - Steve.
Hi Steve,
Or you can use objects instead of structs, jelly them, and send the jellied objects over xmlrpc -- xmlrpc has no problem with nested lists, which is what jelly produces. Good ol' jelly! Caveat: the objects' attributes have to be jellyable (some types aren't). This is analogous to the PB "copyable" paradigm.
As I noted in my other reply, I'm unfortunately using C++ on the client side. Any other ideas on how I get Twisted to turn my struct into a dictionary? Steve
Hi Dave, Thanks for your reply!
No, you just send a python dictionary. XML-RPC will serialize it as a struct and it will be converted back to a Python dict before it is passed to your handler.
If you're using Twisted on the client side as well:
proxy.callRemote('foo', {'x':42})
Unfortunately I'm using C++ on the client side, with XmlRpc++ as my library (http://xmlrpcpp.sourceforge.net/). I'd like to avoid putting Python in that mix, as I wasn't planning on using it on the client at all. Besides, I thought this was what I was doing when I sent my Twisted server a simple struct. If XML-RPC serializes it as a struct (which was what I was sending), what am I doing wrong to prevent it from working as you describe? Thanks! Steve
On Tue, 2003-08-26 at 13:51, Steve Freitas wrote:
Hi Dave,
Thanks for your reply!
No, you just send a python dictionary. XML-RPC will serialize it as a struct and it will be converted back to a Python dict before it is passed to your handler.
If you're using Twisted on the client side as well:
proxy.callRemote('foo', {'x':42})
Unfortunately I'm using C++ on the client side, with XmlRpc++ as my library (http://xmlrpcpp.sourceforge.net/). I'd like to avoid putting Python in that mix, as I wasn't planning on using it on the client at all.
Besides, I thought this was what I was doing when I sent my Twisted server a simple struct. If XML-RPC serializes it as a struct (which was what I was sending), what am I doing wrong to prevent it from working as you describe?
A struct should be unserialized to a Python dict no problem. It's hard to say from here what is going on, but Twisted's XMLRPC class is so simple it should be easy to add a few print statements to find out what is happening. Check out the XMLRPC.render(...) function. You might want to print the request content and unserialized arguments as a starting point. dave
participants (3)
-
Dave Peticolas -
Steve Freitas -
Steve Waterbury