diff options
author | Christian Heimes <christian@cheimes.de> | 2007-12-03 21:02:03 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2007-12-03 21:02:03 (GMT) |
commit | cbf3b5cb76906fba15dbf59a1e83c540a447b907 (patch) | |
tree | 191de62298cf0af90595802cd47cff23d8f39d80 /Doc/library/xmlrpclib.rst | |
parent | f9290773fc026c5064fa96e1d06c3924e49fa89b (diff) | |
download | cpython-cbf3b5cb76906fba15dbf59a1e83c540a447b907.zip cpython-cbf3b5cb76906fba15dbf59a1e83c540a447b907.tar.gz cpython-cbf3b5cb76906fba15dbf59a1e83c540a447b907.tar.bz2 |
Merged revisions 59275-59303 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
NOTE: The merge does NOT contain the modified file Python/import.c from
r59288. I can't get it running. Nick, please check in the PEP 366
manually.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
........
r59279 | georg.brandl | 2007-12-02 19:17:50 +0100 (Sun, 02 Dec 2007) | 2 lines
Fix a sentence I missed before. Do not merge to 3k.
........
r59281 | georg.brandl | 2007-12-02 22:58:54 +0100 (Sun, 02 Dec 2007) | 3 lines
Add documentation for PySys_* functions.
Written by Charlie Shepherd for GHOP. Also fixes #1245.
........
r59288 | nick.coghlan | 2007-12-03 13:55:17 +0100 (Mon, 03 Dec 2007) | 1 line
Implement PEP 366
........
r59290 | christian.heimes | 2007-12-03 14:47:29 +0100 (Mon, 03 Dec 2007) | 3 lines
Applied my patch #1455 with some extra fixes for VS 2005
The new msvc9compiler module supports VS 2005 and VS 2008. I've also fixed build_ext to support PCbuild8 and PCbuild9 and backported my fix for xxmodule.c from py3k. The old code msvccompiler is still in place in case somebody likes to build an extension with VS 2003 or earlier.
I've also updated the cygwin compiler module for VS 2005 and VS 2008. It works with VS 2005 but I'm unable to test it with VS 2008. We have to wait for a new version of cygwin.
........
r59291 | christian.heimes | 2007-12-03 14:55:16 +0100 (Mon, 03 Dec 2007) | 1 line
Added comment to Misc/NEWS for r59290
........
r59292 | christian.heimes | 2007-12-03 15:28:04 +0100 (Mon, 03 Dec 2007) | 1 line
I followed MA Lemberg's suggestion and added comments to the late initialization of the type slots.
........
r59293 | facundo.batista | 2007-12-03 17:29:52 +0100 (Mon, 03 Dec 2007) | 3 lines
Speedup and cleaning of __str__. Thanks Mark Dickinson.
........
r59294 | facundo.batista | 2007-12-03 18:55:00 +0100 (Mon, 03 Dec 2007) | 4 lines
Faster _fix function, and some reordering for a more elegant
coding. Thanks Mark Dickinson.
........
r59295 | martin.v.loewis | 2007-12-03 20:20:02 +0100 (Mon, 03 Dec 2007) | 5 lines
Issue #1727780: Support loading pickles of random.Random objects created
on 32-bit systems on 64-bit systems, and vice versa. As a consequence
of the change, Random pickles created by Python 2.6 cannot be loaded
in Python 2.5.
........
r59297 | facundo.batista | 2007-12-03 20:49:54 +0100 (Mon, 03 Dec 2007) | 3 lines
Two small fixes. Issue 1547.
........
r59299 | georg.brandl | 2007-12-03 20:57:02 +0100 (Mon, 03 Dec 2007) | 2 lines
#1548: fix apostroph placement.
........
r59300 | christian.heimes | 2007-12-03 21:01:02 +0100 (Mon, 03 Dec 2007) | 3 lines
Patch #1537 from Chad Austin
Change GeneratorExit's base class from Exception to BaseException
(This time I'm applying the patch to the correct sandbox.)
........
r59302 | georg.brandl | 2007-12-03 21:03:46 +0100 (Mon, 03 Dec 2007) | 3 lines
Add examples to the xmlrpclib docs.
Written for GHOP by Josip Dzolonga.
........
Diffstat (limited to 'Doc/library/xmlrpclib.rst')
-rw-r--r-- | Doc/library/xmlrpclib.rst | 166 |
1 files changed, 161 insertions, 5 deletions
diff --git a/Doc/library/xmlrpclib.rst b/Doc/library/xmlrpclib.rst index 0fda893..bde0688 100644 --- a/Doc/library/xmlrpclib.rst +++ b/Doc/library/xmlrpclib.rst @@ -192,6 +192,26 @@ unmarshalling code: Write the XML-RPC encoding of this Boolean item to the out stream object. +A working example follows. The server code:: + + import xmlrpclib + from SimpleXMLRPCServer import SimpleXMLRPCServer + + def is_even(n): + return n%2 == 0 + + server = SimpleXMLRPCServer(("localhost", 8000)) + print "Listening on port 8000..." + server.register_function(is_even, "is_even") + server.serve_forever() + +The client code for the preceding server:: + + import xmlrpclib + + proxy = xmlrpclib.ServerProxy("http://localhost:8000/") + print "3 is even: %s" % str(proxy.is_even(3)) + print "100 is even: %s" % str(proxy.is_even(100)) .. _datetime-objects: @@ -217,6 +237,32 @@ mainly for internal use by the marshalling/unmarshalling code: It also supports certain of Python's built-in operators through :meth:`__cmp__` and :meth:`__repr__` methods. +A working example follows. The server code:: + + import datetime + from SimpleXMLRPCServer import SimpleXMLRPCServer + import xmlrpclib + + def today(): + today = datetime.datetime.today() + return xmlrpclib.DateTime(today) + + server = SimpleXMLRPCServer(("localhost", 8000)) + print "Listening on port 8000..." + server.register_function(today, "today") + server.serve_forever() + +The client code for the preceding server:: + + import xmlrpclib + import datetime + + proxy = xmlrpclib.ServerProxy("http://localhost:8000/") + + today = proxy.today() + # convert the ISO8601 string to a datetime object + converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S") + print "Today: %s" % converted.strftime("%d.%m.%Y, %H:%M") .. _binary-objects: @@ -249,6 +295,31 @@ internal use by the marshalling/unmarshalling code: It also supports certain of Python's built-in operators through a :meth:`__cmp__` method. +Example usage of the binary objects. We're going to transfer an image over +XMLRPC:: + + from SimpleXMLRPCServer import SimpleXMLRPCServer + import xmlrpclib + + def python_logo(): + handle = open("python_logo.jpg") + return xmlrpclib.Binary(handle.read()) + handle.close() + + server = SimpleXMLRPCServer(("localhost", 8000)) + print "Listening on port 8000..." + server.register_function(python_logo, 'python_logo') + + server.serve_forever() + +The client gets the image and saves it to a file:: + + import xmlrpclib + + proxy = xmlrpclib.ServerProxy("http://localhost:8000/") + handle = open("fetched_python_logo.jpg", "w") + handle.write(proxy.python_logo().data) + handle.close() .. _fault-objects: @@ -268,6 +339,35 @@ objects have the following members: A string containing a diagnostic message associated with the fault. +In the following example we're going to intentionally cause a :exc:`Fault` by +returning a complex type object. The server code:: + + from SimpleXMLRPCServer import SimpleXMLRPCServer + + # A marshalling error is going to occur because we're returning a + # complex number + def add(x,y): + return x+y+0j + + server = SimpleXMLRPCServer(("localhost", 8000)) + print "Listening on port 8000..." + server.register_function(add, 'add') + + server.serve_forever() + +The client code for the preceding server:: + + import xmlrpclib + + proxy = xmlrpclib.ServerProxy("http://localhost:8000/") + try: + proxy.add(2, 5) + except xmlrpclib.Fault, err: + print "A fault occured" + print "Fault code: %d" % err.faultCode + print "Fault string: %s" % err.faultString + + .. _protocol-error-objects: @@ -299,6 +399,22 @@ does not exist). It has the following members: A dict containing the headers of the HTTP/HTTPS request that triggered the error. +In the following example we're going to intentionally cause a :exc:`ProtocolError` +by providing an invalid URI:: + + import xmlrpclib + + # create a ServerProxy with an invalid URI + proxy = xmlrpclib.ServerProxy("http://invalidaddress/") + + try: + proxy.some_method() + except xmlrpclib.ProtocolError, err: + print "A protocol error occured" + print "URL: %s" % err.url + print "HTTP/HTTPS headers: %s" % err.headers + print "Error code: %d" % err.errcode + print "Error message: %s" % err.errmsg MultiCall Objects ----------------- @@ -317,12 +433,45 @@ encapsulate multiple calls to a remote server into a single request. is a :term:`generator`; iterating over this generator yields the individual results. -A usage example of this class is :: +A usage example of this class follows. The server code :: + + from SimpleXMLRPCServer import SimpleXMLRPCServer + + def add(x,y): + return x+y - multicall = MultiCall(server_proxy) - multicall.add(2,3) - multicall.get_address("Guido") - add_result, address = multicall() + def subtract(x, y): + return x-y + + def multiply(x, y): + return x*y + + def divide(x, y): + return x/y + + # A simple server with simple arithmetic functions + server = SimpleXMLRPCServer(("localhost", 8000)) + print "Listening on port 8000..." + server.register_multicall_functions() + server.register_function(add, 'add') + server.register_function(subtract, 'subtract') + server.register_function(multiply, 'multiply') + server.register_function(divide, 'divide') + server.serve_forever() + +The client code for the preceding server:: + + import xmlrpclib + + proxy = xmlrpclib.ServerProxy("http://localhost:8000/") + multicall = xmlrpclib.MultiCall(proxy) + multicall.add(7,3) + multicall.subtract(7,3) + multicall.multiply(7,3) + multicall.divide(7,3) + result = multicall() + + print "7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d" % tuple(result) Convenience Functions @@ -407,3 +556,10 @@ transport. The following example, written by NoboNobo, shows how: server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p) print(server.currentTime.getCurrentTime()) + +Example of Client and Server Usage +---------------------------------- + +See :ref:`simplexmlrpcserver-example`. + + |