Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 1 | \section{\module{xmlrpclib} --- XML-RPC client access} |
| 2 | |
| 3 | \declaremodule{standard}{xmlrpclib} |
| 4 | \modulesynopsis{XML-RPC client access.} |
Fredrik Lundh | e7c38d4 | 2002-10-19 20:22:56 +0000 | [diff] [blame] | 5 | \moduleauthor{Fredrik Lundh}{fredrik@pythonware.com} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 6 | \sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com} |
| 7 | |
| 8 | % Not everyting is documented yet. It might be good to describe |
| 9 | % Marshaller, Unmarshaller, getparser, dumps, loads, and Transport. |
| 10 | |
| 11 | \versionadded{2.2} |
| 12 | |
| 13 | XML-RPC is a Remote Procedure Call method that uses XML passed via |
| 14 | HTTP as a transport. With it, a client can call methods with |
| 15 | parameters on a remote server (the server is named by a URI) and get back |
| 16 | structured data. This module supports writing XML-RPC client code; it |
| 17 | handles all the details of translating between conformable Python |
| 18 | objects and XML on the wire. |
| 19 | |
Skip Montanaro | dc8d407 | 2002-03-14 17:35:25 +0000 | [diff] [blame] | 20 | \begin{classdesc}{ServerProxy}{uri\optional{, transport\optional{, |
Andrew M. Kuchling | f8d0c07 | 2003-04-25 00:29:31 +0000 | [diff] [blame] | 21 | encoding\optional{, verbose\optional{, |
| 22 | allow_none}}}}} |
Skip Montanaro | dc8d407 | 2002-03-14 17:35:25 +0000 | [diff] [blame] | 23 | A \class{ServerProxy} instance is an object that manages communication |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 24 | with a remote XML-RPC server. The required first argument is a URI |
| 25 | (Uniform Resource Indicator), and will normally be the URL of the |
| 26 | server. The optional second argument is a transport factory instance; |
| 27 | by default it is an internal \class{SafeTransport} instance for https: |
| 28 | URLs and an internal HTTP \class{Transport} instance otherwise. The |
| 29 | optional third argument is an encoding, by default UTF-8. The optional |
Andrew M. Kuchling | f8d0c07 | 2003-04-25 00:29:31 +0000 | [diff] [blame] | 30 | fourth argument is a debugging flag. If \var{allow_none} is true, |
| 31 | the Python constant \code{None} will be translated into XML; the |
| 32 | default behaviour is for \code{None} to raise a \exception{TypeError}. |
| 33 | This is a commonly-used extension to the XML-RPC specification, but isn't |
| 34 | supported by all clients and servers; see |
| 35 | \url{http://ontosys.com/xml-rpc/extensions.html} for a description. |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 36 | |
Fredrik Lundh | 1303c7c | 2002-10-22 18:23:00 +0000 | [diff] [blame] | 37 | Both the HTTP and HTTPS transports support the URL syntax extension for |
Fredrik Lundh | 019bd4a | 2002-10-22 18:26:28 +0000 | [diff] [blame] | 38 | HTTP Basic Authentication: \code{http://user:pass@host:port/path}. The |
Fredrik Lundh | 1303c7c | 2002-10-22 18:23:00 +0000 | [diff] [blame] | 39 | \code{user:pass} portion will be base64-encoded as an HTTP `Authorization' |
| 40 | header, and sent to the remote server as part of the connection process |
| 41 | when invoking an XML-RPC method. You only need to use this if the |
| 42 | remote server requires a Basic Authentication user and password. |
| 43 | |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 44 | The returned instance is a proxy object with methods that can be used |
| 45 | to invoke corresponding RPC calls on the remote server. If the remote |
| 46 | server supports the introspection API, the proxy can also be used to query |
| 47 | the remote server for the methods it supports (service discovery) and |
| 48 | fetch other server-associated metadata. |
| 49 | |
Skip Montanaro | dc8d407 | 2002-03-14 17:35:25 +0000 | [diff] [blame] | 50 | \class{ServerProxy} instance methods take Python basic types and objects as |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 51 | arguments and return Python basic types and classes. Types that are |
| 52 | conformable (e.g. that can be marshalled through XML), include the |
| 53 | following (and except where noted, they are unmarshalled as the same |
| 54 | Python type): |
| 55 | |
| 56 | \begin{tableii}{l|l}{constant}{Name}{Meaning} |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 57 | \lineii{boolean}{The \constant{True} and \constant{False} constants} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 58 | \lineii{integers}{Pass in directly} |
| 59 | \lineii{floating-point numbers}{Pass in directly} |
| 60 | \lineii{strings}{Pass in directly} |
| 61 | \lineii{arrays}{Any Python sequence type containing conformable |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 62 | elements. Arrays are returned as lists} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 63 | \lineii{structures}{A Python dictionary. Keys must be strings, |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 64 | values may be any conformable type.} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 65 | \lineii{dates}{in seconds since the epoch; pass in an instance of the |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 66 | \class{DateTime} wrapper class} |
| 67 | \lineii{binary data}{pass in an instance of the \class{Binary} |
| 68 | wrapper class} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 69 | \end{tableii} |
| 70 | |
| 71 | This is the full set of data types supported by XML-RPC. Method calls |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 72 | may also raise a special \exception{Fault} instance, used to signal |
| 73 | XML-RPC server errors, or \exception{ProtocolError} used to signal an |
Skip Montanaro | 10acc8f | 2002-03-17 23:15:02 +0000 | [diff] [blame] | 74 | error in the HTTP/HTTPS transport layer. Note that even though starting |
| 75 | with Python 2.2 you can subclass builtin types, the xmlrpclib module |
| 76 | currently does not marshal instances of such subclasses. |
Andrew M. Kuchling | 10b3eac | 2002-03-08 17:46:02 +0000 | [diff] [blame] | 77 | |
| 78 | When passing strings, characters special to XML such as \samp{<}, |
| 79 | \samp{>}, and \samp{\&} will be automatically escaped. However, it's |
| 80 | the caller's responsibility to ensure that the string is free of |
| 81 | characters that aren't allowed in XML, such as the control characters |
| 82 | with ASCII values between 0 and 31; failing to do this will result in |
| 83 | an XML-RPC request that isn't well-formed XML. If you have to pass |
| 84 | arbitrary strings via XML-RPC, use the \class{Binary} wrapper class |
| 85 | described below. |
| 86 | |
Skip Montanaro | dc8d407 | 2002-03-14 17:35:25 +0000 | [diff] [blame] | 87 | \class{Server} is retained as an alias for \class{ServerProxy} for backwards |
| 88 | compatibility. New code should use \class{ServerProxy}. |
| 89 | |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 90 | \end{classdesc} |
| 91 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 92 | |
| 93 | \begin{seealso} |
| 94 | \seetitle[http://xmlrpc-c.sourceforge.net/xmlrpc-howto/xmlrpc-howto.html] |
| 95 | {XML-RPC HOWTO}{A good description of XML operation and |
| 96 | client software in several languages. Contains pretty much |
| 97 | everything an XML-RPC client developer needs to know.} |
| 98 | \seetitle[http://xmlrpc-c.sourceforge.net/hacks.php] |
| 99 | {XML-RPC-Hacks page}{Extensions for various open-source |
| 100 | libraries to support instrospection and multicall.} |
| 101 | \end{seealso} |
| 102 | |
| 103 | |
Skip Montanaro | dc8d407 | 2002-03-14 17:35:25 +0000 | [diff] [blame] | 104 | \subsection{ServerProxy Objects \label{serverproxy-objects}} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 105 | |
Skip Montanaro | dc8d407 | 2002-03-14 17:35:25 +0000 | [diff] [blame] | 106 | A \class{ServerProxy} instance has a method corresponding to |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 107 | each remote procedure call accepted by the XML-RPC server. Calling |
| 108 | the method performs an RPC, dispatched by both name and argument |
| 109 | signature (e.g. the same method name can be overloaded with multiple |
| 110 | argument signatures). The RPC finishes by returning a value, which |
| 111 | may be either returned data in a conformant type or a \class{Fault} or |
| 112 | \class{ProtocolError} object indicating an error. |
| 113 | |
| 114 | Servers that support the XML introspection API support some common |
| 115 | methods grouped under the reserved \member{system} member: |
| 116 | |
| 117 | \begin{methoddesc}{system.listMethods}{} |
| 118 | This method returns a list of strings, one for each (non-system) |
| 119 | method supported by the XML-RPC server. |
| 120 | \end{methoddesc} |
| 121 | |
Fred Drake | 4124a0b | 2001-07-14 02:46:01 +0000 | [diff] [blame] | 122 | \begin{methoddesc}{system.methodSignature}{name} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 123 | This method takes one parameter, the name of a method implemented by |
| 124 | the XML-RPC server.It returns an array of possible signatures for this |
| 125 | method. A signature is an array of types. The first of these types is |
| 126 | the return type of the method, the rest are parameters. |
| 127 | |
| 128 | Because multiple signatures (ie. overloading) is permitted, this method |
| 129 | returns a list of signatures rather than a singleton. |
| 130 | |
| 131 | Signatures themselves are restricted to the top level parameters |
| 132 | expected by a method. For instance if a method expects one array of |
| 133 | structs as a parameter, and it returns a string, its signature is |
| 134 | simply "string, array". If it expects three integers and returns a |
| 135 | string, its signature is "string, int, int, int". |
| 136 | |
| 137 | If no signature is defined for the method, a non-array value is |
| 138 | returned. In Python this means that the type of the returned |
| 139 | value will be something other that list. |
| 140 | \end{methoddesc} |
| 141 | |
| 142 | \begin{methoddesc}{system.methodHelp}{name} |
| 143 | This method takes one parameter, the name of a method implemented by |
| 144 | the XML-RPC server. It returns a documentation string describing the |
| 145 | use of that method. If no such string is available, an empty string is |
| 146 | returned. The documentation string may contain HTML markup. |
| 147 | \end{methoddesc} |
| 148 | |
| 149 | Introspection methods are currently supported by servers written in |
| 150 | PHP, C and Microsoft .NET. Partial introspection support is included |
| 151 | in recent updates to UserLand Frontier. Introspection support for |
| 152 | Perl, Python and Java is available at the XML-RPC Hacks page. |
| 153 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 154 | |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 155 | \subsection{Boolean Objects \label{boolean-objects}} |
| 156 | |
| 157 | This class may be initialized from any Python value; the instance |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 158 | returned depends only on its truth value. It supports various Python |
| 159 | operators through \method{__cmp__()}, \method{__repr__()}, |
| 160 | \method{__int__()}, and \method{__nonzero__()} methods, all |
| 161 | implemented in the obvious ways. |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 162 | |
| 163 | It also has the following method, supported mainly for internal use by |
| 164 | the unmarshalling code: |
| 165 | |
| 166 | \begin{methoddesc}{encode}{out} |
| 167 | Write the XML-RPC encoding of this Boolean item to the out stream object. |
| 168 | \end{methoddesc} |
| 169 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 170 | |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 171 | \subsection{DateTime Objects \label{datetime-objects}} |
| 172 | |
| 173 | This class may initialized from date in seconds since the epoch, a |
| 174 | time tuple, or an ISO 8601 time/date string. It has the following |
| 175 | methods, supported mainly for internal use by the |
| 176 | marshalling/unmarshalling code: |
| 177 | |
| 178 | \begin{methoddesc}{decode}{string} |
| 179 | Accept a string as the instance's new time value. |
| 180 | \end{methoddesc} |
| 181 | |
| 182 | \begin{methoddesc}{encode}{out} |
| 183 | Write the XML-RPC encoding of this DateTime item to the out stream object. |
| 184 | \end{methoddesc} |
| 185 | |
| 186 | It also supports certain of Python's built-in operators through |
| 187 | \method{_cmp__} and \method{__repr__} methods. |
| 188 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 189 | |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 190 | \subsection{Binary Objects \label{binary-objects}} |
| 191 | |
| 192 | This class may initialized from string data (which may include NULs). |
Fred Drake | 585775b | 2002-06-14 00:33:02 +0000 | [diff] [blame] | 193 | The primary acess to the content of a \class{Binary} object is |
| 194 | provided by an attribute: |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 195 | |
Fred Drake | 585775b | 2002-06-14 00:33:02 +0000 | [diff] [blame] | 196 | \begin{memberdesc}[Binary]{data} |
| 197 | The binary data encapsulated by the \class{Binary} instance. The data |
| 198 | is provided as an 8-bit string. |
| 199 | \end{memberdesc} |
| 200 | |
| 201 | \class{Binary} objects have the following methods, supported mainly |
| 202 | for internal use by the marshalling/unmarshalling code: |
| 203 | |
| 204 | \begin{methoddesc}[Binary]{decode}{string} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 205 | Accept a base64 string and decode it as the instance's new data. |
| 206 | \end{methoddesc} |
| 207 | |
Fred Drake | 585775b | 2002-06-14 00:33:02 +0000 | [diff] [blame] | 208 | \begin{methoddesc}[Binary]{encode}{out} |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 209 | Write the XML-RPC base 64 encoding of this binary item to the out |
| 210 | stream object. |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 211 | \end{methoddesc} |
| 212 | |
| 213 | It also supports certain of Python's built-in operators through a |
Fred Drake | 585775b | 2002-06-14 00:33:02 +0000 | [diff] [blame] | 214 | \method{_cmp__()} method. |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 215 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 216 | |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 217 | \subsection{Fault Objects \label{fault-objects}} |
| 218 | |
| 219 | A \class{Fault} object encapsulates the content of an XML-RPC fault tag. |
| 220 | Fault objects have the following members: |
| 221 | |
| 222 | \begin{memberdesc}{faultCode} |
| 223 | A string indicating the fault type. |
| 224 | \end{memberdesc} |
| 225 | |
| 226 | \begin{memberdesc}{faultString} |
| 227 | A string containing a diagnostic message associated with the fault. |
| 228 | \end{memberdesc} |
| 229 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 230 | |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 231 | \subsection{ProtocolError Objects \label{protocol-error-objects}} |
| 232 | |
| 233 | A \class{ProtocolError} object describes a protocol error in the |
| 234 | underlying transport layer (such as a 404 `not found' error if the |
| 235 | server named by the URI does not exist). It has the following |
| 236 | members: |
| 237 | |
| 238 | \begin{memberdesc}{url} |
Andrew M. Kuchling | 10b3eac | 2002-03-08 17:46:02 +0000 | [diff] [blame] | 239 | The URI or URL that triggered the error. |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 240 | \end{memberdesc} |
| 241 | |
| 242 | \begin{memberdesc}{errcode} |
| 243 | The error code. |
| 244 | \end{memberdesc} |
| 245 | |
| 246 | \begin{memberdesc}{errmsg} |
Andrew M. Kuchling | 10b3eac | 2002-03-08 17:46:02 +0000 | [diff] [blame] | 247 | The error message or diagnostic string. |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 248 | \end{memberdesc} |
| 249 | |
| 250 | \begin{memberdesc}{headers} |
| 251 | A string containing the headers of the HTTP/HTTPS request that |
| 252 | triggered the error. |
| 253 | \end{memberdesc} |
| 254 | |
Martin v. Löwis | 45394c2 | 2003-10-31 13:49:36 +0000 | [diff] [blame] | 255 | \subsection{MultiCall Objects} |
| 256 | |
| 257 | \versionadded{2.4} |
| 258 | |
| 259 | In \url{http://www.xmlrpc.com/discuss/msgReader\$1208}, an approach |
| 260 | is presented to encapsulate multiple calls to a remote server into |
| 261 | a single request. |
| 262 | |
| 263 | \begin{classdesc}{MultiCall}{server} |
| 264 | |
| 265 | Create an object used to boxcar method calls. \var{server} is the |
| 266 | eventual target of the call. Calls can be made to the result object, |
| 267 | but they will immediately return \var{None}, and only store the |
| 268 | call name and parameters in the \class{MultiCall} object. Calling |
| 269 | the object itself causes all stored calls to be transmitted as |
| 270 | a single \code{system.multicall} request. The result of this call |
| 271 | is a generator; iterating over this generator yields the individual |
| 272 | results. |
| 273 | |
| 274 | \end{classdesc} |
| 275 | |
| 276 | A usage example of this class is |
| 277 | |
| 278 | \begin{verbatim} |
| 279 | multicall = MultiCall(server_proxy) |
| 280 | multicall.add(2,3) |
| 281 | multicall.get_address("Guido") |
| 282 | add_result, address = multicall() |
| 283 | \end{verbatim} |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 284 | |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 285 | \subsection{Convenience Functions} |
| 286 | |
Fred Drake | d90f509 | 2001-10-01 21:05:30 +0000 | [diff] [blame] | 287 | \begin{funcdesc}{boolean}{value} |
| 288 | Convert any Python value to one of the XML-RPC Boolean constants, |
| 289 | \code{True} or \code{False}. |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 290 | \end{funcdesc} |
| 291 | |
| 292 | \begin{funcdesc}{binary}{data} |
| 293 | Trivially convert any Python string to a \class{Binary} object. |
| 294 | \end{funcdesc} |
| 295 | |
Andrew M. Kuchling | 38afcef | 2003-10-22 14:12:03 +0000 | [diff] [blame] | 296 | \begin{funcdesc}{dumps}{params\optional{, methodname\optional{, |
| 297 | methodresponse\optional{, encoding\optional{, |
| 298 | allow_none}}}}} |
| 299 | |
| 300 | Convert \var{params} into an XML-RPC request. |
| 301 | or into a response if \var{methodresponse} is true. |
| 302 | \var{params} can be either a tuple of arguments or an instance of the |
| 303 | \exception{Fault} exception class. If \var{methodresponse} is true, |
| 304 | only a single value can be returned, meaning that \var{params} must be of length 1. |
| 305 | \var{encoding}, if supplied, is the encoding to use in the generated |
| 306 | XML; the default is UTF-8. Python's \constant{None} value cannot be |
| 307 | used in standard XML-RPC; to allow using it via an extension, |
| 308 | provide a true value for \var{allow_none}. |
| 309 | \end{funcdesc} |
| 310 | |
| 311 | \begin{funcdesc}{loads}{data} |
| 312 | Convert an XML-RPC request or response into Python objects, a |
| 313 | \code{(\var{params}, \var{methodname})}. \var{params} is a tuple of argument; \var{methodname} |
| 314 | is a string, or \code{None} if no method name is present in the packet. |
| 315 | If the XML-RPC packet represents a fault condition, this |
| 316 | function will raise a \exception{Fault} exception. |
| 317 | \end{funcdesc} |
| 318 | |
| 319 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 320 | |
| 321 | \subsection{Example of Client Usage \label{xmlrpc-client-example}} |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 322 | |
| 323 | \begin{verbatim} |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 324 | # simple test program (from the XML-RPC specification) |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 325 | |
Skip Montanaro | dc8d407 | 2002-03-14 17:35:25 +0000 | [diff] [blame] | 326 | # server = ServerProxy("http://localhost:8000") # local server |
| 327 | server = ServerProxy("http://betty.userland.com") |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 328 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 329 | print server |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 330 | |
Fred Drake | 5ddf7ad | 2001-07-12 23:39:24 +0000 | [diff] [blame] | 331 | try: |
| 332 | print server.examples.getStateName(41) |
| 333 | except Error, v: |
| 334 | print "ERROR", v |
Eric S. Raymond | e304bb9 | 2001-07-12 02:39:45 +0000 | [diff] [blame] | 335 | \end{verbatim} |