Differences between revisions 2 and 4 (spanning 2 versions)
Revision 2 as of 2004-07-20 04:42:52
Size: 1036
Editor: dsl254-010-130
Comment:
Revision 4 as of 2004-09-09 08:14:59
Size: 2610
Editor: aaron
Comment:
Deletions are marked like this. Additions are marked like this.
Line 8: Line 8:
You can [wiki:Wiki/XmlRpc read more about it over on C2,] or on [http://www.xml-rpc.com/ the XML-RPC home page.] You can [wiki:Wiki/XmlRpc read more about it over on C2,] or on [http://www.xmlrpc.com/ the XML-RPC home page.]
Line 25: Line 25:
== Message Format ==

If you can communicate strings, you can do XML-RPC. You could even do it by e-mail!

Here's how to make your string:

{{{
#!python
import xmlrpclib

func_name = "foo"
arg_1 = "robot"
arg_2 = { "some":1, "dict":2 }
arg_3 = [1,2,3,4,5]

call_string = xmlrpclib.dumps( (arg_1,arg_2,arg_3,), func_name )
}}}

...resulting in the following {{{call_string}}} value:

{{{
<?xml version='1.0'?>
<methodCall>
<methodName>foo</methodName>
<params>
<param>
<value><string>robot</string></value>
</param>
<param>
<value><struct>
<member>
<name>dict</name>
<value><int>2</int></value>
</member>
<member>
<name>some</name>
<value><int>1</int></value>
</member>
</struct></value>
</param>
<param>
<value><array><data>
<value><int>1</int></value>
<value><int>2</int></value>
<value><int>3</int></value>
<value><int>4</int></value>
<value><int>5</int></value>
</data></array></value>
</param>
</params>
</methodCall>
}}}

...which can then be turned ''back'' into Python data:

{{{
#!python
call_data = xmlrpclib.loads( call_string )
}}}

...which then builds:

{{{
(('robot', {'some': 1, 'dict': 2}, [1, 2, 3, 4, 5]), u'foo')
}}}

That is, the first item is the arguments tuple, and the second item is the name of the function.

For some reason, these capabilities do not seem to be described in [http://www.python.org/doc/current/lib/module-xmlrpclib.html the xmlrpclib documentation.]
Line 29: Line 99:
 * [http://www.onlamp.com/pub/a/python/2001/01/17/xmlrpcserver.html XML-RPC: It Works Both Ways (onlamp.com)]

XML-RPC

XML-RPC is a neat way to send messages across the Internet.

The neat thing about XML-RPC is that it transports native data structures- you can ship off lists, strings, dictionaries, and numbers.

You can [wiki:Wiki/XmlRpc read more about it over on C2,] or on [http://www.xmlrpc.com/ the XML-RPC home page.]

Sample Code

   1 import xmlrpclib
   2 
   3 XMLRPC_SERVER_URL = "http://www.python.org/cgi-bin/moinmoin/?action=xmlrpc"
   4 
   5 pythoninfo = xmlrpclib.ServerProxy( XMLRPC_SERVER_URL )
   6 allpages = pythoninfo.getAllPages() # this is the XML-RPC call
   7 
   8 print ", ".join( allpages )

This code calls the PythonInfo wiki, and receives the TitleIndex as a list.

Message Format

If you can communicate strings, you can do XML-RPC. You could even do it by e-mail!

Here's how to make your string:

   1 import xmlrpclib
   2 
   3 func_name = "foo"
   4 arg_1 = "robot"
   5 arg_2 = { "some":1, "dict":2 }
   6 arg_3 = [1,2,3,4,5]
   7 
   8 call_string = xmlrpclib.dumps( (arg_1,arg_2,arg_3,), func_name )

...resulting in the following call_string value:

<?xml version='1.0'?>
<methodCall>
<methodName>foo</methodName>
<params>
<param>
<value><string>robot</string></value>
</param>
<param>
<value><struct>
<member>
<name>dict</name>
<value><int>2</int></value>
</member>
<member>
<name>some</name>
<value><int>1</int></value>
</member>
</struct></value>
</param>
<param>
<value><array><data>
<value><int>1</int></value>
<value><int>2</int></value>
<value><int>3</int></value>
<value><int>4</int></value>
<value><int>5</int></value>
</data></array></value>
</param>
</params>
</methodCall>

...which can then be turned back into Python data:

   1 call_data = xmlrpclib.loads( call_string )

...which then builds:

(('robot', {'some': 1, 'dict': 2}, [1, 2, 3, 4, 5]), u'foo')

That is, the first item is the arguments tuple, and the second item is the name of the function.

For some reason, these capabilities do not seem to be described in [http://www.python.org/doc/current/lib/module-xmlrpclib.html the xmlrpclib documentation.]

See Also

Discussion

  • (none yet!)

XmlRpc (last edited 2018-02-28 14:58:40 by aurelien)

Unable to edit the page? See the FrontPage for instructions.