User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,600 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,443 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 771 | Replies: 2
Reply
Join Date: Aug 2007
Posts: 4
Reputation: spinaltoad is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spinaltoad spinaltoad is offline Offline
Newbie Poster

help de-serialize XML-RPC string

  #1  
Sep 6th, 2007
I cannot figure out the correct way to de-serialize a string passed via xml-rpc. If I print out the string I get
{'params': ['123', 3], 'methodName': 'function'}

I could split the string, and then drop unwanted characters. But, that seems like the wrong way.

Any help would be appreciated.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: help de-serialize XML-RPC string

  #2  
Sep 11th, 2007
I'm mostly ignorant of these matters, but it looks to me like your string is just a dictionary. So:

  1. >>> s = "{'params': ['123', 3], 'methodName': 'function'}"
  2.  
  3. >>> result = eval(s)
  4. >>> result
  5. {'methodName': 'function', 'params': "('123', 3)"}
  6. >>> print result['params']
  7. ['123', 3]
  8. >>> print result['methodName']
  9. function

So now you've deserialized it to a dictionary containing the function call and the parameters. To make the call, you could then do:

  1. def function(a,b):
  2. print a,b # setup for testing only
  3.  
  4. >>> result["params"] = '(' + str(result["params"])[1:-1] + ')'
  5. >>> result["params"] # just to see
  6. "('123', 3)"
  7. >>> result["methodName"] + result["params"]
  8. "function('123', 3)"
  9. >>> return_val = eval(result["methodName"]+result["params"])
  10. 123 3

So we turn the function call into a string and then evaluate it.

HOWEVER, comma,

I am 99.9% certain that the xmlrpclib already has some function to execute the return string for you, probably with appropriate safety measures to keep malicious remote function calls from occurring. Poke around the xmlrpclib file and see.

Jeff
Reply With Quote  
Join Date: Aug 2007
Posts: 4
Reputation: spinaltoad is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spinaltoad spinaltoad is offline Offline
Newbie Poster

Re: help de-serialize XML-RPC string

  #3  
Sep 12th, 2007
Thank you for the help. It will come in handy.

I was luckily enough to be the one working on both the xmlrpc call and the parsing. I was able to found a short cut around it though.

I was using c++ and the XmlRpc++ library. I assigned each variable each own parameter ( param1, param2, param3, etc. It ended up sending a string that looked just like a data list type.

{'param1': '123', 'param2': '3' , 'methodName': 'function'}

I thought was cool, because all I had to do was declare a new data list and call out the variables.

  1. def subtractCredit(self, arg):
  2. d2 = arg[0]
  3. print "Recieved Arg:", arg[0]
  4. print "methodName:", d2['methodName']
  5. print "pin:", d2['param1']
  6. print "amount:", d2['param2']

I will have to look into securing the xmlrpc server. I was thinking about using twisted.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 7:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC