blob: 1871c998ec8df3a32adc70ebb92577a8df47727e [file] [log] [blame]
Georg Brandl38eceaa2008-05-26 11:14:17 +00001:mod:`xmlrpc.client` --- XML-RPC client access
2==============================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
Georg Brandl38eceaa2008-05-26 11:14:17 +00004.. module:: xmlrpc.client
Georg Brandl116aa622007-08-15 14:28:22 +00005 :synopsis: XML-RPC client access.
6.. moduleauthor:: Fredrik Lundh <fredrik@pythonware.com>
7.. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com>
8
9
Christian Heimes5b5e81c2007-12-31 16:14:33 +000010.. XXX Not everything is documented yet. It might be good to describe
Florent Xicluna61665192011-11-15 20:53:25 +010011 Marshaller, Unmarshaller, getparser and Transport.
Georg Brandl116aa622007-08-15 14:28:22 +000012
Raymond Hettinger3029aff2011-02-10 08:09:36 +000013**Source code:** :source:`Lib/xmlrpc/client.py`
14
15--------------
16
Georg Brandl116aa622007-08-15 14:28:22 +000017XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a
18transport. With it, a client can call methods with parameters on a remote
19server (the server is named by a URI) and get back structured data. This module
20supports writing XML-RPC client code; it handles all the details of translating
21between conformable Python objects and XML on the wire.
22
23
Florent Xicluna61665192011-11-15 20:53:25 +010024.. class:: ServerProxy(uri, transport=None, encoding=None, verbose=False, \
25 allow_none=False, use_datetime=False, \
26 use_builtin_types=False)
27
28 .. versionchanged:: 3.3
29 The *use_builtin_types* flag was added.
Georg Brandl116aa622007-08-15 14:28:22 +000030
31 A :class:`ServerProxy` instance is an object that manages communication with a
32 remote XML-RPC server. The required first argument is a URI (Uniform Resource
33 Indicator), and will normally be the URL of the server. The optional second
34 argument is a transport factory instance; by default it is an internal
35 :class:`SafeTransport` instance for https: URLs and an internal HTTP
36 :class:`Transport` instance otherwise. The optional third argument is an
37 encoding, by default UTF-8. The optional fourth argument is a debugging flag.
38 If *allow_none* is true, the Python constant ``None`` will be translated into
39 XML; the default behaviour is for ``None`` to raise a :exc:`TypeError`. This is
40 a commonly-used extension to the XML-RPC specification, but isn't supported by
41 all clients and servers; see http://ontosys.com/xml-rpc/extensions.php for a
Florent Xicluna61665192011-11-15 20:53:25 +010042 description. The *use_builtin_types* flag can be used to cause date/time values
43 to be presented as :class:`datetime.datetime` objects and binary data to be
44 presented as :class:`bytes` objects; this flag is false by default.
45 :class:`datetime.datetime` and :class:`bytes` objects may be passed to calls.
46
47 The obsolete *use_datetime* flag is similar to *use_builtin_types* but it
48 applies only to date/time values.
Georg Brandl116aa622007-08-15 14:28:22 +000049
50 Both the HTTP and HTTPS transports support the URL syntax extension for HTTP
51 Basic Authentication: ``http://user:pass@host:port/path``. The ``user:pass``
52 portion will be base64-encoded as an HTTP 'Authorization' header, and sent to
53 the remote server as part of the connection process when invoking an XML-RPC
54 method. You only need to use this if the remote server requires a Basic
55 Authentication user and password.
56
57 The returned instance is a proxy object with methods that can be used to invoke
58 corresponding RPC calls on the remote server. If the remote server supports the
59 introspection API, the proxy can also be used to query the remote server for the
60 methods it supports (service discovery) and fetch other server-associated
61 metadata.
62
63 :class:`ServerProxy` instance methods take Python basic types and objects as
64 arguments and return Python basic types and classes. Types that are conformable
65 (e.g. that can be marshalled through XML), include the following (and except
66 where noted, they are unmarshalled as the same Python type):
67
68 +---------------------------------+---------------------------------------------+
69 | Name | Meaning |
70 +=================================+=============================================+
71 | :const:`boolean` | The :const:`True` and :const:`False` |
72 | | constants |
73 +---------------------------------+---------------------------------------------+
74 | :const:`integers` | Pass in directly |
75 +---------------------------------+---------------------------------------------+
76 | :const:`floating-point numbers` | Pass in directly |
77 +---------------------------------+---------------------------------------------+
78 | :const:`strings` | Pass in directly |
79 +---------------------------------+---------------------------------------------+
80 | :const:`arrays` | Any Python sequence type containing |
81 | | conformable elements. Arrays are returned |
82 | | as lists |
83 +---------------------------------+---------------------------------------------+
84 | :const:`structures` | A Python dictionary. Keys must be strings, |
85 | | values may be any conformable type. Objects |
86 | | of user-defined classes can be passed in; |
87 | | only their *__dict__* attribute is |
88 | | transmitted. |
89 +---------------------------------+---------------------------------------------+
Florent Xicluna61665192011-11-15 20:53:25 +010090 | :const:`dates` | In seconds since the epoch. Pass in an |
91 | | instance of the :class:`DateTime` class or |
Christian Heimes05e8be12008-02-23 18:30:17 +000092 | | a :class:`datetime.datetime` instance. |
Georg Brandl116aa622007-08-15 14:28:22 +000093 +---------------------------------+---------------------------------------------+
Florent Xicluna61665192011-11-15 20:53:25 +010094 | :const:`binary data` | Pass in an instance of the :class:`Binary` |
95 | | wrapper class or a :class:`bytes` instance. |
Georg Brandl116aa622007-08-15 14:28:22 +000096 +---------------------------------+---------------------------------------------+
97
98 This is the full set of data types supported by XML-RPC. Method calls may also
99 raise a special :exc:`Fault` instance, used to signal XML-RPC server errors, or
100 :exc:`ProtocolError` used to signal an error in the HTTP/HTTPS transport layer.
101 Both :exc:`Fault` and :exc:`ProtocolError` derive from a base class called
Georg Brandl38eceaa2008-05-26 11:14:17 +0000102 :exc:`Error`. Note that the xmlrpc client module currently does not marshal
Georg Brandl22b34312009-07-26 14:54:51 +0000103 instances of subclasses of built-in types.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105 When passing strings, characters special to XML such as ``<``, ``>``, and ``&``
106 will be automatically escaped. However, it's the caller's responsibility to
107 ensure that the string is free of characters that aren't allowed in XML, such as
108 the control characters with ASCII values between 0 and 31 (except, of course,
109 tab, newline and carriage return); failing to do this will result in an XML-RPC
Florent Xicluna61665192011-11-15 20:53:25 +0100110 request that isn't well-formed XML. If you have to pass arbitrary bytes
111 via XML-RPC, use the :class:`bytes` class or the class:`Binary` wrapper class
112 described below.
Georg Brandl116aa622007-08-15 14:28:22 +0000113
114 :class:`Server` is retained as an alias for :class:`ServerProxy` for backwards
115 compatibility. New code should use :class:`ServerProxy`.
116
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118.. seealso::
119
120 `XML-RPC HOWTO <http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html>`_
Christian Heimesa62da1d2008-01-12 19:39:10 +0000121 A good description of XML-RPC operation and client software in several languages.
Georg Brandl116aa622007-08-15 14:28:22 +0000122 Contains pretty much everything an XML-RPC client developer needs to know.
123
Christian Heimesa62da1d2008-01-12 19:39:10 +0000124 `XML-RPC Introspection <http://xmlrpc-c.sourceforge.net/introspection.html>`_
125 Describes the XML-RPC protocol extension for introspection.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000127 `XML-RPC Specification <http://www.xmlrpc.com/spec>`_
128 The official specification.
129
130 `Unofficial XML-RPC Errata <http://effbot.org/zone/xmlrpc-errata.htm>`_
131 Fredrik Lundh's "unofficial errata, intended to clarify certain
132 details in the XML-RPC specification, as well as hint at
133 'best practices' to use when designing your own XML-RPC
134 implementations."
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136.. _serverproxy-objects:
137
138ServerProxy Objects
139-------------------
140
141A :class:`ServerProxy` instance has a method corresponding to each remote
142procedure call accepted by the XML-RPC server. Calling the method performs an
143RPC, dispatched by both name and argument signature (e.g. the same method name
144can be overloaded with multiple argument signatures). The RPC finishes by
145returning a value, which may be either returned data in a conformant type or a
146:class:`Fault` or :class:`ProtocolError` object indicating an error.
147
148Servers that support the XML introspection API support some common methods
Senthil Kumarana6bac952011-07-04 11:28:30 -0700149grouped under the reserved :attr:`system` attribute:
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151
152.. method:: ServerProxy.system.listMethods()
153
154 This method returns a list of strings, one for each (non-system) method
155 supported by the XML-RPC server.
156
157
158.. method:: ServerProxy.system.methodSignature(name)
159
160 This method takes one parameter, the name of a method implemented by the XML-RPC
Georg Brandl94606482009-05-04 20:46:44 +0000161 server. It returns an array of possible signatures for this method. A signature
Georg Brandl116aa622007-08-15 14:28:22 +0000162 is an array of types. The first of these types is the return type of the method,
163 the rest are parameters.
164
165 Because multiple signatures (ie. overloading) is permitted, this method returns
166 a list of signatures rather than a singleton.
167
168 Signatures themselves are restricted to the top level parameters expected by a
169 method. For instance if a method expects one array of structs as a parameter,
170 and it returns a string, its signature is simply "string, array". If it expects
171 three integers and returns a string, its signature is "string, int, int, int".
172
173 If no signature is defined for the method, a non-array value is returned. In
174 Python this means that the type of the returned value will be something other
Georg Brandl94606482009-05-04 20:46:44 +0000175 than list.
Georg Brandl116aa622007-08-15 14:28:22 +0000176
177
178.. method:: ServerProxy.system.methodHelp(name)
179
180 This method takes one parameter, the name of a method implemented by the XML-RPC
181 server. It returns a documentation string describing the use of that method. If
182 no such string is available, an empty string is returned. The documentation
183 string may contain HTML markup.
184
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000186A working example follows. The server code::
187
Georg Brandl38eceaa2008-05-26 11:14:17 +0000188 from xmlrpc.server import SimpleXMLRPCServer
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000189
190 def is_even(n):
191 return n%2 == 0
192
193 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000194 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000195 server.register_function(is_even, "is_even")
196 server.serve_forever()
197
198The client code for the preceding server::
199
Georg Brandl38eceaa2008-05-26 11:14:17 +0000200 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000201
Georg Brandl38eceaa2008-05-26 11:14:17 +0000202 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
Georg Brandlf6945182008-02-01 11:56:49 +0000203 print("3 is even: %s" % str(proxy.is_even(3)))
204 print("100 is even: %s" % str(proxy.is_even(100)))
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206.. _datetime-objects:
207
208DateTime Objects
209----------------
210
Christian Heimes05e8be12008-02-23 18:30:17 +0000211This class may be initialized with seconds since the epoch, a time
212tuple, an ISO 8601 time/date string, or a :class:`datetime.datetime`
213instance. It has the following methods, supported mainly for internal
214use by the marshalling/unmarshalling code:
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216
217.. method:: DateTime.decode(string)
218
219 Accept a string as the instance's new time value.
220
221
222.. method:: DateTime.encode(out)
223
224 Write the XML-RPC encoding of this :class:`DateTime` item to the *out* stream
225 object.
226
Georg Brandl05f5ab72008-09-24 09:11:47 +0000227It also supports certain of Python's built-in operators through rich comparison
Georg Brandl116aa622007-08-15 14:28:22 +0000228and :meth:`__repr__` methods.
229
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000230A working example follows. The server code::
231
232 import datetime
Georg Brandl38eceaa2008-05-26 11:14:17 +0000233 from xmlrpc.server import SimpleXMLRPCServer
234 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000235
236 def today():
237 today = datetime.datetime.today()
Georg Brandl38eceaa2008-05-26 11:14:17 +0000238 return xmlrpc.client.DateTime(today)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000239
240 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000241 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000242 server.register_function(today, "today")
243 server.serve_forever()
244
245The client code for the preceding server::
246
Georg Brandl38eceaa2008-05-26 11:14:17 +0000247 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000248 import datetime
249
Georg Brandl38eceaa2008-05-26 11:14:17 +0000250 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000251
252 today = proxy.today()
253 # convert the ISO8601 string to a datetime object
254 converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")
Georg Brandlf6945182008-02-01 11:56:49 +0000255 print("Today: %s" % converted.strftime("%d.%m.%Y, %H:%M"))
Georg Brandl116aa622007-08-15 14:28:22 +0000256
257.. _binary-objects:
258
259Binary Objects
260--------------
261
Florent Xicluna61665192011-11-15 20:53:25 +0100262This class may be initialized from bytes data (which may include NULs). The
Georg Brandl116aa622007-08-15 14:28:22 +0000263primary access to the content of a :class:`Binary` object is provided by an
264attribute:
265
266
267.. attribute:: Binary.data
268
269 The binary data encapsulated by the :class:`Binary` instance. The data is
Florent Xicluna61665192011-11-15 20:53:25 +0100270 provided as a :class:`bytes` object.
Georg Brandl116aa622007-08-15 14:28:22 +0000271
272:class:`Binary` objects have the following methods, supported mainly for
273internal use by the marshalling/unmarshalling code:
274
275
Florent Xicluna61665192011-11-15 20:53:25 +0100276.. method:: Binary.decode(bytes)
Georg Brandl116aa622007-08-15 14:28:22 +0000277
Florent Xicluna61665192011-11-15 20:53:25 +0100278 Accept a base64 :class:`bytes` object and decode it as the instance's new data.
Georg Brandl116aa622007-08-15 14:28:22 +0000279
280
281.. method:: Binary.encode(out)
282
283 Write the XML-RPC base 64 encoding of this binary item to the out stream object.
284
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000285 The encoded data will have newlines every 76 characters as per
286 `RFC 2045 section 6.8 <http://tools.ietf.org/html/rfc2045#section-6.8>`_,
287 which was the de facto standard base64 specification when the
288 XML-RPC spec was written.
289
Georg Brandl05f5ab72008-09-24 09:11:47 +0000290It also supports certain of Python's built-in operators through :meth:`__eq__`
291and :meth:`__ne__` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000292
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000293Example usage of the binary objects. We're going to transfer an image over
294XMLRPC::
295
Georg Brandl38eceaa2008-05-26 11:14:17 +0000296 from xmlrpc.server import SimpleXMLRPCServer
297 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000298
299 def python_logo():
Victor Stinner757db832010-01-30 02:16:55 +0000300 with open("python_logo.jpg", "rb") as handle:
301 return xmlrpc.client.Binary(handle.read())
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000302
303 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000304 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000305 server.register_function(python_logo, 'python_logo')
306
307 server.serve_forever()
308
309The client gets the image and saves it to a file::
310
Georg Brandl38eceaa2008-05-26 11:14:17 +0000311 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000312
Georg Brandl38eceaa2008-05-26 11:14:17 +0000313 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
Victor Stinner757db832010-01-30 02:16:55 +0000314 with open("fetched_python_logo.jpg", "wb") as handle:
315 handle.write(proxy.python_logo().data)
Georg Brandl116aa622007-08-15 14:28:22 +0000316
317.. _fault-objects:
318
319Fault Objects
320-------------
321
322A :class:`Fault` object encapsulates the content of an XML-RPC fault tag. Fault
Senthil Kumarana6bac952011-07-04 11:28:30 -0700323objects have the following attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000324
325
326.. attribute:: Fault.faultCode
327
328 A string indicating the fault type.
329
330
331.. attribute:: Fault.faultString
332
333 A string containing a diagnostic message associated with the fault.
334
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000335In the following example we're going to intentionally cause a :exc:`Fault` by
336returning a complex type object. The server code::
337
Georg Brandl38eceaa2008-05-26 11:14:17 +0000338 from xmlrpc.server import SimpleXMLRPCServer
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000339
340 # A marshalling error is going to occur because we're returning a
341 # complex number
342 def add(x,y):
343 return x+y+0j
344
345 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000346 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000347 server.register_function(add, 'add')
348
349 server.serve_forever()
350
351The client code for the preceding server::
352
Georg Brandl38eceaa2008-05-26 11:14:17 +0000353 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000354
Georg Brandl38eceaa2008-05-26 11:14:17 +0000355 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000356 try:
357 proxy.add(2, 5)
Georg Brandl0dedf452009-05-22 16:44:06 +0000358 except xmlrpc.client.Fault as err:
Georg Brandl2ee470f2008-07-16 12:55:28 +0000359 print("A fault occurred")
Georg Brandlf6945182008-02-01 11:56:49 +0000360 print("Fault code: %d" % err.faultCode)
361 print("Fault string: %s" % err.faultString)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000362
363
Georg Brandl116aa622007-08-15 14:28:22 +0000364
365.. _protocol-error-objects:
366
367ProtocolError Objects
368---------------------
369
370A :class:`ProtocolError` object describes a protocol error in the underlying
371transport layer (such as a 404 'not found' error if the server named by the URI
Senthil Kumarana6bac952011-07-04 11:28:30 -0700372does not exist). It has the following attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000373
374
375.. attribute:: ProtocolError.url
376
377 The URI or URL that triggered the error.
378
379
380.. attribute:: ProtocolError.errcode
381
382 The error code.
383
384
385.. attribute:: ProtocolError.errmsg
386
387 The error message or diagnostic string.
388
389
390.. attribute:: ProtocolError.headers
391
Guido van Rossum460add42007-08-23 02:13:35 +0000392 A dict containing the headers of the HTTP/HTTPS request that triggered the
Georg Brandl116aa622007-08-15 14:28:22 +0000393 error.
394
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000395In the following example we're going to intentionally cause a :exc:`ProtocolError`
396by providing an invalid URI::
397
Georg Brandl38eceaa2008-05-26 11:14:17 +0000398 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000399
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000400 # create a ServerProxy with an URI that doesn't respond to XMLRPC requests
401 proxy = xmlrpc.client.ServerProxy("http://google.com/")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000402
403 try:
404 proxy.some_method()
Georg Brandl0dedf452009-05-22 16:44:06 +0000405 except xmlrpc.client.ProtocolError as err:
Georg Brandl2ee470f2008-07-16 12:55:28 +0000406 print("A protocol error occurred")
Georg Brandlf6945182008-02-01 11:56:49 +0000407 print("URL: %s" % err.url)
408 print("HTTP/HTTPS headers: %s" % err.headers)
409 print("Error code: %d" % err.errcode)
410 print("Error message: %s" % err.errmsg)
Georg Brandl116aa622007-08-15 14:28:22 +0000411
412MultiCall Objects
413-----------------
414
Sandro Tosi9daf98d2011-08-20 17:05:56 +0200415The :class:`MultiCall` object provides a way to encapsulate multiple calls to a
416remote server into a single request [#]_.
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418
419.. class:: MultiCall(server)
420
421 Create an object used to boxcar method calls. *server* is the eventual target of
422 the call. Calls can be made to the result object, but they will immediately
423 return ``None``, and only store the call name and parameters in the
424 :class:`MultiCall` object. Calling the object itself causes all stored calls to
425 be transmitted as a single ``system.multicall`` request. The result of this call
Georg Brandl9afde1c2007-11-01 20:32:30 +0000426 is a :term:`generator`; iterating over this generator yields the individual
427 results.
Georg Brandl116aa622007-08-15 14:28:22 +0000428
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000429A usage example of this class follows. The server code ::
Georg Brandl116aa622007-08-15 14:28:22 +0000430
Georg Brandl38eceaa2008-05-26 11:14:17 +0000431 from xmlrpc.server import SimpleXMLRPCServer
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000432
433 def add(x,y):
434 return x+y
435
436 def subtract(x, y):
437 return x-y
438
439 def multiply(x, y):
440 return x*y
441
442 def divide(x, y):
443 return x/y
444
445 # A simple server with simple arithmetic functions
446 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000447 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000448 server.register_multicall_functions()
449 server.register_function(add, 'add')
450 server.register_function(subtract, 'subtract')
451 server.register_function(multiply, 'multiply')
452 server.register_function(divide, 'divide')
453 server.serve_forever()
454
455The client code for the preceding server::
456
Georg Brandl38eceaa2008-05-26 11:14:17 +0000457 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000458
Georg Brandl38eceaa2008-05-26 11:14:17 +0000459 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
460 multicall = xmlrpc.client.MultiCall(proxy)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000461 multicall.add(7,3)
462 multicall.subtract(7,3)
463 multicall.multiply(7,3)
464 multicall.divide(7,3)
465 result = multicall()
466
Georg Brandlf6945182008-02-01 11:56:49 +0000467 print("7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d" % tuple(result))
Georg Brandl116aa622007-08-15 14:28:22 +0000468
469
470Convenience Functions
471---------------------
472
Georg Brandl7f01a132009-09-16 15:58:14 +0000473.. function:: dumps(params, methodname=None, methodresponse=None, encoding=None, allow_none=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000474
475 Convert *params* into an XML-RPC request. or into a response if *methodresponse*
476 is true. *params* can be either a tuple of arguments or an instance of the
477 :exc:`Fault` exception class. If *methodresponse* is true, only a single value
478 can be returned, meaning that *params* must be of length 1. *encoding*, if
479 supplied, is the encoding to use in the generated XML; the default is UTF-8.
480 Python's :const:`None` value cannot be used in standard XML-RPC; to allow using
481 it via an extension, provide a true value for *allow_none*.
482
483
Florent Xicluna61665192011-11-15 20:53:25 +0100484.. function:: loads(data, use_datetime=False, use_builtin_types=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000485
486 Convert an XML-RPC request or response into Python objects, a ``(params,
487 methodname)``. *params* is a tuple of argument; *methodname* is a string, or
488 ``None`` if no method name is present in the packet. If the XML-RPC packet
489 represents a fault condition, this function will raise a :exc:`Fault` exception.
Florent Xicluna61665192011-11-15 20:53:25 +0100490 The *use_builtin_types* flag can be used to cause date/time values to be
491 presented as :class:`datetime.datetime` objects and binary data to be
492 presented as :class:`bytes` objects; this flag is false by default.
493
494 The obsolete *use_datetime* flag is similar to *use_builtin_types* but it
495 applies only to date/time values.
496
497 .. versionchanged:: 3.3
498 The *use_builtin_types* flag was added.
Georg Brandl116aa622007-08-15 14:28:22 +0000499
Georg Brandl116aa622007-08-15 14:28:22 +0000500
501.. _xmlrpc-client-example:
502
503Example of Client Usage
504-----------------------
505
506::
507
508 # simple test program (from the XML-RPC specification)
Georg Brandl38eceaa2008-05-26 11:14:17 +0000509 from xmlrpc.client import ServerProxy, Error
Georg Brandl116aa622007-08-15 14:28:22 +0000510
511 # server = ServerProxy("http://localhost:8000") # local server
512 server = ServerProxy("http://betty.userland.com")
513
Collin Winterc79461b2007-09-01 23:34:30 +0000514 print(server)
Georg Brandl116aa622007-08-15 14:28:22 +0000515
516 try:
Collin Winterc79461b2007-09-01 23:34:30 +0000517 print(server.examples.getStateName(41))
Georg Brandl116aa622007-08-15 14:28:22 +0000518 except Error as v:
Collin Winterc79461b2007-09-01 23:34:30 +0000519 print("ERROR", v)
Georg Brandl116aa622007-08-15 14:28:22 +0000520
521To access an XML-RPC server through a proxy, you need to define a custom
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000522transport. The following example shows how:
Georg Brandl116aa622007-08-15 14:28:22 +0000523
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000524.. Example taken from http://lowlife.jp/nobonobo/wiki/xmlrpcwithproxy.html
Georg Brandl116aa622007-08-15 14:28:22 +0000525
526::
527
Georg Brandl24420152008-05-26 16:32:26 +0000528 import xmlrpc.client, http.client
Georg Brandl116aa622007-08-15 14:28:22 +0000529
Georg Brandl38eceaa2008-05-26 11:14:17 +0000530 class ProxiedTransport(xmlrpc.client.Transport):
Georg Brandl116aa622007-08-15 14:28:22 +0000531 def set_proxy(self, proxy):
532 self.proxy = proxy
533 def make_connection(self, host):
534 self.realhost = host
Georg Brandl06788c92009-01-03 21:31:47 +0000535 h = http.client.HTTP(self.proxy)
536 return h
Georg Brandl116aa622007-08-15 14:28:22 +0000537 def send_request(self, connection, handler, request_body):
538 connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
539 def send_host(self, connection, host):
540 connection.putheader('Host', self.realhost)
541
542 p = ProxiedTransport()
543 p.set_proxy('proxy-server:8080')
Georg Brandl38eceaa2008-05-26 11:14:17 +0000544 server = xmlrpc.client.Server('http://time.xmlrpc.com/RPC2', transport=p)
Collin Winterc79461b2007-09-01 23:34:30 +0000545 print(server.currentTime.getCurrentTime())
Georg Brandl116aa622007-08-15 14:28:22 +0000546
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000547
548Example of Client and Server Usage
549----------------------------------
550
551See :ref:`simplexmlrpcserver-example`.
552
553
Sandro Tosi9daf98d2011-08-20 17:05:56 +0200554.. rubric:: Footnotes
555
556.. [#] This approach has been first presented in `a discussion on xmlrpc.com
557 <http://web.archive.org/web/20060624230303/http://www.xmlrpc.com/discuss/msgReader$1208?mode=topic>`_.
558.. the link now points to webarchive since the one at
559.. http://www.xmlrpc.com/discuss/msgReader%241208 is broken (and webadmin
560.. doesn't reply)