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