blob: ab9ef97b3acdbb9feafbbc9382248c80f53eb83a [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
Fred Drakeb184ae82005-01-19 03:39:17 +00008% Not everything is documented yet. It might be good to describe
Eric S. Raymonde304bb92001-07-12 02:39:45 +00009% 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{,
Skip Montanaro174dd222005-05-14 20:54:16 +000022 allow_none\optional{, use_datetime}}}}}}
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
Skip Montanaro3f8f6662005-03-21 19:39:16 +000035\url{http://ontosys.com/xml-rpc/extensions.php} for a description.
Skip Montanaro174dd222005-05-14 20:54:16 +000036The \var{use_datetime} flag can be used to cause date/time values to be
37presented as \class{\refmodule{datetime}.datetime} objects; this is false
38by default. \class{\refmodule{datetime}.datetime},
39\class{\refmodule{datetime}.date} and \class{\refmodule{datetime}.time}
40objects may be passed to calls. \class{\refmodule{datetime}.date} objects
41are converted with a time of ``00:00:00''.
42\class{\refmodule{datetime}.time} objects are converted using today's date.
Eric S. Raymonde304bb92001-07-12 02:39:45 +000043
Fredrik Lundh1303c7c2002-10-22 18:23:00 +000044Both the HTTP and HTTPS transports support the URL syntax extension for
Fredrik Lundh019bd4a2002-10-22 18:26:28 +000045HTTP Basic Authentication: \code{http://user:pass@host:port/path}. The
Fredrik Lundh1303c7c2002-10-22 18:23:00 +000046\code{user:pass} portion will be base64-encoded as an HTTP `Authorization'
47header, and sent to the remote server as part of the connection process
48when invoking an XML-RPC method. You only need to use this if the
49remote server requires a Basic Authentication user and password.
50
Eric S. Raymonde304bb92001-07-12 02:39:45 +000051The returned instance is a proxy object with methods that can be used
52to invoke corresponding RPC calls on the remote server. If the remote
53server supports the introspection API, the proxy can also be used to query
54the remote server for the methods it supports (service discovery) and
55fetch other server-associated metadata.
56
Skip Montanarodc8d4072002-03-14 17:35:25 +000057\class{ServerProxy} instance methods take Python basic types and objects as
Eric S. Raymonde304bb92001-07-12 02:39:45 +000058arguments and return Python basic types and classes. Types that are
59conformable (e.g. that can be marshalled through XML), include the
60following (and except where noted, they are unmarshalled as the same
61Python type):
62
63\begin{tableii}{l|l}{constant}{Name}{Meaning}
Fred Drake5ddf7ad2001-07-12 23:39:24 +000064 \lineii{boolean}{The \constant{True} and \constant{False} constants}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000065 \lineii{integers}{Pass in directly}
66 \lineii{floating-point numbers}{Pass in directly}
67 \lineii{strings}{Pass in directly}
68 \lineii{arrays}{Any Python sequence type containing conformable
Fred Drake5ddf7ad2001-07-12 23:39:24 +000069 elements. Arrays are returned as lists}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000070 \lineii{structures}{A Python dictionary. Keys must be strings,
Fred Drake5ddf7ad2001-07-12 23:39:24 +000071 values may be any conformable type.}
Skip Montanaro174dd222005-05-14 20:54:16 +000072 \lineii{dates}{in seconds since the epoch (pass in an instance of the
73 \class{DateTime} class) or a
74 \class{\refmodule{datetime}.datetime},
75 \class{\refmodule{datetime}.date} or
76 \class{\refmodule{datetime}.time} instance}
Fred Drake5ddf7ad2001-07-12 23:39:24 +000077 \lineii{binary data}{pass in an instance of the \class{Binary}
78 wrapper class}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000079\end{tableii}
80
81This is the full set of data types supported by XML-RPC. Method calls
Fred Drake5ddf7ad2001-07-12 23:39:24 +000082may also raise a special \exception{Fault} instance, used to signal
83XML-RPC server errors, or \exception{ProtocolError} used to signal an
Andrew M. Kuchlingefd3a092006-04-27 12:42:54 +000084error in the HTTP/HTTPS transport layer. Both \exception{Fault} and
85\exception{ProtocolError} derive from a base class called
86\exception{Error}. Note that even though starting with Python 2.2 you
87can subclass builtin types, the xmlrpclib module currently does not
88marshal instances of such subclasses.
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +000089
90When passing strings, characters special to XML such as \samp{<},
91\samp{>}, and \samp{\&} will be automatically escaped. However, it's
92the caller's responsibility to ensure that the string is free of
93characters that aren't allowed in XML, such as the control characters
Georg Brandl77dff902007-07-26 09:36:28 +000094with ASCII values between 0 and 31 (except, of course, tab, newline and
95carriage return); failing to do this will result in
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +000096an XML-RPC request that isn't well-formed XML. If you have to pass
97arbitrary strings via XML-RPC, use the \class{Binary} wrapper class
98described below.
99
Skip Montanarodc8d4072002-03-14 17:35:25 +0000100\class{Server} is retained as an alias for \class{ServerProxy} for backwards
101compatibility. New code should use \class{ServerProxy}.
102
Skip Montanaro174dd222005-05-14 20:54:16 +0000103\versionchanged[The \var{use_datetime} flag was added]{2.5}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000104\end{classdesc}
105
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000106
107\begin{seealso}
Fred Drake3f207b62005-11-29 12:40:58 +0000108 \seetitle[http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html]
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000109 {XML-RPC HOWTO}{A good description of XML operation and
110 client software in several languages. Contains pretty much
111 everything an XML-RPC client developer needs to know.}
112 \seetitle[http://xmlrpc-c.sourceforge.net/hacks.php]
Skip Montanaro174dd222005-05-14 20:54:16 +0000113 {XML-RPC Hacks page}{Extensions for various open-source
Raymond Hettinger68804312005-01-01 00:28:46 +0000114 libraries to support introspection and multicall.}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000115\end{seealso}
116
117
Skip Montanarodc8d4072002-03-14 17:35:25 +0000118\subsection{ServerProxy Objects \label{serverproxy-objects}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000119
Skip Montanarodc8d4072002-03-14 17:35:25 +0000120A \class{ServerProxy} instance has a method corresponding to
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000121each remote procedure call accepted by the XML-RPC server. Calling
122the method performs an RPC, dispatched by both name and argument
123signature (e.g. the same method name can be overloaded with multiple
124argument signatures). The RPC finishes by returning a value, which
125may be either returned data in a conformant type or a \class{Fault} or
126\class{ProtocolError} object indicating an error.
127
128Servers that support the XML introspection API support some common
129methods grouped under the reserved \member{system} member:
130
131\begin{methoddesc}{system.listMethods}{}
132This method returns a list of strings, one for each (non-system)
133method supported by the XML-RPC server.
134\end{methoddesc}
135
Fred Drake4124a0b2001-07-14 02:46:01 +0000136\begin{methoddesc}{system.methodSignature}{name}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000137This method takes one parameter, the name of a method implemented by
138the XML-RPC server.It returns an array of possible signatures for this
139method. A signature is an array of types. The first of these types is
140the return type of the method, the rest are parameters.
141
142Because multiple signatures (ie. overloading) is permitted, this method
143returns a list of signatures rather than a singleton.
144
145Signatures themselves are restricted to the top level parameters
146expected by a method. For instance if a method expects one array of
147structs as a parameter, and it returns a string, its signature is
148simply "string, array". If it expects three integers and returns a
149string, its signature is "string, int, int, int".
150
151If no signature is defined for the method, a non-array value is
152returned. In Python this means that the type of the returned
153value will be something other that list.
154\end{methoddesc}
155
156\begin{methoddesc}{system.methodHelp}{name}
157This method takes one parameter, the name of a method implemented by
158the XML-RPC server. It returns a documentation string describing the
159use of that method. If no such string is available, an empty string is
160returned. The documentation string may contain HTML markup.
161\end{methoddesc}
162
163Introspection methods are currently supported by servers written in
164PHP, C and Microsoft .NET. Partial introspection support is included
165in recent updates to UserLand Frontier. Introspection support for
Skip Montanaro174dd222005-05-14 20:54:16 +0000166Perl, Python and Java is available at the \ulink{XML-RPC
167Hacks}{http://xmlrpc-c.sourceforge.net/hacks.php} page.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000168
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000169
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000170\subsection{Boolean Objects \label{boolean-objects}}
171
172This class may be initialized from any Python value; the instance
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000173returned depends only on its truth value. It supports various Python
174operators through \method{__cmp__()}, \method{__repr__()},
175\method{__int__()}, and \method{__nonzero__()} methods, all
176implemented in the obvious ways.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000177
178It also has the following method, supported mainly for internal use by
179the unmarshalling code:
180
181\begin{methoddesc}{encode}{out}
182Write the XML-RPC encoding of this Boolean item to the out stream object.
183\end{methoddesc}
184
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000185
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000186\subsection{DateTime Objects \label{datetime-objects}}
187
Skip Montanaro174dd222005-05-14 20:54:16 +0000188This class may be initialized with seconds since the epoch, a time tuple, an
189ISO 8601 time/date string, or a {}\class{\refmodule{datetime}.datetime},
190{}\class{\refmodule{datetime}.date} or {}\class{\refmodule{datetime}.time}
191instance. It has the following methods, supported mainly for internal use
192by the marshalling/unmarshalling code:
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000193
194\begin{methoddesc}{decode}{string}
195Accept a string as the instance's new time value.
196\end{methoddesc}
197
198\begin{methoddesc}{encode}{out}
Skip Montanaro174dd222005-05-14 20:54:16 +0000199Write the XML-RPC encoding of this \class{DateTime} item to the
200\var{out} stream object.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000201\end{methoddesc}
202
203It also supports certain of Python's built-in operators through
Skip Montanaro174dd222005-05-14 20:54:16 +0000204\method{__cmp__()} and \method{__repr__()} methods.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000205
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000206
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000207\subsection{Binary Objects \label{binary-objects}}
208
George Yoshidae7670a32006-04-18 16:18:15 +0000209This class may be initialized from string data (which may include NULs).
Fred Drakebb066cf2004-05-12 03:07:27 +0000210The primary access to the content of a \class{Binary} object is
Fred Drake585775b2002-06-14 00:33:02 +0000211provided by an attribute:
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000212
Fred Drake585775b2002-06-14 00:33:02 +0000213\begin{memberdesc}[Binary]{data}
214The binary data encapsulated by the \class{Binary} instance. The data
215is provided as an 8-bit string.
216\end{memberdesc}
217
218\class{Binary} objects have the following methods, supported mainly
219for internal use by the marshalling/unmarshalling code:
220
221\begin{methoddesc}[Binary]{decode}{string}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000222Accept a base64 string and decode it as the instance's new data.
223\end{methoddesc}
224
Fred Drake585775b2002-06-14 00:33:02 +0000225\begin{methoddesc}[Binary]{encode}{out}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000226Write the XML-RPC base 64 encoding of this binary item to the out
227stream object.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000228\end{methoddesc}
229
230It also supports certain of Python's built-in operators through a
Johannes Gijsbersf4a70f32004-12-12 16:52:40 +0000231\method{__cmp__()} method.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000232
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000233
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000234\subsection{Fault Objects \label{fault-objects}}
235
236A \class{Fault} object encapsulates the content of an XML-RPC fault tag.
237Fault objects have the following members:
238
239\begin{memberdesc}{faultCode}
240A string indicating the fault type.
241\end{memberdesc}
242
243\begin{memberdesc}{faultString}
244A string containing a diagnostic message associated with the fault.
245\end{memberdesc}
246
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000247
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000248\subsection{ProtocolError Objects \label{protocol-error-objects}}
249
250A \class{ProtocolError} object describes a protocol error in the
251underlying transport layer (such as a 404 `not found' error if the
252server named by the URI does not exist). It has the following
253members:
254
255\begin{memberdesc}{url}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000256The URI or URL that triggered the error.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000257\end{memberdesc}
258
259\begin{memberdesc}{errcode}
260The error code.
261\end{memberdesc}
262
263\begin{memberdesc}{errmsg}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000264The error message or diagnostic string.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000265\end{memberdesc}
266
267\begin{memberdesc}{headers}
268A string containing the headers of the HTTP/HTTPS request that
269triggered the error.
270\end{memberdesc}
271
Martin v. Löwis45394c22003-10-31 13:49:36 +0000272\subsection{MultiCall Objects}
273
274\versionadded{2.4}
275
Michael W. Hudson1baa2482004-08-07 17:39:35 +0000276In \url{http://www.xmlrpc.com/discuss/msgReader\%241208}, an approach
277is presented to encapsulate multiple calls to a remote server into a
278single request.
Martin v. Löwis45394c22003-10-31 13:49:36 +0000279
280\begin{classdesc}{MultiCall}{server}
281
282Create an object used to boxcar method calls. \var{server} is the
283eventual target of the call. Calls can be made to the result object,
Fred Drake12c29502007-04-26 04:43:58 +0000284but they will immediately return \code{None}, and only store the
Martin v. Löwis45394c22003-10-31 13:49:36 +0000285call name and parameters in the \class{MultiCall} object. Calling
286the object itself causes all stored calls to be transmitted as
287a single \code{system.multicall} request. The result of this call
288is a generator; iterating over this generator yields the individual
289results.
290
291\end{classdesc}
292
293A usage example of this class is
294
295\begin{verbatim}
296multicall = MultiCall(server_proxy)
297multicall.add(2,3)
298multicall.get_address("Guido")
299add_result, address = multicall()
300\end{verbatim}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000301
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000302\subsection{Convenience Functions}
303
Fred Draked90f5092001-10-01 21:05:30 +0000304\begin{funcdesc}{boolean}{value}
305Convert any Python value to one of the XML-RPC Boolean constants,
306\code{True} or \code{False}.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000307\end{funcdesc}
308
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000309\begin{funcdesc}{dumps}{params\optional{, methodname\optional{,
310 methodresponse\optional{, encoding\optional{,
311 allow_none}}}}}
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000312Convert \var{params} into an XML-RPC request.
313or into a response if \var{methodresponse} is true.
314\var{params} can be either a tuple of arguments or an instance of the
315\exception{Fault} exception class. If \var{methodresponse} is true,
316only a single value can be returned, meaning that \var{params} must be of length 1.
317\var{encoding}, if supplied, is the encoding to use in the generated
318XML; the default is UTF-8. Python's \constant{None} value cannot be
319used in standard XML-RPC; to allow using it via an extension,
320provide a true value for \var{allow_none}.
321\end{funcdesc}
322
Skip Montanaro174dd222005-05-14 20:54:16 +0000323\begin{funcdesc}{loads}{data\optional{, use_datetime}}
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000324Convert an XML-RPC request or response into Python objects, a
325\code{(\var{params}, \var{methodname})}. \var{params} is a tuple of argument; \var{methodname}
326is a string, or \code{None} if no method name is present in the packet.
327If the XML-RPC packet represents a fault condition, this
328function will raise a \exception{Fault} exception.
Skip Montanaro174dd222005-05-14 20:54:16 +0000329The \var{use_datetime} flag can be used to cause date/time values to be
330presented as \class{\refmodule{datetime}.datetime} objects; this is false
331by default.
332Note that even if you call an XML-RPC method with
333\class{\refmodule{datetime}.date} or \class{\refmodule{datetime}.time}
334objects, they are converted to \class{DateTime} objects internally, so only
335{}\class{\refmodule{datetime}.datetime} objects will be returned.
336
337\versionchanged[The \var{use_datetime} flag was added]{2.5}
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000338\end{funcdesc}
339
340
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000341
342\subsection{Example of Client Usage \label{xmlrpc-client-example}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000343
344\begin{verbatim}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000345# simple test program (from the XML-RPC specification)
Andrew M. Kuchling356f9382006-04-27 12:38:35 +0000346from xmlrpclib import ServerProxy, Error
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000347
Skip Montanarodc8d4072002-03-14 17:35:25 +0000348# server = ServerProxy("http://localhost:8000") # local server
349server = ServerProxy("http://betty.userland.com")
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000350
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000351print server
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000352
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000353try:
354 print server.examples.getStateName(41)
355except Error, v:
356 print "ERROR", v
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000357\end{verbatim}
Andrew M. Kuchling432be362005-12-08 14:54:20 +0000358
359To access an XML-RPC server through a proxy, you need to define
360a custom transport. The following example,
361written by NoboNobo, % fill in original author's name if we ever learn it
362shows how:
363
364% Example taken from http://lowlife.jp/nobonobo/wiki/xmlrpcwithproxy.html
365\begin{verbatim}
366import xmlrpclib, httplib
367
368class ProxiedTransport(xmlrpclib.Transport):
369 def set_proxy(self, proxy):
370 self.proxy = proxy
371 def make_connection(self, host):
372 self.realhost = host
373 h = httplib.HTTP(self.proxy)
374 return h
375 def send_request(self, connection, handler, request_body):
376 connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
377 def send_host(self, connection, host):
378 connection.putheader('Host', self.realhost)
379
380p = ProxiedTransport()
381p.set_proxy('proxy-server:8080')
382server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)
383print server.currentTime.getCurrentTime()
384\end{verbatim}