blob: 833dc7ac7db7fa7b1565c1af6596ffb151e252ef [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`xmlrpclib` --- XML-RPC client access
2==========================================
3
4.. module:: xmlrpclib
5 :synopsis: XML-RPC client access.
6.. moduleauthor:: Fredrik Lundh <fredrik@pythonware.com>
7.. sectionauthor:: Eric S. Raymond <esr@snark.thyrsus.com>
8
Georg Brandle2caef72008-05-26 10:47:43 +00009.. note::
10 The :mod:`xmlrpclib` module has been renamed to :mod:`xmlrpc.client` in
Ezio Melotti510ff542012-05-03 19:21:40 +030011 Python 3. The :term:`2to3` tool will automatically adapt imports when
12 converting your sources to Python 3.
Georg Brandle2caef72008-05-26 10:47:43 +000013
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
Georg Brandlb19be572007-12-29 10:57:00 +000015.. XXX Not everything is documented yet. It might be good to describe
16 Marshaller, Unmarshaller, getparser, dumps, loads, and Transport.
Georg Brandl8ec7f652007-08-15 14:28:01 +000017
18.. versionadded:: 2.2
19
Éric Araujo29a0b572011-08-19 02:14:03 +020020**Source code:** :source:`Lib/xmlrpclib.py`
21
22--------------
23
Georg Brandl8ec7f652007-08-15 14:28:01 +000024XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a
25transport. With it, a client can call methods with parameters on a remote
26server (the server is named by a URI) and get back structured data. This module
27supports writing XML-RPC client code; it handles all the details of translating
28between conformable Python objects and XML on the wire.
29
30
Christian Heimes23790b42013-03-26 17:53:05 +010031.. warning::
32
33 The :mod:`xmlrpclib` module is not secure against maliciously
34 constructed data. If you need to parse untrusted or unauthenticated data see
35 :ref:`xml-vulnerabilities`.
36
Benjamin Petersone3e7d402014-11-23 21:02:02 -060037.. versionchanged:: 2.7.9
Benjamin Peterson078ece22014-10-13 11:53:54 -040038
Benjamin Petersone3e7d402014-11-23 21:02:02 -060039 For https URIs, :mod:`xmlrpclib` now performs all the necessary certificate
40 and hostname checks by default
Christian Heimes23790b42013-03-26 17:53:05 +010041
Benjamin Petersonefa3cf82014-11-29 22:55:35 -050042.. class:: ServerProxy(uri[, transport[, encoding[, verbose[, allow_none[, use_datetime[, context]]]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
44 A :class:`ServerProxy` instance is an object that manages communication with a
45 remote XML-RPC server. The required first argument is a URI (Uniform Resource
46 Indicator), and will normally be the URL of the server. The optional second
47 argument is a transport factory instance; by default it is an internal
48 :class:`SafeTransport` instance for https: URLs and an internal HTTP
49 :class:`Transport` instance otherwise. The optional third argument is an
50 encoding, by default UTF-8. The optional fourth argument is a debugging flag.
51 If *allow_none* is true, the Python constant ``None`` will be translated into
52 XML; the default behaviour is for ``None`` to raise a :exc:`TypeError`. This is
53 a commonly-used extension to the XML-RPC specification, but isn't supported by
54 all clients and servers; see http://ontosys.com/xml-rpc/extensions.php for a
55 description. The *use_datetime* flag can be used to cause date/time values to
56 be presented as :class:`datetime.datetime` objects; this is false by default.
Andrew M. Kuchling085f75a2008-02-23 16:23:05 +000057 :class:`datetime.datetime` objects may be passed to calls.
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
59 Both the HTTP and HTTPS transports support the URL syntax extension for HTTP
Benjamin Petersonefa3cf82014-11-29 22:55:35 -050060 Basic Authentication: ``http://user:pass@host:port/path``. The ``user:pass``
Georg Brandl8ec7f652007-08-15 14:28:01 +000061 portion will be base64-encoded as an HTTP 'Authorization' header, and sent to
62 the remote server as part of the connection process when invoking an XML-RPC
63 method. You only need to use this if the remote server requires a Basic
Benjamin Petersonefa3cf82014-11-29 22:55:35 -050064 Authentication user and password. If an HTTPS url is provided, *context* may
65 be :class:`ssl.SSLContext` and configures the SSL settings of the underlying
66 HTTPS connection.
Georg Brandl8ec7f652007-08-15 14:28:01 +000067
68 The returned instance is a proxy object with methods that can be used to invoke
69 corresponding RPC calls on the remote server. If the remote server supports the
70 introspection API, the proxy can also be used to query the remote server for the
71 methods it supports (service discovery) and fetch other server-associated
72 metadata.
73
74 :class:`ServerProxy` instance methods take Python basic types and objects as
75 arguments and return Python basic types and classes. Types that are conformable
76 (e.g. that can be marshalled through XML), include the following (and except
77 where noted, they are unmarshalled as the same Python type):
78
79 +---------------------------------+---------------------------------------------+
80 | Name | Meaning |
81 +=================================+=============================================+
82 | :const:`boolean` | The :const:`True` and :const:`False` |
83 | | constants |
84 +---------------------------------+---------------------------------------------+
85 | :const:`integers` | Pass in directly |
86 +---------------------------------+---------------------------------------------+
87 | :const:`floating-point numbers` | Pass in directly |
88 +---------------------------------+---------------------------------------------+
89 | :const:`strings` | Pass in directly |
90 +---------------------------------+---------------------------------------------+
91 | :const:`arrays` | Any Python sequence type containing |
92 | | conformable elements. Arrays are returned |
93 | | as lists |
94 +---------------------------------+---------------------------------------------+
95 | :const:`structures` | A Python dictionary. Keys must be strings, |
96 | | values may be any conformable type. Objects |
97 | | of user-defined classes can be passed in; |
98 | | only their *__dict__* attribute is |
99 | | transmitted. |
100 +---------------------------------+---------------------------------------------+
101 | :const:`dates` | in seconds since the epoch (pass in an |
102 | | instance of the :class:`DateTime` class) or |
Andrew M. Kuchling085f75a2008-02-23 16:23:05 +0000103 | | a :class:`datetime.datetime` instance. |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104 +---------------------------------+---------------------------------------------+
105 | :const:`binary data` | pass in an instance of the :class:`Binary` |
106 | | wrapper class |
107 +---------------------------------+---------------------------------------------+
108
109 This is the full set of data types supported by XML-RPC. Method calls may also
110 raise a special :exc:`Fault` instance, used to signal XML-RPC server errors, or
111 :exc:`ProtocolError` used to signal an error in the HTTP/HTTPS transport layer.
112 Both :exc:`Fault` and :exc:`ProtocolError` derive from a base class called
113 :exc:`Error`. Note that even though starting with Python 2.2 you can subclass
Georg Brandld7d4fd72009-07-26 14:37:28 +0000114 built-in types, the xmlrpclib module currently does not marshal instances of such
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115 subclasses.
116
117 When passing strings, characters special to XML such as ``<``, ``>``, and ``&``
118 will be automatically escaped. However, it's the caller's responsibility to
119 ensure that the string is free of characters that aren't allowed in XML, such as
120 the control characters with ASCII values between 0 and 31 (except, of course,
121 tab, newline and carriage return); failing to do this will result in an XML-RPC
122 request that isn't well-formed XML. If you have to pass arbitrary strings via
123 XML-RPC, use the :class:`Binary` wrapper class described below.
124
125 :class:`Server` is retained as an alias for :class:`ServerProxy` for backwards
126 compatibility. New code should use :class:`ServerProxy`.
127
128 .. versionchanged:: 2.5
129 The *use_datetime* flag was added.
130
131 .. versionchanged:: 2.6
Georg Brandla7395032007-10-21 12:15:05 +0000132 Instances of :term:`new-style class`\es can be passed in if they have an
133 *__dict__* attribute and don't have a base class that is marshalled in a
134 special way.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135
Benjamin Petersonefa3cf82014-11-29 22:55:35 -0500136 .. versionchanged:: 2.7.9
137 Added the *context* argument.
138
Georg Brandl8ec7f652007-08-15 14:28:01 +0000139
140.. seealso::
141
142 `XML-RPC HOWTO <http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html>`_
Andrew M. Kuchlingde680372008-01-11 19:33:24 +0000143 A good description of XML-RPC operation and client software in several languages.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000144 Contains pretty much everything an XML-RPC client developer needs to know.
145
Andrew M. Kuchlingde680372008-01-11 19:33:24 +0000146 `XML-RPC Introspection <http://xmlrpc-c.sourceforge.net/introspection.html>`_
147 Describes the XML-RPC protocol extension for introspection.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000148
Skip Montanaro6d9aafa2008-04-22 22:45:09 +0000149 `XML-RPC Specification <http://www.xmlrpc.com/spec>`_
150 The official specification.
151
152 `Unofficial XML-RPC Errata <http://effbot.org/zone/xmlrpc-errata.htm>`_
153 Fredrik Lundh's "unofficial errata, intended to clarify certain
154 details in the XML-RPC specification, as well as hint at
155 'best practices' to use when designing your own XML-RPC
156 implementations."
Georg Brandl8ec7f652007-08-15 14:28:01 +0000157
158.. _serverproxy-objects:
159
160ServerProxy Objects
161-------------------
162
163A :class:`ServerProxy` instance has a method corresponding to each remote
164procedure call accepted by the XML-RPC server. Calling the method performs an
165RPC, dispatched by both name and argument signature (e.g. the same method name
166can be overloaded with multiple argument signatures). The RPC finishes by
167returning a value, which may be either returned data in a conformant type or a
168:class:`Fault` or :class:`ProtocolError` object indicating an error.
169
170Servers that support the XML introspection API support some common methods
Senthil Kumaran6f18b982011-07-04 12:50:02 -0700171grouped under the reserved :attr:`system` attribute:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172
173
174.. method:: ServerProxy.system.listMethods()
175
176 This method returns a list of strings, one for each (non-system) method
177 supported by the XML-RPC server.
178
179
180.. method:: ServerProxy.system.methodSignature(name)
181
182 This method takes one parameter, the name of a method implemented by the XML-RPC
Georg Brandlf5f045e2009-05-04 20:45:13 +0000183 server. It returns an array of possible signatures for this method. A signature
Georg Brandl8ec7f652007-08-15 14:28:01 +0000184 is an array of types. The first of these types is the return type of the method,
185 the rest are parameters.
186
187 Because multiple signatures (ie. overloading) is permitted, this method returns
188 a list of signatures rather than a singleton.
189
190 Signatures themselves are restricted to the top level parameters expected by a
191 method. For instance if a method expects one array of structs as a parameter,
192 and it returns a string, its signature is simply "string, array". If it expects
193 three integers and returns a string, its signature is "string, int, int, int".
194
195 If no signature is defined for the method, a non-array value is returned. In
196 Python this means that the type of the returned value will be something other
Georg Brandlf5f045e2009-05-04 20:45:13 +0000197 than list.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000198
199
200.. method:: ServerProxy.system.methodHelp(name)
201
202 This method takes one parameter, the name of a method implemented by the XML-RPC
203 server. It returns a documentation string describing the use of that method. If
204 no such string is available, an empty string is returned. The documentation
205 string may contain HTML markup.
206
Georg Brandl8ec7f652007-08-15 14:28:01 +0000207
208.. _boolean-objects:
209
210Boolean Objects
211---------------
212
213This class may be initialized from any Python value; the instance returned
214depends only on its truth value. It supports various Python operators through
215:meth:`__cmp__`, :meth:`__repr__`, :meth:`__int__`, and :meth:`__nonzero__`
216methods, all implemented in the obvious ways.
217
218It also has the following method, supported mainly for internal use by the
219unmarshalling code:
220
221
222.. method:: Boolean.encode(out)
223
224 Write the XML-RPC encoding of this Boolean item to the out stream object.
225
Georg Brandl0a0cf162007-12-03 20:03:46 +0000226A working example follows. The server code::
227
228 import xmlrpclib
229 from SimpleXMLRPCServer import SimpleXMLRPCServer
230
231 def is_even(n):
232 return n%2 == 0
233
234 server = SimpleXMLRPCServer(("localhost", 8000))
235 print "Listening on port 8000..."
236 server.register_function(is_even, "is_even")
237 server.serve_forever()
238
239The client code for the preceding server::
240
241 import xmlrpclib
242
243 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
244 print "3 is even: %s" % str(proxy.is_even(3))
245 print "100 is even: %s" % str(proxy.is_even(100))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000246
247.. _datetime-objects:
248
249DateTime Objects
250----------------
251
Andrew M. Kuchling085f75a2008-02-23 16:23:05 +0000252This class may be initialized with seconds since the epoch, a time
253tuple, an ISO 8601 time/date string, or a :class:`datetime.datetime`
254instance. It has the following methods, supported mainly for internal
255use by the marshalling/unmarshalling code:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000256
257
258.. method:: DateTime.decode(string)
259
260 Accept a string as the instance's new time value.
261
262
263.. method:: DateTime.encode(out)
264
265 Write the XML-RPC encoding of this :class:`DateTime` item to the *out* stream
266 object.
267
268It also supports certain of Python's built-in operators through :meth:`__cmp__`
269and :meth:`__repr__` methods.
270
Georg Brandl0a0cf162007-12-03 20:03:46 +0000271A working example follows. The server code::
272
273 import datetime
274 from SimpleXMLRPCServer import SimpleXMLRPCServer
275 import xmlrpclib
276
277 def today():
278 today = datetime.datetime.today()
279 return xmlrpclib.DateTime(today)
280
281 server = SimpleXMLRPCServer(("localhost", 8000))
282 print "Listening on port 8000..."
283 server.register_function(today, "today")
284 server.serve_forever()
285
286The client code for the preceding server::
287
288 import xmlrpclib
289 import datetime
290
291 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
292
293 today = proxy.today()
294 # convert the ISO8601 string to a datetime object
295 converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")
296 print "Today: %s" % converted.strftime("%d.%m.%Y, %H:%M")
Georg Brandl8ec7f652007-08-15 14:28:01 +0000297
298.. _binary-objects:
299
300Binary Objects
301--------------
302
303This class may be initialized from string data (which may include NULs). The
304primary access to the content of a :class:`Binary` object is provided by an
305attribute:
306
307
308.. attribute:: Binary.data
309
310 The binary data encapsulated by the :class:`Binary` instance. The data is
311 provided as an 8-bit string.
312
313:class:`Binary` objects have the following methods, supported mainly for
314internal use by the marshalling/unmarshalling code:
315
316
317.. method:: Binary.decode(string)
318
319 Accept a base64 string and decode it as the instance's new data.
320
321
322.. method:: Binary.encode(out)
323
324 Write the XML-RPC base 64 encoding of this binary item to the out stream object.
325
Skip Montanaro6d9aafa2008-04-22 22:45:09 +0000326 The encoded data will have newlines every 76 characters as per
327 `RFC 2045 section 6.8 <http://tools.ietf.org/html/rfc2045#section-6.8>`_,
328 which was the de facto standard base64 specification when the
329 XML-RPC spec was written.
330
Georg Brandl8ec7f652007-08-15 14:28:01 +0000331It also supports certain of Python's built-in operators through a
332:meth:`__cmp__` method.
333
Georg Brandl0a0cf162007-12-03 20:03:46 +0000334Example usage of the binary objects. We're going to transfer an image over
335XMLRPC::
336
337 from SimpleXMLRPCServer import SimpleXMLRPCServer
338 import xmlrpclib
339
340 def python_logo():
Victor Stinner75d3fb12010-01-30 02:00:26 +0000341 with open("python_logo.jpg", "rb") as handle:
Georg Brandl34feea32009-02-07 12:21:17 +0000342 return xmlrpclib.Binary(handle.read())
Georg Brandl0a0cf162007-12-03 20:03:46 +0000343
344 server = SimpleXMLRPCServer(("localhost", 8000))
345 print "Listening on port 8000..."
346 server.register_function(python_logo, 'python_logo')
347
348 server.serve_forever()
349
350The client gets the image and saves it to a file::
351
352 import xmlrpclib
353
354 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
Victor Stinner75d3fb12010-01-30 02:00:26 +0000355 with open("fetched_python_logo.jpg", "wb") as handle:
Georg Brandl34feea32009-02-07 12:21:17 +0000356 handle.write(proxy.python_logo().data)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000357
358.. _fault-objects:
359
360Fault Objects
361-------------
362
363A :class:`Fault` object encapsulates the content of an XML-RPC fault tag. Fault
Senthil Kumaran6f18b982011-07-04 12:50:02 -0700364objects have the following attributes:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000365
366
367.. attribute:: Fault.faultCode
368
369 A string indicating the fault type.
370
371
372.. attribute:: Fault.faultString
373
374 A string containing a diagnostic message associated with the fault.
375
Georg Brandl0a0cf162007-12-03 20:03:46 +0000376In the following example we're going to intentionally cause a :exc:`Fault` by
377returning a complex type object. The server code::
378
379 from SimpleXMLRPCServer import SimpleXMLRPCServer
380
381 # A marshalling error is going to occur because we're returning a
382 # complex number
383 def add(x,y):
384 return x+y+0j
385
386 server = SimpleXMLRPCServer(("localhost", 8000))
387 print "Listening on port 8000..."
388 server.register_function(add, 'add')
389
390 server.serve_forever()
391
392The client code for the preceding server::
393
394 import xmlrpclib
395
396 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
397 try:
398 proxy.add(2, 5)
Andrew Svetlov1625d882012-10-30 21:56:43 +0200399 except xmlrpclib.Fault as err:
Benjamin Peterson90f36732008-07-12 20:16:19 +0000400 print "A fault occurred"
Georg Brandl0a0cf162007-12-03 20:03:46 +0000401 print "Fault code: %d" % err.faultCode
402 print "Fault string: %s" % err.faultString
403
404
Georg Brandl8ec7f652007-08-15 14:28:01 +0000405
406.. _protocol-error-objects:
407
408ProtocolError Objects
409---------------------
410
411A :class:`ProtocolError` object describes a protocol error in the underlying
412transport layer (such as a 404 'not found' error if the server named by the URI
Senthil Kumaran6f18b982011-07-04 12:50:02 -0700413does not exist). It has the following attributes:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000414
415
416.. attribute:: ProtocolError.url
417
418 The URI or URL that triggered the error.
419
420
421.. attribute:: ProtocolError.errcode
422
423 The error code.
424
425
426.. attribute:: ProtocolError.errmsg
427
428 The error message or diagnostic string.
429
430
431.. attribute:: ProtocolError.headers
432
433 A string containing the headers of the HTTP/HTTPS request that triggered the
434 error.
435
Georg Brandl0a0cf162007-12-03 20:03:46 +0000436In the following example we're going to intentionally cause a :exc:`ProtocolError`
Georg Brandl53ffca52010-01-30 17:57:48 +0000437by providing an URI that doesn't point to an XMLRPC server::
Georg Brandl0a0cf162007-12-03 20:03:46 +0000438
439 import xmlrpclib
440
Georg Brandl53ffca52010-01-30 17:57:48 +0000441 # create a ServerProxy with an URI that doesn't respond to XMLRPC requests
442 proxy = xmlrpclib.ServerProxy("http://www.google.com/")
Georg Brandl0a0cf162007-12-03 20:03:46 +0000443
444 try:
445 proxy.some_method()
Andrew Svetlov1625d882012-10-30 21:56:43 +0200446 except xmlrpclib.ProtocolError as err:
Benjamin Peterson90f36732008-07-12 20:16:19 +0000447 print "A protocol error occurred"
Georg Brandl0a0cf162007-12-03 20:03:46 +0000448 print "URL: %s" % err.url
449 print "HTTP/HTTPS headers: %s" % err.headers
450 print "Error code: %d" % err.errcode
451 print "Error message: %s" % err.errmsg
Georg Brandl8ec7f652007-08-15 14:28:01 +0000452
453MultiCall Objects
454-----------------
455
456.. versionadded:: 2.4
457
Sandro Tosi9b680922011-08-20 17:05:15 +0200458The :class:`MultiCall` object provides a way to encapsulate multiple calls to a
459remote server into a single request [#]_.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000460
461
462.. class:: MultiCall(server)
463
464 Create an object used to boxcar method calls. *server* is the eventual target of
465 the call. Calls can be made to the result object, but they will immediately
466 return ``None``, and only store the call name and parameters in the
467 :class:`MultiCall` object. Calling the object itself causes all stored calls to
468 be transmitted as a single ``system.multicall`` request. The result of this call
Georg Brandlcf3fb252007-10-21 10:52:38 +0000469 is a :term:`generator`; iterating over this generator yields the individual
470 results.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000471
Georg Brandl0a0cf162007-12-03 20:03:46 +0000472A usage example of this class follows. The server code ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000473
Georg Brandl0a0cf162007-12-03 20:03:46 +0000474 from SimpleXMLRPCServer import SimpleXMLRPCServer
475
476 def add(x,y):
477 return x+y
478
479 def subtract(x, y):
480 return x-y
481
482 def multiply(x, y):
483 return x*y
484
485 def divide(x, y):
486 return x/y
487
488 # A simple server with simple arithmetic functions
489 server = SimpleXMLRPCServer(("localhost", 8000))
490 print "Listening on port 8000..."
491 server.register_multicall_functions()
492 server.register_function(add, 'add')
493 server.register_function(subtract, 'subtract')
494 server.register_function(multiply, 'multiply')
495 server.register_function(divide, 'divide')
496 server.serve_forever()
497
498The client code for the preceding server::
499
500 import xmlrpclib
501
502 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
503 multicall = xmlrpclib.MultiCall(proxy)
504 multicall.add(7,3)
505 multicall.subtract(7,3)
506 multicall.multiply(7,3)
507 multicall.divide(7,3)
508 result = multicall()
509
510 print "7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d" % tuple(result)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000511
512
513Convenience Functions
514---------------------
515
516
517.. function:: boolean(value)
518
519 Convert any Python value to one of the XML-RPC Boolean constants, ``True`` or
520 ``False``.
521
522
523.. function:: dumps(params[, methodname[, methodresponse[, encoding[, allow_none]]]])
524
525 Convert *params* into an XML-RPC request. or into a response if *methodresponse*
526 is true. *params* can be either a tuple of arguments or an instance of the
527 :exc:`Fault` exception class. If *methodresponse* is true, only a single value
528 can be returned, meaning that *params* must be of length 1. *encoding*, if
529 supplied, is the encoding to use in the generated XML; the default is UTF-8.
530 Python's :const:`None` value cannot be used in standard XML-RPC; to allow using
531 it via an extension, provide a true value for *allow_none*.
532
533
534.. function:: loads(data[, use_datetime])
535
536 Convert an XML-RPC request or response into Python objects, a ``(params,
537 methodname)``. *params* is a tuple of argument; *methodname* is a string, or
538 ``None`` if no method name is present in the packet. If the XML-RPC packet
539 represents a fault condition, this function will raise a :exc:`Fault` exception.
540 The *use_datetime* flag can be used to cause date/time values to be presented as
Andrew M. Kuchling085f75a2008-02-23 16:23:05 +0000541 :class:`datetime.datetime` objects; this is false by default.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000542
543 .. versionchanged:: 2.5
544 The *use_datetime* flag was added.
545
546
547.. _xmlrpc-client-example:
548
549Example of Client Usage
550-----------------------
551
552::
553
554 # simple test program (from the XML-RPC specification)
555 from xmlrpclib import ServerProxy, Error
556
557 # server = ServerProxy("http://localhost:8000") # local server
558 server = ServerProxy("http://betty.userland.com")
559
560 print server
561
562 try:
563 print server.examples.getStateName(41)
Andrew Svetlov1625d882012-10-30 21:56:43 +0200564 except Error as v:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000565 print "ERROR", v
566
567To access an XML-RPC server through a proxy, you need to define a custom
Georg Brandlb19be572007-12-29 10:57:00 +0000568transport. The following example shows how:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000569
Georg Brandlb19be572007-12-29 10:57:00 +0000570.. Example taken from http://lowlife.jp/nobonobo/wiki/xmlrpcwithproxy.html
Georg Brandl8ec7f652007-08-15 14:28:01 +0000571
572::
573
Benjamin Petersona7b55a32009-02-20 03:31:23 +0000574 import xmlrpclib, httplib
Georg Brandl8ec7f652007-08-15 14:28:01 +0000575
576 class ProxiedTransport(xmlrpclib.Transport):
577 def set_proxy(self, proxy):
578 self.proxy = proxy
579 def make_connection(self, host):
580 self.realhost = host
Georg Brandl7044b112009-01-03 21:04:55 +0000581 h = httplib.HTTP(self.proxy)
582 return h
Georg Brandl8ec7f652007-08-15 14:28:01 +0000583 def send_request(self, connection, handler, request_body):
584 connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
585 def send_host(self, connection, host):
586 connection.putheader('Host', self.realhost)
587
588 p = ProxiedTransport()
589 p.set_proxy('proxy-server:8080')
590 server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)
591 print server.currentTime.getCurrentTime()
592
Georg Brandl0a0cf162007-12-03 20:03:46 +0000593
594Example of Client and Server Usage
595----------------------------------
596
597See :ref:`simplexmlrpcserver-example`.
598
599
Sandro Tosi9b680922011-08-20 17:05:15 +0200600.. rubric:: Footnotes
601
602.. [#] This approach has been first presented in `a discussion on xmlrpc.com
603 <http://web.archive.org/web/20060624230303/http://www.xmlrpc.com/discuss/msgReader$1208?mode=topic>`_.
604.. the link now points to webarchive since the one at
605.. http://www.xmlrpc.com/discuss/msgReader%241208 is broken (and webadmin
606.. doesn't reply)