blob: a62021d47300a40b0f35ff298ae892c9a18aeca4 [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
11 Marshaller, Unmarshaller, getparser, dumps, loads, 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
Georg Brandl7f01a132009-09-16 15:58:14 +000024.. class:: ServerProxy(uri, transport=None, encoding=None, verbose=False, allow_none=False, use_datetime=False)
Georg Brandl116aa622007-08-15 14:28:22 +000025
26 A :class:`ServerProxy` instance is an object that manages communication with a
27 remote XML-RPC server. The required first argument is a URI (Uniform Resource
28 Indicator), and will normally be the URL of the server. The optional second
29 argument is a transport factory instance; by default it is an internal
30 :class:`SafeTransport` instance for https: URLs and an internal HTTP
31 :class:`Transport` instance otherwise. The optional third argument is an
32 encoding, by default UTF-8. The optional fourth argument is a debugging flag.
33 If *allow_none* is true, the Python constant ``None`` will be translated into
34 XML; the default behaviour is for ``None`` to raise a :exc:`TypeError`. This is
35 a commonly-used extension to the XML-RPC specification, but isn't supported by
36 all clients and servers; see http://ontosys.com/xml-rpc/extensions.php for a
37 description. The *use_datetime* flag can be used to cause date/time values to
38 be presented as :class:`datetime.datetime` objects; this is false by default.
Christian Heimes05e8be12008-02-23 18:30:17 +000039 :class:`datetime.datetime` objects may be passed to calls.
Georg Brandl116aa622007-08-15 14:28:22 +000040
41 Both the HTTP and HTTPS transports support the URL syntax extension for HTTP
42 Basic Authentication: ``http://user:pass@host:port/path``. The ``user:pass``
43 portion will be base64-encoded as an HTTP 'Authorization' header, and sent to
44 the remote server as part of the connection process when invoking an XML-RPC
45 method. You only need to use this if the remote server requires a Basic
46 Authentication user and password.
47
48 The returned instance is a proxy object with methods that can be used to invoke
49 corresponding RPC calls on the remote server. If the remote server supports the
50 introspection API, the proxy can also be used to query the remote server for the
51 methods it supports (service discovery) and fetch other server-associated
52 metadata.
53
54 :class:`ServerProxy` instance methods take Python basic types and objects as
55 arguments and return Python basic types and classes. Types that are conformable
56 (e.g. that can be marshalled through XML), include the following (and except
57 where noted, they are unmarshalled as the same Python type):
58
59 +---------------------------------+---------------------------------------------+
60 | Name | Meaning |
61 +=================================+=============================================+
62 | :const:`boolean` | The :const:`True` and :const:`False` |
63 | | constants |
64 +---------------------------------+---------------------------------------------+
65 | :const:`integers` | Pass in directly |
66 +---------------------------------+---------------------------------------------+
67 | :const:`floating-point numbers` | Pass in directly |
68 +---------------------------------+---------------------------------------------+
69 | :const:`strings` | Pass in directly |
70 +---------------------------------+---------------------------------------------+
71 | :const:`arrays` | Any Python sequence type containing |
72 | | conformable elements. Arrays are returned |
73 | | as lists |
74 +---------------------------------+---------------------------------------------+
75 | :const:`structures` | A Python dictionary. Keys must be strings, |
76 | | values may be any conformable type. Objects |
77 | | of user-defined classes can be passed in; |
78 | | only their *__dict__* attribute is |
79 | | transmitted. |
80 +---------------------------------+---------------------------------------------+
81 | :const:`dates` | in seconds since the epoch (pass in an |
82 | | instance of the :class:`DateTime` class) or |
Christian Heimes05e8be12008-02-23 18:30:17 +000083 | | a :class:`datetime.datetime` instance. |
Georg Brandl116aa622007-08-15 14:28:22 +000084 +---------------------------------+---------------------------------------------+
85 | :const:`binary data` | pass in an instance of the :class:`Binary` |
86 | | wrapper class |
87 +---------------------------------+---------------------------------------------+
88
89 This is the full set of data types supported by XML-RPC. Method calls may also
90 raise a special :exc:`Fault` instance, used to signal XML-RPC server errors, or
91 :exc:`ProtocolError` used to signal an error in the HTTP/HTTPS transport layer.
92 Both :exc:`Fault` and :exc:`ProtocolError` derive from a base class called
Georg Brandl38eceaa2008-05-26 11:14:17 +000093 :exc:`Error`. Note that the xmlrpc client module currently does not marshal
Georg Brandl22b34312009-07-26 14:54:51 +000094 instances of subclasses of built-in types.
Georg Brandl116aa622007-08-15 14:28:22 +000095
96 When passing strings, characters special to XML such as ``<``, ``>``, and ``&``
97 will be automatically escaped. However, it's the caller's responsibility to
98 ensure that the string is free of characters that aren't allowed in XML, such as
99 the control characters with ASCII values between 0 and 31 (except, of course,
100 tab, newline and carriage return); failing to do this will result in an XML-RPC
101 request that isn't well-formed XML. If you have to pass arbitrary strings via
102 XML-RPC, use the :class:`Binary` wrapper class described below.
103
104 :class:`Server` is retained as an alias for :class:`ServerProxy` for backwards
105 compatibility. New code should use :class:`ServerProxy`.
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108.. seealso::
109
110 `XML-RPC HOWTO <http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html>`_
Christian Heimesa62da1d2008-01-12 19:39:10 +0000111 A good description of XML-RPC operation and client software in several languages.
Georg Brandl116aa622007-08-15 14:28:22 +0000112 Contains pretty much everything an XML-RPC client developer needs to know.
113
Christian Heimesa62da1d2008-01-12 19:39:10 +0000114 `XML-RPC Introspection <http://xmlrpc-c.sourceforge.net/introspection.html>`_
115 Describes the XML-RPC protocol extension for introspection.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000117 `XML-RPC Specification <http://www.xmlrpc.com/spec>`_
118 The official specification.
119
120 `Unofficial XML-RPC Errata <http://effbot.org/zone/xmlrpc-errata.htm>`_
121 Fredrik Lundh's "unofficial errata, intended to clarify certain
122 details in the XML-RPC specification, as well as hint at
123 'best practices' to use when designing your own XML-RPC
124 implementations."
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126.. _serverproxy-objects:
127
128ServerProxy Objects
129-------------------
130
131A :class:`ServerProxy` instance has a method corresponding to each remote
132procedure call accepted by the XML-RPC server. Calling the method performs an
133RPC, dispatched by both name and argument signature (e.g. the same method name
134can be overloaded with multiple argument signatures). The RPC finishes by
135returning a value, which may be either returned data in a conformant type or a
136:class:`Fault` or :class:`ProtocolError` object indicating an error.
137
138Servers that support the XML introspection API support some common methods
139grouped under the reserved :attr:`system` member:
140
141
142.. method:: ServerProxy.system.listMethods()
143
144 This method returns a list of strings, one for each (non-system) method
145 supported by the XML-RPC server.
146
147
148.. method:: ServerProxy.system.methodSignature(name)
149
150 This method takes one parameter, the name of a method implemented by the XML-RPC
Georg Brandl94606482009-05-04 20:46:44 +0000151 server. It returns an array of possible signatures for this method. A signature
Georg Brandl116aa622007-08-15 14:28:22 +0000152 is an array of types. The first of these types is the return type of the method,
153 the rest are parameters.
154
155 Because multiple signatures (ie. overloading) is permitted, this method returns
156 a list of signatures rather than a singleton.
157
158 Signatures themselves are restricted to the top level parameters expected by a
159 method. For instance if a method expects one array of structs as a parameter,
160 and it returns a string, its signature is simply "string, array". If it expects
161 three integers and returns a string, its signature is "string, int, int, int".
162
163 If no signature is defined for the method, a non-array value is returned. In
164 Python this means that the type of the returned value will be something other
Georg Brandl94606482009-05-04 20:46:44 +0000165 than list.
Georg Brandl116aa622007-08-15 14:28:22 +0000166
167
168.. method:: ServerProxy.system.methodHelp(name)
169
170 This method takes one parameter, the name of a method implemented by the XML-RPC
171 server. It returns a documentation string describing the use of that method. If
172 no such string is available, an empty string is returned. The documentation
173 string may contain HTML markup.
174
Georg Brandl116aa622007-08-15 14:28:22 +0000175
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000176A working example follows. The server code::
177
Georg Brandl38eceaa2008-05-26 11:14:17 +0000178 from xmlrpc.server import SimpleXMLRPCServer
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000179
180 def is_even(n):
181 return n%2 == 0
182
183 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000184 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000185 server.register_function(is_even, "is_even")
186 server.serve_forever()
187
188The client code for the preceding server::
189
Georg Brandl38eceaa2008-05-26 11:14:17 +0000190 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000191
Georg Brandl38eceaa2008-05-26 11:14:17 +0000192 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
Georg Brandlf6945182008-02-01 11:56:49 +0000193 print("3 is even: %s" % str(proxy.is_even(3)))
194 print("100 is even: %s" % str(proxy.is_even(100)))
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196.. _datetime-objects:
197
198DateTime Objects
199----------------
200
Christian Heimes05e8be12008-02-23 18:30:17 +0000201This class may be initialized with seconds since the epoch, a time
202tuple, an ISO 8601 time/date string, or a :class:`datetime.datetime`
203instance. It has the following methods, supported mainly for internal
204use by the marshalling/unmarshalling code:
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206
207.. method:: DateTime.decode(string)
208
209 Accept a string as the instance's new time value.
210
211
212.. method:: DateTime.encode(out)
213
214 Write the XML-RPC encoding of this :class:`DateTime` item to the *out* stream
215 object.
216
Georg Brandl05f5ab72008-09-24 09:11:47 +0000217It also supports certain of Python's built-in operators through rich comparison
Georg Brandl116aa622007-08-15 14:28:22 +0000218and :meth:`__repr__` methods.
219
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000220A working example follows. The server code::
221
222 import datetime
Georg Brandl38eceaa2008-05-26 11:14:17 +0000223 from xmlrpc.server import SimpleXMLRPCServer
224 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000225
226 def today():
227 today = datetime.datetime.today()
Georg Brandl38eceaa2008-05-26 11:14:17 +0000228 return xmlrpc.client.DateTime(today)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000229
230 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000231 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000232 server.register_function(today, "today")
233 server.serve_forever()
234
235The client code for the preceding server::
236
Georg Brandl38eceaa2008-05-26 11:14:17 +0000237 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000238 import datetime
239
Georg Brandl38eceaa2008-05-26 11:14:17 +0000240 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000241
242 today = proxy.today()
243 # convert the ISO8601 string to a datetime object
244 converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")
Georg Brandlf6945182008-02-01 11:56:49 +0000245 print("Today: %s" % converted.strftime("%d.%m.%Y, %H:%M"))
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247.. _binary-objects:
248
249Binary Objects
250--------------
251
252This class may be initialized from string data (which may include NULs). The
253primary access to the content of a :class:`Binary` object is provided by an
254attribute:
255
256
257.. attribute:: Binary.data
258
259 The binary data encapsulated by the :class:`Binary` instance. The data is
260 provided as an 8-bit string.
261
262:class:`Binary` objects have the following methods, supported mainly for
263internal use by the marshalling/unmarshalling code:
264
265
266.. method:: Binary.decode(string)
267
268 Accept a base64 string and decode it as the instance's new data.
269
270
271.. method:: Binary.encode(out)
272
273 Write the XML-RPC base 64 encoding of this binary item to the out stream object.
274
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000275 The encoded data will have newlines every 76 characters as per
276 `RFC 2045 section 6.8 <http://tools.ietf.org/html/rfc2045#section-6.8>`_,
277 which was the de facto standard base64 specification when the
278 XML-RPC spec was written.
279
Georg Brandl05f5ab72008-09-24 09:11:47 +0000280It also supports certain of Python's built-in operators through :meth:`__eq__`
281and :meth:`__ne__` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000282
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000283Example usage of the binary objects. We're going to transfer an image over
284XMLRPC::
285
Georg Brandl38eceaa2008-05-26 11:14:17 +0000286 from xmlrpc.server import SimpleXMLRPCServer
287 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000288
289 def python_logo():
Victor Stinner757db832010-01-30 02:16:55 +0000290 with open("python_logo.jpg", "rb") as handle:
291 return xmlrpc.client.Binary(handle.read())
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000292
293 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000294 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000295 server.register_function(python_logo, 'python_logo')
296
297 server.serve_forever()
298
299The client gets the image and saves it to a file::
300
Georg Brandl38eceaa2008-05-26 11:14:17 +0000301 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000302
Georg Brandl38eceaa2008-05-26 11:14:17 +0000303 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
Victor Stinner757db832010-01-30 02:16:55 +0000304 with open("fetched_python_logo.jpg", "wb") as handle:
305 handle.write(proxy.python_logo().data)
Georg Brandl116aa622007-08-15 14:28:22 +0000306
307.. _fault-objects:
308
309Fault Objects
310-------------
311
312A :class:`Fault` object encapsulates the content of an XML-RPC fault tag. Fault
313objects have the following members:
314
315
316.. attribute:: Fault.faultCode
317
318 A string indicating the fault type.
319
320
321.. attribute:: Fault.faultString
322
323 A string containing a diagnostic message associated with the fault.
324
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000325In the following example we're going to intentionally cause a :exc:`Fault` by
326returning a complex type object. The server code::
327
Georg Brandl38eceaa2008-05-26 11:14:17 +0000328 from xmlrpc.server import SimpleXMLRPCServer
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000329
330 # A marshalling error is going to occur because we're returning a
331 # complex number
332 def add(x,y):
333 return x+y+0j
334
335 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000336 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000337 server.register_function(add, 'add')
338
339 server.serve_forever()
340
341The client code for the preceding server::
342
Georg Brandl38eceaa2008-05-26 11:14:17 +0000343 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000344
Georg Brandl38eceaa2008-05-26 11:14:17 +0000345 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000346 try:
347 proxy.add(2, 5)
Georg Brandl0dedf452009-05-22 16:44:06 +0000348 except xmlrpc.client.Fault as err:
Georg Brandl2ee470f2008-07-16 12:55:28 +0000349 print("A fault occurred")
Georg Brandlf6945182008-02-01 11:56:49 +0000350 print("Fault code: %d" % err.faultCode)
351 print("Fault string: %s" % err.faultString)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000352
353
Georg Brandl116aa622007-08-15 14:28:22 +0000354
355.. _protocol-error-objects:
356
357ProtocolError Objects
358---------------------
359
360A :class:`ProtocolError` object describes a protocol error in the underlying
361transport layer (such as a 404 'not found' error if the server named by the URI
362does not exist). It has the following members:
363
364
365.. attribute:: ProtocolError.url
366
367 The URI or URL that triggered the error.
368
369
370.. attribute:: ProtocolError.errcode
371
372 The error code.
373
374
375.. attribute:: ProtocolError.errmsg
376
377 The error message or diagnostic string.
378
379
380.. attribute:: ProtocolError.headers
381
Guido van Rossum460add42007-08-23 02:13:35 +0000382 A dict containing the headers of the HTTP/HTTPS request that triggered the
Georg Brandl116aa622007-08-15 14:28:22 +0000383 error.
384
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000385In the following example we're going to intentionally cause a :exc:`ProtocolError`
386by providing an invalid URI::
387
Georg Brandl38eceaa2008-05-26 11:14:17 +0000388 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000389
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000390 # create a ServerProxy with an URI that doesn't respond to XMLRPC requests
391 proxy = xmlrpc.client.ServerProxy("http://google.com/")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000392
393 try:
394 proxy.some_method()
Georg Brandl0dedf452009-05-22 16:44:06 +0000395 except xmlrpc.client.ProtocolError as err:
Georg Brandl2ee470f2008-07-16 12:55:28 +0000396 print("A protocol error occurred")
Georg Brandlf6945182008-02-01 11:56:49 +0000397 print("URL: %s" % err.url)
398 print("HTTP/HTTPS headers: %s" % err.headers)
399 print("Error code: %d" % err.errcode)
400 print("Error message: %s" % err.errmsg)
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402MultiCall Objects
403-----------------
404
Georg Brandl116aa622007-08-15 14:28:22 +0000405In http://www.xmlrpc.com/discuss/msgReader%241208, an approach is presented to
406encapsulate multiple calls to a remote server into a single request.
407
408
409.. class:: MultiCall(server)
410
411 Create an object used to boxcar method calls. *server* is the eventual target of
412 the call. Calls can be made to the result object, but they will immediately
413 return ``None``, and only store the call name and parameters in the
414 :class:`MultiCall` object. Calling the object itself causes all stored calls to
415 be transmitted as a single ``system.multicall`` request. The result of this call
Georg Brandl9afde1c2007-11-01 20:32:30 +0000416 is a :term:`generator`; iterating over this generator yields the individual
417 results.
Georg Brandl116aa622007-08-15 14:28:22 +0000418
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000419A usage example of this class follows. The server code ::
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Georg Brandl38eceaa2008-05-26 11:14:17 +0000421 from xmlrpc.server import SimpleXMLRPCServer
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000422
423 def add(x,y):
424 return x+y
425
426 def subtract(x, y):
427 return x-y
428
429 def multiply(x, y):
430 return x*y
431
432 def divide(x, y):
433 return x/y
434
435 # A simple server with simple arithmetic functions
436 server = SimpleXMLRPCServer(("localhost", 8000))
Georg Brandlf6945182008-02-01 11:56:49 +0000437 print("Listening on port 8000...")
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000438 server.register_multicall_functions()
439 server.register_function(add, 'add')
440 server.register_function(subtract, 'subtract')
441 server.register_function(multiply, 'multiply')
442 server.register_function(divide, 'divide')
443 server.serve_forever()
444
445The client code for the preceding server::
446
Georg Brandl38eceaa2008-05-26 11:14:17 +0000447 import xmlrpc.client
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000448
Georg Brandl38eceaa2008-05-26 11:14:17 +0000449 proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
450 multicall = xmlrpc.client.MultiCall(proxy)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000451 multicall.add(7,3)
452 multicall.subtract(7,3)
453 multicall.multiply(7,3)
454 multicall.divide(7,3)
455 result = multicall()
456
Georg Brandlf6945182008-02-01 11:56:49 +0000457 print("7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d" % tuple(result))
Georg Brandl116aa622007-08-15 14:28:22 +0000458
459
460Convenience Functions
461---------------------
462
Georg Brandl7f01a132009-09-16 15:58:14 +0000463.. function:: dumps(params, methodname=None, methodresponse=None, encoding=None, allow_none=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000464
465 Convert *params* into an XML-RPC request. or into a response if *methodresponse*
466 is true. *params* can be either a tuple of arguments or an instance of the
467 :exc:`Fault` exception class. If *methodresponse* is true, only a single value
468 can be returned, meaning that *params* must be of length 1. *encoding*, if
469 supplied, is the encoding to use in the generated XML; the default is UTF-8.
470 Python's :const:`None` value cannot be used in standard XML-RPC; to allow using
471 it via an extension, provide a true value for *allow_none*.
472
473
Georg Brandl7f01a132009-09-16 15:58:14 +0000474.. function:: loads(data, use_datetime=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000475
476 Convert an XML-RPC request or response into Python objects, a ``(params,
477 methodname)``. *params* is a tuple of argument; *methodname* is a string, or
478 ``None`` if no method name is present in the packet. If the XML-RPC packet
479 represents a fault condition, this function will raise a :exc:`Fault` exception.
480 The *use_datetime* flag can be used to cause date/time values to be presented as
Christian Heimes05e8be12008-02-23 18:30:17 +0000481 :class:`datetime.datetime` objects; this is false by default.
Georg Brandl116aa622007-08-15 14:28:22 +0000482
Georg Brandl116aa622007-08-15 14:28:22 +0000483
484.. _xmlrpc-client-example:
485
486Example of Client Usage
487-----------------------
488
489::
490
491 # simple test program (from the XML-RPC specification)
Georg Brandl38eceaa2008-05-26 11:14:17 +0000492 from xmlrpc.client import ServerProxy, Error
Georg Brandl116aa622007-08-15 14:28:22 +0000493
494 # server = ServerProxy("http://localhost:8000") # local server
495 server = ServerProxy("http://betty.userland.com")
496
Collin Winterc79461b2007-09-01 23:34:30 +0000497 print(server)
Georg Brandl116aa622007-08-15 14:28:22 +0000498
499 try:
Collin Winterc79461b2007-09-01 23:34:30 +0000500 print(server.examples.getStateName(41))
Georg Brandl116aa622007-08-15 14:28:22 +0000501 except Error as v:
Collin Winterc79461b2007-09-01 23:34:30 +0000502 print("ERROR", v)
Georg Brandl116aa622007-08-15 14:28:22 +0000503
504To access an XML-RPC server through a proxy, you need to define a custom
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000505transport. The following example shows how:
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000507.. Example taken from http://lowlife.jp/nobonobo/wiki/xmlrpcwithproxy.html
Georg Brandl116aa622007-08-15 14:28:22 +0000508
509::
510
Georg Brandl24420152008-05-26 16:32:26 +0000511 import xmlrpc.client, http.client
Georg Brandl116aa622007-08-15 14:28:22 +0000512
Georg Brandl38eceaa2008-05-26 11:14:17 +0000513 class ProxiedTransport(xmlrpc.client.Transport):
Georg Brandl116aa622007-08-15 14:28:22 +0000514 def set_proxy(self, proxy):
515 self.proxy = proxy
516 def make_connection(self, host):
517 self.realhost = host
Georg Brandl06788c92009-01-03 21:31:47 +0000518 h = http.client.HTTP(self.proxy)
519 return h
Georg Brandl116aa622007-08-15 14:28:22 +0000520 def send_request(self, connection, handler, request_body):
521 connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
522 def send_host(self, connection, host):
523 connection.putheader('Host', self.realhost)
524
525 p = ProxiedTransport()
526 p.set_proxy('proxy-server:8080')
Georg Brandl38eceaa2008-05-26 11:14:17 +0000527 server = xmlrpc.client.Server('http://time.xmlrpc.com/RPC2', transport=p)
Collin Winterc79461b2007-09-01 23:34:30 +0000528 print(server.currentTime.getCurrentTime())
Georg Brandl116aa622007-08-15 14:28:22 +0000529
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000530
531Example of Client and Server Usage
532----------------------------------
533
534See :ref:`simplexmlrpcserver-example`.
535
536