blob: 766a796c3dc09f9c8d06579477ddcbdd6ce8c81e [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 Peterson078ece22014-10-13 11:53:54 -040037.. warning::
38
39 In the case of https URIS, :mod:`xmlrpclib` does not do any verification of
40 the server's certificate.
41
Christian Heimes23790b42013-03-26 17:53:05 +010042
Georg Brandl8ec7f652007-08-15 14:28:01 +000043.. class:: ServerProxy(uri[, transport[, encoding[, verbose[, allow_none[, use_datetime]]]]])
44
45 A :class:`ServerProxy` instance is an object that manages communication with a
46 remote XML-RPC server. The required first argument is a URI (Uniform Resource
47 Indicator), and will normally be the URL of the server. The optional second
48 argument is a transport factory instance; by default it is an internal
49 :class:`SafeTransport` instance for https: URLs and an internal HTTP
50 :class:`Transport` instance otherwise. The optional third argument is an
51 encoding, by default UTF-8. The optional fourth argument is a debugging flag.
52 If *allow_none* is true, the Python constant ``None`` will be translated into
53 XML; the default behaviour is for ``None`` to raise a :exc:`TypeError`. This is
54 a commonly-used extension to the XML-RPC specification, but isn't supported by
55 all clients and servers; see http://ontosys.com/xml-rpc/extensions.php for a
56 description. The *use_datetime* flag can be used to cause date/time values to
57 be presented as :class:`datetime.datetime` objects; this is false by default.
Andrew M. Kuchling085f75a2008-02-23 16:23:05 +000058 :class:`datetime.datetime` objects may be passed to calls.
Georg Brandl8ec7f652007-08-15 14:28:01 +000059
60 Both the HTTP and HTTPS transports support the URL syntax extension for HTTP
61 Basic Authentication: ``http://user:pass@host:port/path``. The ``user:pass``
62 portion will be base64-encoded as an HTTP 'Authorization' header, and sent to
63 the remote server as part of the connection process when invoking an XML-RPC
64 method. You only need to use this if the remote server requires a Basic
65 Authentication user and password.
66
67 The returned instance is a proxy object with methods that can be used to invoke
68 corresponding RPC calls on the remote server. If the remote server supports the
69 introspection API, the proxy can also be used to query the remote server for the
70 methods it supports (service discovery) and fetch other server-associated
71 metadata.
72
73 :class:`ServerProxy` instance methods take Python basic types and objects as
74 arguments and return Python basic types and classes. Types that are conformable
75 (e.g. that can be marshalled through XML), include the following (and except
76 where noted, they are unmarshalled as the same Python type):
77
78 +---------------------------------+---------------------------------------------+
79 | Name | Meaning |
80 +=================================+=============================================+
81 | :const:`boolean` | The :const:`True` and :const:`False` |
82 | | constants |
83 +---------------------------------+---------------------------------------------+
84 | :const:`integers` | Pass in directly |
85 +---------------------------------+---------------------------------------------+
86 | :const:`floating-point numbers` | Pass in directly |
87 +---------------------------------+---------------------------------------------+
88 | :const:`strings` | Pass in directly |
89 +---------------------------------+---------------------------------------------+
90 | :const:`arrays` | Any Python sequence type containing |
91 | | conformable elements. Arrays are returned |
92 | | as lists |
93 +---------------------------------+---------------------------------------------+
94 | :const:`structures` | A Python dictionary. Keys must be strings, |
95 | | values may be any conformable type. Objects |
96 | | of user-defined classes can be passed in; |
97 | | only their *__dict__* attribute is |
98 | | transmitted. |
99 +---------------------------------+---------------------------------------------+
100 | :const:`dates` | in seconds since the epoch (pass in an |
101 | | instance of the :class:`DateTime` class) or |
Andrew M. Kuchling085f75a2008-02-23 16:23:05 +0000102 | | a :class:`datetime.datetime` instance. |
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103 +---------------------------------+---------------------------------------------+
104 | :const:`binary data` | pass in an instance of the :class:`Binary` |
105 | | wrapper class |
106 +---------------------------------+---------------------------------------------+
107
108 This is the full set of data types supported by XML-RPC. Method calls may also
109 raise a special :exc:`Fault` instance, used to signal XML-RPC server errors, or
110 :exc:`ProtocolError` used to signal an error in the HTTP/HTTPS transport layer.
111 Both :exc:`Fault` and :exc:`ProtocolError` derive from a base class called
112 :exc:`Error`. Note that even though starting with Python 2.2 you can subclass
Georg Brandld7d4fd72009-07-26 14:37:28 +0000113 built-in types, the xmlrpclib module currently does not marshal instances of such
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114 subclasses.
115
116 When passing strings, characters special to XML such as ``<``, ``>``, and ``&``
117 will be automatically escaped. However, it's the caller's responsibility to
118 ensure that the string is free of characters that aren't allowed in XML, such as
119 the control characters with ASCII values between 0 and 31 (except, of course,
120 tab, newline and carriage return); failing to do this will result in an XML-RPC
121 request that isn't well-formed XML. If you have to pass arbitrary strings via
122 XML-RPC, use the :class:`Binary` wrapper class described below.
123
124 :class:`Server` is retained as an alias for :class:`ServerProxy` for backwards
125 compatibility. New code should use :class:`ServerProxy`.
126
127 .. versionchanged:: 2.5
128 The *use_datetime* flag was added.
129
130 .. versionchanged:: 2.6
Georg Brandla7395032007-10-21 12:15:05 +0000131 Instances of :term:`new-style class`\es can be passed in if they have an
132 *__dict__* attribute and don't have a base class that is marshalled in a
133 special way.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134
135
136.. seealso::
137
138 `XML-RPC HOWTO <http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html>`_
Andrew M. Kuchlingde680372008-01-11 19:33:24 +0000139 A good description of XML-RPC operation and client software in several languages.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140 Contains pretty much everything an XML-RPC client developer needs to know.
141
Andrew M. Kuchlingde680372008-01-11 19:33:24 +0000142 `XML-RPC Introspection <http://xmlrpc-c.sourceforge.net/introspection.html>`_
143 Describes the XML-RPC protocol extension for introspection.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000144
Skip Montanaro6d9aafa2008-04-22 22:45:09 +0000145 `XML-RPC Specification <http://www.xmlrpc.com/spec>`_
146 The official specification.
147
148 `Unofficial XML-RPC Errata <http://effbot.org/zone/xmlrpc-errata.htm>`_
149 Fredrik Lundh's "unofficial errata, intended to clarify certain
150 details in the XML-RPC specification, as well as hint at
151 'best practices' to use when designing your own XML-RPC
152 implementations."
Georg Brandl8ec7f652007-08-15 14:28:01 +0000153
154.. _serverproxy-objects:
155
156ServerProxy Objects
157-------------------
158
159A :class:`ServerProxy` instance has a method corresponding to each remote
160procedure call accepted by the XML-RPC server. Calling the method performs an
161RPC, dispatched by both name and argument signature (e.g. the same method name
162can be overloaded with multiple argument signatures). The RPC finishes by
163returning a value, which may be either returned data in a conformant type or a
164:class:`Fault` or :class:`ProtocolError` object indicating an error.
165
166Servers that support the XML introspection API support some common methods
Senthil Kumaran6f18b982011-07-04 12:50:02 -0700167grouped under the reserved :attr:`system` attribute:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000168
169
170.. method:: ServerProxy.system.listMethods()
171
172 This method returns a list of strings, one for each (non-system) method
173 supported by the XML-RPC server.
174
175
176.. method:: ServerProxy.system.methodSignature(name)
177
178 This method takes one parameter, the name of a method implemented by the XML-RPC
Georg Brandlf5f045e2009-05-04 20:45:13 +0000179 server. It returns an array of possible signatures for this method. A signature
Georg Brandl8ec7f652007-08-15 14:28:01 +0000180 is an array of types. The first of these types is the return type of the method,
181 the rest are parameters.
182
183 Because multiple signatures (ie. overloading) is permitted, this method returns
184 a list of signatures rather than a singleton.
185
186 Signatures themselves are restricted to the top level parameters expected by a
187 method. For instance if a method expects one array of structs as a parameter,
188 and it returns a string, its signature is simply "string, array". If it expects
189 three integers and returns a string, its signature is "string, int, int, int".
190
191 If no signature is defined for the method, a non-array value is returned. In
192 Python this means that the type of the returned value will be something other
Georg Brandlf5f045e2009-05-04 20:45:13 +0000193 than list.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000194
195
196.. method:: ServerProxy.system.methodHelp(name)
197
198 This method takes one parameter, the name of a method implemented by the XML-RPC
199 server. It returns a documentation string describing the use of that method. If
200 no such string is available, an empty string is returned. The documentation
201 string may contain HTML markup.
202
Georg Brandl8ec7f652007-08-15 14:28:01 +0000203
204.. _boolean-objects:
205
206Boolean Objects
207---------------
208
209This class may be initialized from any Python value; the instance returned
210depends only on its truth value. It supports various Python operators through
211:meth:`__cmp__`, :meth:`__repr__`, :meth:`__int__`, and :meth:`__nonzero__`
212methods, all implemented in the obvious ways.
213
214It also has the following method, supported mainly for internal use by the
215unmarshalling code:
216
217
218.. method:: Boolean.encode(out)
219
220 Write the XML-RPC encoding of this Boolean item to the out stream object.
221
Georg Brandl0a0cf162007-12-03 20:03:46 +0000222A working example follows. The server code::
223
224 import xmlrpclib
225 from SimpleXMLRPCServer import SimpleXMLRPCServer
226
227 def is_even(n):
228 return n%2 == 0
229
230 server = SimpleXMLRPCServer(("localhost", 8000))
231 print "Listening on port 8000..."
232 server.register_function(is_even, "is_even")
233 server.serve_forever()
234
235The client code for the preceding server::
236
237 import xmlrpclib
238
239 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
240 print "3 is even: %s" % str(proxy.is_even(3))
241 print "100 is even: %s" % str(proxy.is_even(100))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000242
243.. _datetime-objects:
244
245DateTime Objects
246----------------
247
Andrew M. Kuchling085f75a2008-02-23 16:23:05 +0000248This class may be initialized with seconds since the epoch, a time
249tuple, an ISO 8601 time/date string, or a :class:`datetime.datetime`
250instance. It has the following methods, supported mainly for internal
251use by the marshalling/unmarshalling code:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000252
253
254.. method:: DateTime.decode(string)
255
256 Accept a string as the instance's new time value.
257
258
259.. method:: DateTime.encode(out)
260
261 Write the XML-RPC encoding of this :class:`DateTime` item to the *out* stream
262 object.
263
264It also supports certain of Python's built-in operators through :meth:`__cmp__`
265and :meth:`__repr__` methods.
266
Georg Brandl0a0cf162007-12-03 20:03:46 +0000267A working example follows. The server code::
268
269 import datetime
270 from SimpleXMLRPCServer import SimpleXMLRPCServer
271 import xmlrpclib
272
273 def today():
274 today = datetime.datetime.today()
275 return xmlrpclib.DateTime(today)
276
277 server = SimpleXMLRPCServer(("localhost", 8000))
278 print "Listening on port 8000..."
279 server.register_function(today, "today")
280 server.serve_forever()
281
282The client code for the preceding server::
283
284 import xmlrpclib
285 import datetime
286
287 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
288
289 today = proxy.today()
290 # convert the ISO8601 string to a datetime object
291 converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")
292 print "Today: %s" % converted.strftime("%d.%m.%Y, %H:%M")
Georg Brandl8ec7f652007-08-15 14:28:01 +0000293
294.. _binary-objects:
295
296Binary Objects
297--------------
298
299This class may be initialized from string data (which may include NULs). The
300primary access to the content of a :class:`Binary` object is provided by an
301attribute:
302
303
304.. attribute:: Binary.data
305
306 The binary data encapsulated by the :class:`Binary` instance. The data is
307 provided as an 8-bit string.
308
309:class:`Binary` objects have the following methods, supported mainly for
310internal use by the marshalling/unmarshalling code:
311
312
313.. method:: Binary.decode(string)
314
315 Accept a base64 string and decode it as the instance's new data.
316
317
318.. method:: Binary.encode(out)
319
320 Write the XML-RPC base 64 encoding of this binary item to the out stream object.
321
Skip Montanaro6d9aafa2008-04-22 22:45:09 +0000322 The encoded data will have newlines every 76 characters as per
323 `RFC 2045 section 6.8 <http://tools.ietf.org/html/rfc2045#section-6.8>`_,
324 which was the de facto standard base64 specification when the
325 XML-RPC spec was written.
326
Georg Brandl8ec7f652007-08-15 14:28:01 +0000327It also supports certain of Python's built-in operators through a
328:meth:`__cmp__` method.
329
Georg Brandl0a0cf162007-12-03 20:03:46 +0000330Example usage of the binary objects. We're going to transfer an image over
331XMLRPC::
332
333 from SimpleXMLRPCServer import SimpleXMLRPCServer
334 import xmlrpclib
335
336 def python_logo():
Victor Stinner75d3fb12010-01-30 02:00:26 +0000337 with open("python_logo.jpg", "rb") as handle:
Georg Brandl34feea32009-02-07 12:21:17 +0000338 return xmlrpclib.Binary(handle.read())
Georg Brandl0a0cf162007-12-03 20:03:46 +0000339
340 server = SimpleXMLRPCServer(("localhost", 8000))
341 print "Listening on port 8000..."
342 server.register_function(python_logo, 'python_logo')
343
344 server.serve_forever()
345
346The client gets the image and saves it to a file::
347
348 import xmlrpclib
349
350 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
Victor Stinner75d3fb12010-01-30 02:00:26 +0000351 with open("fetched_python_logo.jpg", "wb") as handle:
Georg Brandl34feea32009-02-07 12:21:17 +0000352 handle.write(proxy.python_logo().data)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000353
354.. _fault-objects:
355
356Fault Objects
357-------------
358
359A :class:`Fault` object encapsulates the content of an XML-RPC fault tag. Fault
Senthil Kumaran6f18b982011-07-04 12:50:02 -0700360objects have the following attributes:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000361
362
363.. attribute:: Fault.faultCode
364
365 A string indicating the fault type.
366
367
368.. attribute:: Fault.faultString
369
370 A string containing a diagnostic message associated with the fault.
371
Georg Brandl0a0cf162007-12-03 20:03:46 +0000372In the following example we're going to intentionally cause a :exc:`Fault` by
373returning a complex type object. The server code::
374
375 from SimpleXMLRPCServer import SimpleXMLRPCServer
376
377 # A marshalling error is going to occur because we're returning a
378 # complex number
379 def add(x,y):
380 return x+y+0j
381
382 server = SimpleXMLRPCServer(("localhost", 8000))
383 print "Listening on port 8000..."
384 server.register_function(add, 'add')
385
386 server.serve_forever()
387
388The client code for the preceding server::
389
390 import xmlrpclib
391
392 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
393 try:
394 proxy.add(2, 5)
Andrew Svetlov1625d882012-10-30 21:56:43 +0200395 except xmlrpclib.Fault as err:
Benjamin Peterson90f36732008-07-12 20:16:19 +0000396 print "A fault occurred"
Georg Brandl0a0cf162007-12-03 20:03:46 +0000397 print "Fault code: %d" % err.faultCode
398 print "Fault string: %s" % err.faultString
399
400
Georg Brandl8ec7f652007-08-15 14:28:01 +0000401
402.. _protocol-error-objects:
403
404ProtocolError Objects
405---------------------
406
407A :class:`ProtocolError` object describes a protocol error in the underlying
408transport layer (such as a 404 'not found' error if the server named by the URI
Senthil Kumaran6f18b982011-07-04 12:50:02 -0700409does not exist). It has the following attributes:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000410
411
412.. attribute:: ProtocolError.url
413
414 The URI or URL that triggered the error.
415
416
417.. attribute:: ProtocolError.errcode
418
419 The error code.
420
421
422.. attribute:: ProtocolError.errmsg
423
424 The error message or diagnostic string.
425
426
427.. attribute:: ProtocolError.headers
428
429 A string containing the headers of the HTTP/HTTPS request that triggered the
430 error.
431
Georg Brandl0a0cf162007-12-03 20:03:46 +0000432In the following example we're going to intentionally cause a :exc:`ProtocolError`
Georg Brandl53ffca52010-01-30 17:57:48 +0000433by providing an URI that doesn't point to an XMLRPC server::
Georg Brandl0a0cf162007-12-03 20:03:46 +0000434
435 import xmlrpclib
436
Georg Brandl53ffca52010-01-30 17:57:48 +0000437 # create a ServerProxy with an URI that doesn't respond to XMLRPC requests
438 proxy = xmlrpclib.ServerProxy("http://www.google.com/")
Georg Brandl0a0cf162007-12-03 20:03:46 +0000439
440 try:
441 proxy.some_method()
Andrew Svetlov1625d882012-10-30 21:56:43 +0200442 except xmlrpclib.ProtocolError as err:
Benjamin Peterson90f36732008-07-12 20:16:19 +0000443 print "A protocol error occurred"
Georg Brandl0a0cf162007-12-03 20:03:46 +0000444 print "URL: %s" % err.url
445 print "HTTP/HTTPS headers: %s" % err.headers
446 print "Error code: %d" % err.errcode
447 print "Error message: %s" % err.errmsg
Georg Brandl8ec7f652007-08-15 14:28:01 +0000448
449MultiCall Objects
450-----------------
451
452.. versionadded:: 2.4
453
Sandro Tosi9b680922011-08-20 17:05:15 +0200454The :class:`MultiCall` object provides a way to encapsulate multiple calls to a
455remote server into a single request [#]_.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000456
457
458.. class:: MultiCall(server)
459
460 Create an object used to boxcar method calls. *server* is the eventual target of
461 the call. Calls can be made to the result object, but they will immediately
462 return ``None``, and only store the call name and parameters in the
463 :class:`MultiCall` object. Calling the object itself causes all stored calls to
464 be transmitted as a single ``system.multicall`` request. The result of this call
Georg Brandlcf3fb252007-10-21 10:52:38 +0000465 is a :term:`generator`; iterating over this generator yields the individual
466 results.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000467
Georg Brandl0a0cf162007-12-03 20:03:46 +0000468A usage example of this class follows. The server code ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000469
Georg Brandl0a0cf162007-12-03 20:03:46 +0000470 from SimpleXMLRPCServer import SimpleXMLRPCServer
471
472 def add(x,y):
473 return x+y
474
475 def subtract(x, y):
476 return x-y
477
478 def multiply(x, y):
479 return x*y
480
481 def divide(x, y):
482 return x/y
483
484 # A simple server with simple arithmetic functions
485 server = SimpleXMLRPCServer(("localhost", 8000))
486 print "Listening on port 8000..."
487 server.register_multicall_functions()
488 server.register_function(add, 'add')
489 server.register_function(subtract, 'subtract')
490 server.register_function(multiply, 'multiply')
491 server.register_function(divide, 'divide')
492 server.serve_forever()
493
494The client code for the preceding server::
495
496 import xmlrpclib
497
498 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
499 multicall = xmlrpclib.MultiCall(proxy)
500 multicall.add(7,3)
501 multicall.subtract(7,3)
502 multicall.multiply(7,3)
503 multicall.divide(7,3)
504 result = multicall()
505
506 print "7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d" % tuple(result)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000507
508
509Convenience Functions
510---------------------
511
512
513.. function:: boolean(value)
514
515 Convert any Python value to one of the XML-RPC Boolean constants, ``True`` or
516 ``False``.
517
518
519.. function:: dumps(params[, methodname[, methodresponse[, encoding[, allow_none]]]])
520
521 Convert *params* into an XML-RPC request. or into a response if *methodresponse*
522 is true. *params* can be either a tuple of arguments or an instance of the
523 :exc:`Fault` exception class. If *methodresponse* is true, only a single value
524 can be returned, meaning that *params* must be of length 1. *encoding*, if
525 supplied, is the encoding to use in the generated XML; the default is UTF-8.
526 Python's :const:`None` value cannot be used in standard XML-RPC; to allow using
527 it via an extension, provide a true value for *allow_none*.
528
529
530.. function:: loads(data[, use_datetime])
531
532 Convert an XML-RPC request or response into Python objects, a ``(params,
533 methodname)``. *params* is a tuple of argument; *methodname* is a string, or
534 ``None`` if no method name is present in the packet. If the XML-RPC packet
535 represents a fault condition, this function will raise a :exc:`Fault` exception.
536 The *use_datetime* flag can be used to cause date/time values to be presented as
Andrew M. Kuchling085f75a2008-02-23 16:23:05 +0000537 :class:`datetime.datetime` objects; this is false by default.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000538
539 .. versionchanged:: 2.5
540 The *use_datetime* flag was added.
541
542
543.. _xmlrpc-client-example:
544
545Example of Client Usage
546-----------------------
547
548::
549
550 # simple test program (from the XML-RPC specification)
551 from xmlrpclib import ServerProxy, Error
552
553 # server = ServerProxy("http://localhost:8000") # local server
554 server = ServerProxy("http://betty.userland.com")
555
556 print server
557
558 try:
559 print server.examples.getStateName(41)
Andrew Svetlov1625d882012-10-30 21:56:43 +0200560 except Error as v:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000561 print "ERROR", v
562
563To access an XML-RPC server through a proxy, you need to define a custom
Georg Brandlb19be572007-12-29 10:57:00 +0000564transport. The following example shows how:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000565
Georg Brandlb19be572007-12-29 10:57:00 +0000566.. Example taken from http://lowlife.jp/nobonobo/wiki/xmlrpcwithproxy.html
Georg Brandl8ec7f652007-08-15 14:28:01 +0000567
568::
569
Benjamin Petersona7b55a32009-02-20 03:31:23 +0000570 import xmlrpclib, httplib
Georg Brandl8ec7f652007-08-15 14:28:01 +0000571
572 class ProxiedTransport(xmlrpclib.Transport):
573 def set_proxy(self, proxy):
574 self.proxy = proxy
575 def make_connection(self, host):
576 self.realhost = host
Georg Brandl7044b112009-01-03 21:04:55 +0000577 h = httplib.HTTP(self.proxy)
578 return h
Georg Brandl8ec7f652007-08-15 14:28:01 +0000579 def send_request(self, connection, handler, request_body):
580 connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
581 def send_host(self, connection, host):
582 connection.putheader('Host', self.realhost)
583
584 p = ProxiedTransport()
585 p.set_proxy('proxy-server:8080')
586 server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)
587 print server.currentTime.getCurrentTime()
588
Georg Brandl0a0cf162007-12-03 20:03:46 +0000589
590Example of Client and Server Usage
591----------------------------------
592
593See :ref:`simplexmlrpcserver-example`.
594
595
Sandro Tosi9b680922011-08-20 17:05:15 +0200596.. rubric:: Footnotes
597
598.. [#] This approach has been first presented in `a discussion on xmlrpc.com
599 <http://web.archive.org/web/20060624230303/http://www.xmlrpc.com/discuss/msgReader$1208?mode=topic>`_.
600.. the link now points to webarchive since the one at
601.. http://www.xmlrpc.com/discuss/msgReader%241208 is broken (and webadmin
602.. doesn't reply)