blob: 176c9290ac6eefbd75b963399fb4fd158187931b [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
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
Georg Brandl116aa622007-08-15 14:28:22 +000013XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a
14transport. With it, a client can call methods with parameters on a remote
15server (the server is named by a URI) and get back structured data. This module
16supports writing XML-RPC client code; it handles all the details of translating
17between conformable Python objects and XML on the wire.
18
19
20.. class:: ServerProxy(uri[, transport[, encoding[, verbose[, allow_none[, use_datetime]]]]])
21
22 A :class:`ServerProxy` instance is an object that manages communication with a
23 remote XML-RPC server. The required first argument is a URI (Uniform Resource
24 Indicator), and will normally be the URL of the server. The optional second
25 argument is a transport factory instance; by default it is an internal
26 :class:`SafeTransport` instance for https: URLs and an internal HTTP
27 :class:`Transport` instance otherwise. The optional third argument is an
28 encoding, by default UTF-8. The optional fourth argument is a debugging flag.
29 If *allow_none* is true, the Python constant ``None`` will be translated into
30 XML; the default behaviour is for ``None`` to raise a :exc:`TypeError`. This is
31 a commonly-used extension to the XML-RPC specification, but isn't supported by
32 all clients and servers; see http://ontosys.com/xml-rpc/extensions.php for a
33 description. The *use_datetime* flag can be used to cause date/time values to
34 be presented as :class:`datetime.datetime` objects; this is false by default.
35 :class:`datetime.datetime`, :class:`datetime.date` and :class:`datetime.time`
36 objects may be passed to calls. :class:`datetime.date` objects are converted
37 with a time of "00:00:00". :class:`datetime.time` objects are converted using
38 today's date.
39
40 Both the HTTP and HTTPS transports support the URL syntax extension for HTTP
41 Basic Authentication: ``http://user:pass@host:port/path``. The ``user:pass``
42 portion will be base64-encoded as an HTTP 'Authorization' header, and sent to
43 the remote server as part of the connection process when invoking an XML-RPC
44 method. You only need to use this if the remote server requires a Basic
45 Authentication user and password.
46
47 The returned instance is a proxy object with methods that can be used to invoke
48 corresponding RPC calls on the remote server. If the remote server supports the
49 introspection API, the proxy can also be used to query the remote server for the
50 methods it supports (service discovery) and fetch other server-associated
51 metadata.
52
53 :class:`ServerProxy` instance methods take Python basic types and objects as
54 arguments and return Python basic types and classes. Types that are conformable
55 (e.g. that can be marshalled through XML), include the following (and except
56 where noted, they are unmarshalled as the same Python type):
57
58 +---------------------------------+---------------------------------------------+
59 | Name | Meaning |
60 +=================================+=============================================+
61 | :const:`boolean` | The :const:`True` and :const:`False` |
62 | | constants |
63 +---------------------------------+---------------------------------------------+
64 | :const:`integers` | Pass in directly |
65 +---------------------------------+---------------------------------------------+
66 | :const:`floating-point numbers` | Pass in directly |
67 +---------------------------------+---------------------------------------------+
68 | :const:`strings` | Pass in directly |
69 +---------------------------------+---------------------------------------------+
70 | :const:`arrays` | Any Python sequence type containing |
71 | | conformable elements. Arrays are returned |
72 | | as lists |
73 +---------------------------------+---------------------------------------------+
74 | :const:`structures` | A Python dictionary. Keys must be strings, |
75 | | values may be any conformable type. Objects |
76 | | of user-defined classes can be passed in; |
77 | | only their *__dict__* attribute is |
78 | | transmitted. |
79 +---------------------------------+---------------------------------------------+
80 | :const:`dates` | in seconds since the epoch (pass in an |
81 | | instance of the :class:`DateTime` class) or |
82 | | a :class:`datetime.datetime`, |
83 | | :class:`datetime.date` or |
84 | | :class:`datetime.time` instance |
85 +---------------------------------+---------------------------------------------+
86 | :const:`binary data` | pass in an instance of the :class:`Binary` |
87 | | wrapper class |
88 +---------------------------------+---------------------------------------------+
89
90 This is the full set of data types supported by XML-RPC. Method calls may also
91 raise a special :exc:`Fault` instance, used to signal XML-RPC server errors, or
92 :exc:`ProtocolError` used to signal an error in the HTTP/HTTPS transport layer.
93 Both :exc:`Fault` and :exc:`ProtocolError` derive from a base class called
94 :exc:`Error`. Note that even though starting with Python 2.2 you can subclass
95 builtin types, the xmlrpclib module currently does not marshal instances of such
96 subclasses.
97
98 When passing strings, characters special to XML such as ``<``, ``>``, and ``&``
99 will be automatically escaped. However, it's the caller's responsibility to
100 ensure that the string is free of characters that aren't allowed in XML, such as
101 the control characters with ASCII values between 0 and 31 (except, of course,
102 tab, newline and carriage return); failing to do this will result in an XML-RPC
103 request that isn't well-formed XML. If you have to pass arbitrary strings via
104 XML-RPC, use the :class:`Binary` wrapper class described below.
105
106 :class:`Server` is retained as an alias for :class:`ServerProxy` for backwards
107 compatibility. New code should use :class:`ServerProxy`.
108
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110.. seealso::
111
112 `XML-RPC HOWTO <http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html>`_
113 A good description of XML operation and client software in several languages.
114 Contains pretty much everything an XML-RPC client developer needs to know.
115
116 `XML-RPC Hacks page <http://xmlrpc-c.sourceforge.net/hacks.php>`_
117 Extensions for various open-source libraries to support introspection and
118 multicall.
119
120
121.. _serverproxy-objects:
122
123ServerProxy Objects
124-------------------
125
126A :class:`ServerProxy` instance has a method corresponding to each remote
127procedure call accepted by the XML-RPC server. Calling the method performs an
128RPC, dispatched by both name and argument signature (e.g. the same method name
129can be overloaded with multiple argument signatures). The RPC finishes by
130returning a value, which may be either returned data in a conformant type or a
131:class:`Fault` or :class:`ProtocolError` object indicating an error.
132
133Servers that support the XML introspection API support some common methods
134grouped under the reserved :attr:`system` member:
135
136
137.. method:: ServerProxy.system.listMethods()
138
139 This method returns a list of strings, one for each (non-system) method
140 supported by the XML-RPC server.
141
142
143.. method:: ServerProxy.system.methodSignature(name)
144
145 This method takes one parameter, the name of a method implemented by the XML-RPC
146 server.It returns an array of possible signatures for this method. A signature
147 is an array of types. The first of these types is the return type of the method,
148 the rest are parameters.
149
150 Because multiple signatures (ie. overloading) is permitted, this method returns
151 a list of signatures rather than a singleton.
152
153 Signatures themselves are restricted to the top level parameters expected by a
154 method. For instance if a method expects one array of structs as a parameter,
155 and it returns a string, its signature is simply "string, array". If it expects
156 three integers and returns a string, its signature is "string, int, int, int".
157
158 If no signature is defined for the method, a non-array value is returned. In
159 Python this means that the type of the returned value will be something other
160 that list.
161
162
163.. method:: ServerProxy.system.methodHelp(name)
164
165 This method takes one parameter, the name of a method implemented by the XML-RPC
166 server. It returns a documentation string describing the use of that method. If
167 no such string is available, an empty string is returned. The documentation
168 string may contain HTML markup.
169
170Introspection methods are currently supported by servers written in PHP, C and
171Microsoft .NET. Partial introspection support is included in recent updates to
172UserLand Frontier. Introspection support for Perl, Python and Java is available
173at the `XML-RPC Hacks <http://xmlrpc-c.sourceforge.net/hacks.php>`_ page.
174
175
176.. _boolean-objects:
177
178Boolean Objects
179---------------
180
181This class may be initialized from any Python value; the instance returned
182depends only on its truth value. It supports various Python operators through
183:meth:`__cmp__`, :meth:`__repr__`, :meth:`__int__`, and :meth:`__bool__`
184methods, all implemented in the obvious ways.
185
186It also has the following method, supported mainly for internal use by the
187unmarshalling code:
188
189
190.. method:: Boolean.encode(out)
191
192 Write the XML-RPC encoding of this Boolean item to the out stream object.
193
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000194A working example follows. The server code::
195
196 import xmlrpclib
197 from SimpleXMLRPCServer import SimpleXMLRPCServer
198
199 def is_even(n):
200 return n%2 == 0
201
202 server = SimpleXMLRPCServer(("localhost", 8000))
203 print "Listening on port 8000..."
204 server.register_function(is_even, "is_even")
205 server.serve_forever()
206
207The client code for the preceding server::
208
209 import xmlrpclib
210
211 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
212 print "3 is even: %s" % str(proxy.is_even(3))
213 print "100 is even: %s" % str(proxy.is_even(100))
Georg Brandl116aa622007-08-15 14:28:22 +0000214
215.. _datetime-objects:
216
217DateTime Objects
218----------------
219
220This class may be initialized with seconds since the epoch, a time tuple, an ISO
2218601 time/date string, or a :class:`datetime.datetime`, :class:`datetime.date`
222or :class:`datetime.time` instance. It has the following methods, supported
223mainly for internal use by the marshalling/unmarshalling code:
224
225
226.. method:: DateTime.decode(string)
227
228 Accept a string as the instance's new time value.
229
230
231.. method:: DateTime.encode(out)
232
233 Write the XML-RPC encoding of this :class:`DateTime` item to the *out* stream
234 object.
235
236It also supports certain of Python's built-in operators through :meth:`__cmp__`
237and :meth:`__repr__` methods.
238
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000239A working example follows. The server code::
240
241 import datetime
242 from SimpleXMLRPCServer import SimpleXMLRPCServer
243 import xmlrpclib
244
245 def today():
246 today = datetime.datetime.today()
247 return xmlrpclib.DateTime(today)
248
249 server = SimpleXMLRPCServer(("localhost", 8000))
250 print "Listening on port 8000..."
251 server.register_function(today, "today")
252 server.serve_forever()
253
254The client code for the preceding server::
255
256 import xmlrpclib
257 import datetime
258
259 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
260
261 today = proxy.today()
262 # convert the ISO8601 string to a datetime object
263 converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")
264 print "Today: %s" % converted.strftime("%d.%m.%Y, %H:%M")
Georg Brandl116aa622007-08-15 14:28:22 +0000265
266.. _binary-objects:
267
268Binary Objects
269--------------
270
271This class may be initialized from string data (which may include NULs). The
272primary access to the content of a :class:`Binary` object is provided by an
273attribute:
274
275
276.. attribute:: Binary.data
277
278 The binary data encapsulated by the :class:`Binary` instance. The data is
279 provided as an 8-bit string.
280
281:class:`Binary` objects have the following methods, supported mainly for
282internal use by the marshalling/unmarshalling code:
283
284
285.. method:: Binary.decode(string)
286
287 Accept a base64 string and decode it as the instance's new data.
288
289
290.. method:: Binary.encode(out)
291
292 Write the XML-RPC base 64 encoding of this binary item to the out stream object.
293
294It also supports certain of Python's built-in operators through a
295:meth:`__cmp__` method.
296
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000297Example usage of the binary objects. We're going to transfer an image over
298XMLRPC::
299
300 from SimpleXMLRPCServer import SimpleXMLRPCServer
301 import xmlrpclib
302
303 def python_logo():
304 handle = open("python_logo.jpg")
305 return xmlrpclib.Binary(handle.read())
306 handle.close()
307
308 server = SimpleXMLRPCServer(("localhost", 8000))
309 print "Listening on port 8000..."
310 server.register_function(python_logo, 'python_logo')
311
312 server.serve_forever()
313
314The client gets the image and saves it to a file::
315
316 import xmlrpclib
317
318 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
319 handle = open("fetched_python_logo.jpg", "w")
320 handle.write(proxy.python_logo().data)
321 handle.close()
Georg Brandl116aa622007-08-15 14:28:22 +0000322
323.. _fault-objects:
324
325Fault Objects
326-------------
327
328A :class:`Fault` object encapsulates the content of an XML-RPC fault tag. Fault
329objects have the following members:
330
331
332.. attribute:: Fault.faultCode
333
334 A string indicating the fault type.
335
336
337.. attribute:: Fault.faultString
338
339 A string containing a diagnostic message associated with the fault.
340
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000341In the following example we're going to intentionally cause a :exc:`Fault` by
342returning a complex type object. The server code::
343
344 from SimpleXMLRPCServer import SimpleXMLRPCServer
345
346 # A marshalling error is going to occur because we're returning a
347 # complex number
348 def add(x,y):
349 return x+y+0j
350
351 server = SimpleXMLRPCServer(("localhost", 8000))
352 print "Listening on port 8000..."
353 server.register_function(add, 'add')
354
355 server.serve_forever()
356
357The client code for the preceding server::
358
359 import xmlrpclib
360
361 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
362 try:
363 proxy.add(2, 5)
364 except xmlrpclib.Fault, err:
365 print "A fault occured"
366 print "Fault code: %d" % err.faultCode
367 print "Fault string: %s" % err.faultString
368
369
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371.. _protocol-error-objects:
372
373ProtocolError Objects
374---------------------
375
376A :class:`ProtocolError` object describes a protocol error in the underlying
377transport layer (such as a 404 'not found' error if the server named by the URI
378does not exist). It has the following members:
379
380
381.. attribute:: ProtocolError.url
382
383 The URI or URL that triggered the error.
384
385
386.. attribute:: ProtocolError.errcode
387
388 The error code.
389
390
391.. attribute:: ProtocolError.errmsg
392
393 The error message or diagnostic string.
394
395
396.. attribute:: ProtocolError.headers
397
Guido van Rossum460add42007-08-23 02:13:35 +0000398 A dict containing the headers of the HTTP/HTTPS request that triggered the
Georg Brandl116aa622007-08-15 14:28:22 +0000399 error.
400
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000401In the following example we're going to intentionally cause a :exc:`ProtocolError`
402by providing an invalid URI::
403
404 import xmlrpclib
405
406 # create a ServerProxy with an invalid URI
407 proxy = xmlrpclib.ServerProxy("http://invalidaddress/")
408
409 try:
410 proxy.some_method()
411 except xmlrpclib.ProtocolError, err:
412 print "A protocol error occured"
413 print "URL: %s" % err.url
414 print "HTTP/HTTPS headers: %s" % err.headers
415 print "Error code: %d" % err.errcode
416 print "Error message: %s" % err.errmsg
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418MultiCall Objects
419-----------------
420
Georg Brandl116aa622007-08-15 14:28:22 +0000421In http://www.xmlrpc.com/discuss/msgReader%241208, an approach is presented to
422encapsulate multiple calls to a remote server into a single request.
423
424
425.. class:: MultiCall(server)
426
427 Create an object used to boxcar method calls. *server* is the eventual target of
428 the call. Calls can be made to the result object, but they will immediately
429 return ``None``, and only store the call name and parameters in the
430 :class:`MultiCall` object. Calling the object itself causes all stored calls to
431 be transmitted as a single ``system.multicall`` request. The result of this call
Georg Brandl9afde1c2007-11-01 20:32:30 +0000432 is a :term:`generator`; iterating over this generator yields the individual
433 results.
Georg Brandl116aa622007-08-15 14:28:22 +0000434
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000435A usage example of this class follows. The server code ::
Georg Brandl116aa622007-08-15 14:28:22 +0000436
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000437 from SimpleXMLRPCServer import SimpleXMLRPCServer
438
439 def add(x,y):
440 return x+y
441
442 def subtract(x, y):
443 return x-y
444
445 def multiply(x, y):
446 return x*y
447
448 def divide(x, y):
449 return x/y
450
451 # A simple server with simple arithmetic functions
452 server = SimpleXMLRPCServer(("localhost", 8000))
453 print "Listening on port 8000..."
454 server.register_multicall_functions()
455 server.register_function(add, 'add')
456 server.register_function(subtract, 'subtract')
457 server.register_function(multiply, 'multiply')
458 server.register_function(divide, 'divide')
459 server.serve_forever()
460
461The client code for the preceding server::
462
463 import xmlrpclib
464
465 proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
466 multicall = xmlrpclib.MultiCall(proxy)
467 multicall.add(7,3)
468 multicall.subtract(7,3)
469 multicall.multiply(7,3)
470 multicall.divide(7,3)
471 result = multicall()
472
473 print "7+3=%d, 7-3=%d, 7*3=%d, 7/3=%d" % tuple(result)
Georg Brandl116aa622007-08-15 14:28:22 +0000474
475
476Convenience Functions
477---------------------
478
479
480.. function:: boolean(value)
481
482 Convert any Python value to one of the XML-RPC Boolean constants, ``True`` or
483 ``False``.
484
485
486.. function:: dumps(params[, methodname[, methodresponse[, encoding[, allow_none]]]])
487
488 Convert *params* into an XML-RPC request. or into a response if *methodresponse*
489 is true. *params* can be either a tuple of arguments or an instance of the
490 :exc:`Fault` exception class. If *methodresponse* is true, only a single value
491 can be returned, meaning that *params* must be of length 1. *encoding*, if
492 supplied, is the encoding to use in the generated XML; the default is UTF-8.
493 Python's :const:`None` value cannot be used in standard XML-RPC; to allow using
494 it via an extension, provide a true value for *allow_none*.
495
496
497.. function:: loads(data[, use_datetime])
498
499 Convert an XML-RPC request or response into Python objects, a ``(params,
500 methodname)``. *params* is a tuple of argument; *methodname* is a string, or
501 ``None`` if no method name is present in the packet. If the XML-RPC packet
502 represents a fault condition, this function will raise a :exc:`Fault` exception.
503 The *use_datetime* flag can be used to cause date/time values to be presented as
504 :class:`datetime.datetime` objects; this is false by default. Note that even if
505 you call an XML-RPC method with :class:`datetime.date` or :class:`datetime.time`
506 objects, they are converted to :class:`DateTime` objects internally, so only
507 :class:`datetime.datetime` objects will be returned.
508
Georg Brandl116aa622007-08-15 14:28:22 +0000509
510.. _xmlrpc-client-example:
511
512Example of Client Usage
513-----------------------
514
515::
516
517 # simple test program (from the XML-RPC specification)
518 from xmlrpclib import ServerProxy, Error
519
520 # server = ServerProxy("http://localhost:8000") # local server
521 server = ServerProxy("http://betty.userland.com")
522
Collin Winterc79461b2007-09-01 23:34:30 +0000523 print(server)
Georg Brandl116aa622007-08-15 14:28:22 +0000524
525 try:
Collin Winterc79461b2007-09-01 23:34:30 +0000526 print(server.examples.getStateName(41))
Georg Brandl116aa622007-08-15 14:28:22 +0000527 except Error as v:
Collin Winterc79461b2007-09-01 23:34:30 +0000528 print("ERROR", v)
Georg Brandl116aa622007-08-15 14:28:22 +0000529
530To access an XML-RPC server through a proxy, you need to define a custom
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000531transport. The following example shows how:
Georg Brandl116aa622007-08-15 14:28:22 +0000532
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000533.. Example taken from http://lowlife.jp/nobonobo/wiki/xmlrpcwithproxy.html
Georg Brandl116aa622007-08-15 14:28:22 +0000534
535::
536
537 import xmlrpclib, httplib
538
539 class ProxiedTransport(xmlrpclib.Transport):
540 def set_proxy(self, proxy):
541 self.proxy = proxy
542 def make_connection(self, host):
543 self.realhost = host
544 h = httplib.HTTP(self.proxy)
545 return h
546 def send_request(self, connection, handler, request_body):
547 connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
548 def send_host(self, connection, host):
549 connection.putheader('Host', self.realhost)
550
551 p = ProxiedTransport()
552 p.set_proxy('proxy-server:8080')
553 server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)
Collin Winterc79461b2007-09-01 23:34:30 +0000554 print(server.currentTime.getCurrentTime())
Georg Brandl116aa622007-08-15 14:28:22 +0000555
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000556
557Example of Client and Server Usage
558----------------------------------
559
560See :ref:`simplexmlrpcserver-example`.
561
562