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