blob: 957a25b46b2d0cb95c3814fa7887423a6c6f0b6a [file] [log] [blame]
Eric S. Raymonde304bb92001-07-12 02:39:45 +00001\section{\module{xmlrpclib} --- XML-RPC client access}
2
3\declaremodule{standard}{xmlrpclib}
4\modulesynopsis{XML-RPC client access.}
Fredrik Lundhe7c38d42002-10-19 20:22:56 +00005\moduleauthor{Fredrik Lundh}{fredrik@pythonware.com}
Eric S. Raymonde304bb92001-07-12 02:39:45 +00006\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
13XML-RPC is a Remote Procedure Call method that uses XML passed via
14HTTP as a transport. With it, a client can call methods with
15parameters on a remote server (the server is named by a URI) and get back
16structured data. This module supports writing XML-RPC client code; it
17handles all the details of translating between conformable Python
18objects and XML on the wire.
19
Skip Montanarodc8d4072002-03-14 17:35:25 +000020\begin{classdesc}{ServerProxy}{uri\optional{, transport\optional{,
Andrew M. Kuchlingf8d0c072003-04-25 00:29:31 +000021 encoding\optional{, verbose\optional{,
22 allow_none}}}}}
Skip Montanarodc8d4072002-03-14 17:35:25 +000023A \class{ServerProxy} instance is an object that manages communication
Eric S. Raymonde304bb92001-07-12 02:39:45 +000024with a remote XML-RPC server. The required first argument is a URI
25(Uniform Resource Indicator), and will normally be the URL of the
26server. The optional second argument is a transport factory instance;
27by default it is an internal \class{SafeTransport} instance for https:
28URLs and an internal HTTP \class{Transport} instance otherwise. The
29optional third argument is an encoding, by default UTF-8. The optional
Andrew M. Kuchlingf8d0c072003-04-25 00:29:31 +000030fourth argument is a debugging flag. If \var{allow_none} is true,
31the Python constant \code{None} will be translated into XML; the
32default behaviour is for \code{None} to raise a \exception{TypeError}.
33This is a commonly-used extension to the XML-RPC specification, but isn't
34supported by all clients and servers; see
35\url{http://ontosys.com/xml-rpc/extensions.html} for a description.
Eric S. Raymonde304bb92001-07-12 02:39:45 +000036
Fredrik Lundh1303c7c2002-10-22 18:23:00 +000037Both the HTTP and HTTPS transports support the URL syntax extension for
Fredrik Lundh019bd4a2002-10-22 18:26:28 +000038HTTP Basic Authentication: \code{http://user:pass@host:port/path}. The
Fredrik Lundh1303c7c2002-10-22 18:23:00 +000039\code{user:pass} portion will be base64-encoded as an HTTP `Authorization'
40header, and sent to the remote server as part of the connection process
41when invoking an XML-RPC method. You only need to use this if the
42remote server requires a Basic Authentication user and password.
43
Eric S. Raymonde304bb92001-07-12 02:39:45 +000044The returned instance is a proxy object with methods that can be used
45to invoke corresponding RPC calls on the remote server. If the remote
46server supports the introspection API, the proxy can also be used to query
47the remote server for the methods it supports (service discovery) and
48fetch other server-associated metadata.
49
Skip Montanarodc8d4072002-03-14 17:35:25 +000050\class{ServerProxy} instance methods take Python basic types and objects as
Eric S. Raymonde304bb92001-07-12 02:39:45 +000051arguments and return Python basic types and classes. Types that are
52conformable (e.g. that can be marshalled through XML), include the
53following (and except where noted, they are unmarshalled as the same
54Python type):
55
56\begin{tableii}{l|l}{constant}{Name}{Meaning}
Fred Drake5ddf7ad2001-07-12 23:39:24 +000057 \lineii{boolean}{The \constant{True} and \constant{False} constants}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000058 \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 Drake5ddf7ad2001-07-12 23:39:24 +000062 elements. Arrays are returned as lists}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000063 \lineii{structures}{A Python dictionary. Keys must be strings,
Fred Drake5ddf7ad2001-07-12 23:39:24 +000064 values may be any conformable type.}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000065 \lineii{dates}{in seconds since the epoch; pass in an instance of the
Fred Drake5ddf7ad2001-07-12 23:39:24 +000066 \class{DateTime} wrapper class}
67 \lineii{binary data}{pass in an instance of the \class{Binary}
68 wrapper class}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000069\end{tableii}
70
71This is the full set of data types supported by XML-RPC. Method calls
Fred Drake5ddf7ad2001-07-12 23:39:24 +000072may also raise a special \exception{Fault} instance, used to signal
73XML-RPC server errors, or \exception{ProtocolError} used to signal an
Skip Montanaro10acc8f2002-03-17 23:15:02 +000074error in the HTTP/HTTPS transport layer. Note that even though starting
75with Python 2.2 you can subclass builtin types, the xmlrpclib module
76currently does not marshal instances of such subclasses.
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +000077
78When passing strings, characters special to XML such as \samp{<},
79\samp{>}, and \samp{\&} will be automatically escaped. However, it's
80the caller's responsibility to ensure that the string is free of
81characters that aren't allowed in XML, such as the control characters
82with ASCII values between 0 and 31; failing to do this will result in
83an XML-RPC request that isn't well-formed XML. If you have to pass
84arbitrary strings via XML-RPC, use the \class{Binary} wrapper class
85described below.
86
Skip Montanarodc8d4072002-03-14 17:35:25 +000087\class{Server} is retained as an alias for \class{ServerProxy} for backwards
88compatibility. New code should use \class{ServerProxy}.
89
Eric S. Raymonde304bb92001-07-12 02:39:45 +000090\end{classdesc}
91
Fred Drake5ddf7ad2001-07-12 23:39:24 +000092
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 Montanarodc8d4072002-03-14 17:35:25 +0000104\subsection{ServerProxy Objects \label{serverproxy-objects}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000105
Skip Montanarodc8d4072002-03-14 17:35:25 +0000106A \class{ServerProxy} instance has a method corresponding to
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000107each remote procedure call accepted by the XML-RPC server. Calling
108the method performs an RPC, dispatched by both name and argument
109signature (e.g. the same method name can be overloaded with multiple
110argument signatures). The RPC finishes by returning a value, which
111may be either returned data in a conformant type or a \class{Fault} or
112\class{ProtocolError} object indicating an error.
113
114Servers that support the XML introspection API support some common
115methods grouped under the reserved \member{system} member:
116
117\begin{methoddesc}{system.listMethods}{}
118This method returns a list of strings, one for each (non-system)
119method supported by the XML-RPC server.
120\end{methoddesc}
121
Fred Drake4124a0b2001-07-14 02:46:01 +0000122\begin{methoddesc}{system.methodSignature}{name}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000123This method takes one parameter, the name of a method implemented by
124the XML-RPC server.It returns an array of possible signatures for this
125method. A signature is an array of types. The first of these types is
126the return type of the method, the rest are parameters.
127
128Because multiple signatures (ie. overloading) is permitted, this method
129returns a list of signatures rather than a singleton.
130
131Signatures themselves are restricted to the top level parameters
132expected by a method. For instance if a method expects one array of
133structs as a parameter, and it returns a string, its signature is
134simply "string, array". If it expects three integers and returns a
135string, its signature is "string, int, int, int".
136
137If no signature is defined for the method, a non-array value is
138returned. In Python this means that the type of the returned
139value will be something other that list.
140\end{methoddesc}
141
142\begin{methoddesc}{system.methodHelp}{name}
143This method takes one parameter, the name of a method implemented by
144the XML-RPC server. It returns a documentation string describing the
145use of that method. If no such string is available, an empty string is
146returned. The documentation string may contain HTML markup.
147\end{methoddesc}
148
149Introspection methods are currently supported by servers written in
150PHP, C and Microsoft .NET. Partial introspection support is included
151in recent updates to UserLand Frontier. Introspection support for
152Perl, Python and Java is available at the XML-RPC Hacks page.
153
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000154
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000155\subsection{Boolean Objects \label{boolean-objects}}
156
157This class may be initialized from any Python value; the instance
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000158returned depends only on its truth value. It supports various Python
159operators through \method{__cmp__()}, \method{__repr__()},
160\method{__int__()}, and \method{__nonzero__()} methods, all
161implemented in the obvious ways.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000162
163It also has the following method, supported mainly for internal use by
164the unmarshalling code:
165
166\begin{methoddesc}{encode}{out}
167Write the XML-RPC encoding of this Boolean item to the out stream object.
168\end{methoddesc}
169
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000170
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000171\subsection{DateTime Objects \label{datetime-objects}}
172
173This class may initialized from date in seconds since the epoch, a
174time tuple, or an ISO 8601 time/date string. It has the following
175methods, supported mainly for internal use by the
176marshalling/unmarshalling code:
177
178\begin{methoddesc}{decode}{string}
179Accept a string as the instance's new time value.
180\end{methoddesc}
181
182\begin{methoddesc}{encode}{out}
183Write the XML-RPC encoding of this DateTime item to the out stream object.
184\end{methoddesc}
185
186It also supports certain of Python's built-in operators through
187\method{_cmp__} and \method{__repr__} methods.
188
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000189
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000190\subsection{Binary Objects \label{binary-objects}}
191
192This class may initialized from string data (which may include NULs).
Fred Drakebb066cf2004-05-12 03:07:27 +0000193The primary access to the content of a \class{Binary} object is
Fred Drake585775b2002-06-14 00:33:02 +0000194provided by an attribute:
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000195
Fred Drake585775b2002-06-14 00:33:02 +0000196\begin{memberdesc}[Binary]{data}
197The binary data encapsulated by the \class{Binary} instance. The data
198is provided as an 8-bit string.
199\end{memberdesc}
200
201\class{Binary} objects have the following methods, supported mainly
202for internal use by the marshalling/unmarshalling code:
203
204\begin{methoddesc}[Binary]{decode}{string}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000205Accept a base64 string and decode it as the instance's new data.
206\end{methoddesc}
207
Fred Drake585775b2002-06-14 00:33:02 +0000208\begin{methoddesc}[Binary]{encode}{out}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000209Write the XML-RPC base 64 encoding of this binary item to the out
210stream object.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000211\end{methoddesc}
212
213It also supports certain of Python's built-in operators through a
Fred Drake585775b2002-06-14 00:33:02 +0000214\method{_cmp__()} method.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000215
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000216
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000217\subsection{Fault Objects \label{fault-objects}}
218
219A \class{Fault} object encapsulates the content of an XML-RPC fault tag.
220Fault objects have the following members:
221
222\begin{memberdesc}{faultCode}
223A string indicating the fault type.
224\end{memberdesc}
225
226\begin{memberdesc}{faultString}
227A string containing a diagnostic message associated with the fault.
228\end{memberdesc}
229
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000230
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000231\subsection{ProtocolError Objects \label{protocol-error-objects}}
232
233A \class{ProtocolError} object describes a protocol error in the
234underlying transport layer (such as a 404 `not found' error if the
235server named by the URI does not exist). It has the following
236members:
237
238\begin{memberdesc}{url}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000239The URI or URL that triggered the error.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000240\end{memberdesc}
241
242\begin{memberdesc}{errcode}
243The error code.
244\end{memberdesc}
245
246\begin{memberdesc}{errmsg}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000247The error message or diagnostic string.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000248\end{memberdesc}
249
250\begin{memberdesc}{headers}
251A string containing the headers of the HTTP/HTTPS request that
252triggered the error.
253\end{memberdesc}
254
Martin v. Löwis45394c22003-10-31 13:49:36 +0000255\subsection{MultiCall Objects}
256
257\versionadded{2.4}
258
259In \url{http://www.xmlrpc.com/discuss/msgReader\$1208}, an approach
260is presented to encapsulate multiple calls to a remote server into
261a single request.
262
263\begin{classdesc}{MultiCall}{server}
264
265Create an object used to boxcar method calls. \var{server} is the
266eventual target of the call. Calls can be made to the result object,
267but they will immediately return \var{None}, and only store the
268call name and parameters in the \class{MultiCall} object. Calling
269the object itself causes all stored calls to be transmitted as
270a single \code{system.multicall} request. The result of this call
271is a generator; iterating over this generator yields the individual
272results.
273
274\end{classdesc}
275
276A usage example of this class is
277
278\begin{verbatim}
279multicall = MultiCall(server_proxy)
280multicall.add(2,3)
281multicall.get_address("Guido")
282add_result, address = multicall()
283\end{verbatim}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000284
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000285\subsection{Convenience Functions}
286
Fred Draked90f5092001-10-01 21:05:30 +0000287\begin{funcdesc}{boolean}{value}
288Convert any Python value to one of the XML-RPC Boolean constants,
289\code{True} or \code{False}.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000290\end{funcdesc}
291
292\begin{funcdesc}{binary}{data}
293Trivially convert any Python string to a \class{Binary} object.
294\end{funcdesc}
295
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000296\begin{funcdesc}{dumps}{params\optional{, methodname\optional{,
297 methodresponse\optional{, encoding\optional{,
298 allow_none}}}}}
299
300Convert \var{params} into an XML-RPC request.
301or 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,
304only 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
306XML; the default is UTF-8. Python's \constant{None} value cannot be
307used in standard XML-RPC; to allow using it via an extension,
308provide a true value for \var{allow_none}.
309\end{funcdesc}
310
311\begin{funcdesc}{loads}{data}
312Convert 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}
314is a string, or \code{None} if no method name is present in the packet.
315If the XML-RPC packet represents a fault condition, this
316function will raise a \exception{Fault} exception.
317\end{funcdesc}
318
319
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000320
321\subsection{Example of Client Usage \label{xmlrpc-client-example}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000322
323\begin{verbatim}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000324# simple test program (from the XML-RPC specification)
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000325
Skip Montanarodc8d4072002-03-14 17:35:25 +0000326# server = ServerProxy("http://localhost:8000") # local server
327server = ServerProxy("http://betty.userland.com")
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000328
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000329print server
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000330
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000331try:
332 print server.examples.getStateName(41)
333except Error, v:
334 print "ERROR", v
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000335\end{verbatim}