blob: c870d262c62b17fdad3a12c3f4a47fad1abe4554 [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,
Thomas Wouters89f507f2006-12-13 04:49:30 +000071 values may be any conformable type. Objects
72 of user-defined classes can be passed in;
73 only their \var{__dict__} attribute is
74 transmitted.}
Skip Montanaro174dd222005-05-14 20:54:16 +000075 \lineii{dates}{in seconds since the epoch (pass in an instance of the
76 \class{DateTime} class) or a
77 \class{\refmodule{datetime}.datetime},
78 \class{\refmodule{datetime}.date} or
79 \class{\refmodule{datetime}.time} instance}
Fred Drake5ddf7ad2001-07-12 23:39:24 +000080 \lineii{binary data}{pass in an instance of the \class{Binary}
81 wrapper class}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000082\end{tableii}
83
84This is the full set of data types supported by XML-RPC. Method calls
Fred Drake5ddf7ad2001-07-12 23:39:24 +000085may also raise a special \exception{Fault} instance, used to signal
86XML-RPC server errors, or \exception{ProtocolError} used to signal an
Thomas Wouters477c8d52006-05-27 19:21:47 +000087error in the HTTP/HTTPS transport layer. Both \exception{Fault} and
88\exception{ProtocolError} derive from a base class called
89\exception{Error}. Note that even though starting with Python 2.2 you
90can subclass builtin types, the xmlrpclib module currently does not
91marshal instances of such subclasses.
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +000092
93When passing strings, characters special to XML such as \samp{<},
94\samp{>}, and \samp{\&} will be automatically escaped. However, it's
95the caller's responsibility to ensure that the string is free of
96characters that aren't allowed in XML, such as the control characters
97with ASCII values between 0 and 31; failing to do this will result in
98an XML-RPC request that isn't well-formed XML. If you have to pass
99arbitrary strings via XML-RPC, use the \class{Binary} wrapper class
100described below.
101
Skip Montanarodc8d4072002-03-14 17:35:25 +0000102\class{Server} is retained as an alias for \class{ServerProxy} for backwards
103compatibility. New code should use \class{ServerProxy}.
104
Skip Montanaro174dd222005-05-14 20:54:16 +0000105\versionchanged[The \var{use_datetime} flag was added]{2.5}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106
107\versionchanged[Instances of new-style classes can be passed in
108if they have an \var{__dict__} attribute and don't have a base class
109that is marshalled in a special way]{2.6}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000110\end{classdesc}
111
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000112
113\begin{seealso}
Fred Drake3f207b62005-11-29 12:40:58 +0000114 \seetitle[http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html]
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000115 {XML-RPC HOWTO}{A good description of XML operation and
116 client software in several languages. Contains pretty much
117 everything an XML-RPC client developer needs to know.}
118 \seetitle[http://xmlrpc-c.sourceforge.net/hacks.php]
Skip Montanaro174dd222005-05-14 20:54:16 +0000119 {XML-RPC Hacks page}{Extensions for various open-source
Raymond Hettinger68804312005-01-01 00:28:46 +0000120 libraries to support introspection and multicall.}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000121\end{seealso}
122
123
Skip Montanarodc8d4072002-03-14 17:35:25 +0000124\subsection{ServerProxy Objects \label{serverproxy-objects}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000125
Skip Montanarodc8d4072002-03-14 17:35:25 +0000126A \class{ServerProxy} instance has a method corresponding to
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000127each remote procedure call accepted by the XML-RPC server. Calling
128the method performs an RPC, dispatched by both name and argument
129signature (e.g. the same method name can be overloaded with multiple
130argument signatures). The RPC finishes by returning a value, which
131may be either returned data in a conformant type or a \class{Fault} or
132\class{ProtocolError} object indicating an error.
133
134Servers that support the XML introspection API support some common
135methods grouped under the reserved \member{system} member:
136
137\begin{methoddesc}{system.listMethods}{}
138This method returns a list of strings, one for each (non-system)
139method supported by the XML-RPC server.
140\end{methoddesc}
141
Fred Drake4124a0b2001-07-14 02:46:01 +0000142\begin{methoddesc}{system.methodSignature}{name}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000143This method takes one parameter, the name of a method implemented by
144the XML-RPC server.It returns an array of possible signatures for this
145method. A signature is an array of types. The first of these types is
146the return type of the method, the rest are parameters.
147
148Because multiple signatures (ie. overloading) is permitted, this method
149returns a list of signatures rather than a singleton.
150
151Signatures themselves are restricted to the top level parameters
152expected by a method. For instance if a method expects one array of
153structs as a parameter, and it returns a string, its signature is
154simply "string, array". If it expects three integers and returns a
155string, its signature is "string, int, int, int".
156
157If no signature is defined for the method, a non-array value is
158returned. In Python this means that the type of the returned
159value will be something other that list.
160\end{methoddesc}
161
162\begin{methoddesc}{system.methodHelp}{name}
163This method takes one parameter, the name of a method implemented by
164the XML-RPC server. It returns a documentation string describing the
165use of that method. If no such string is available, an empty string is
166returned. The documentation string may contain HTML markup.
167\end{methoddesc}
168
169Introspection methods are currently supported by servers written in
170PHP, C and Microsoft .NET. Partial introspection support is included
171in recent updates to UserLand Frontier. Introspection support for
Skip Montanaro174dd222005-05-14 20:54:16 +0000172Perl, Python and Java is available at the \ulink{XML-RPC
173Hacks}{http://xmlrpc-c.sourceforge.net/hacks.php} page.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000174
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000175
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000176\subsection{Boolean Objects \label{boolean-objects}}
177
178This class may be initialized from any Python value; the instance
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000179returned depends only on its truth value. It supports various Python
180operators through \method{__cmp__()}, \method{__repr__()},
Jack Diederich4dafcc42006-11-28 19:15:13 +0000181\method{__int__()}, and \method{__bool__()} methods, all
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000182implemented in the obvious ways.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000183
184It also has the following method, supported mainly for internal use by
185the unmarshalling code:
186
187\begin{methoddesc}{encode}{out}
188Write the XML-RPC encoding of this Boolean item to the out stream object.
189\end{methoddesc}
190
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000191
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000192\subsection{DateTime Objects \label{datetime-objects}}
193
Skip Montanaro174dd222005-05-14 20:54:16 +0000194This class may be initialized with seconds since the epoch, a time tuple, an
195ISO 8601 time/date string, or a {}\class{\refmodule{datetime}.datetime},
196{}\class{\refmodule{datetime}.date} or {}\class{\refmodule{datetime}.time}
197instance. It has the following methods, supported mainly for internal use
198by the marshalling/unmarshalling code:
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000199
200\begin{methoddesc}{decode}{string}
201Accept a string as the instance's new time value.
202\end{methoddesc}
203
204\begin{methoddesc}{encode}{out}
Skip Montanaro174dd222005-05-14 20:54:16 +0000205Write the XML-RPC encoding of this \class{DateTime} item to the
206\var{out} stream object.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000207\end{methoddesc}
208
209It also supports certain of Python's built-in operators through
Skip Montanaro174dd222005-05-14 20:54:16 +0000210\method{__cmp__()} and \method{__repr__()} methods.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000211
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000212
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000213\subsection{Binary Objects \label{binary-objects}}
214
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215This class may be initialized from string data (which may include NULs).
Fred Drakebb066cf2004-05-12 03:07:27 +0000216The primary access to the content of a \class{Binary} object is
Fred Drake585775b2002-06-14 00:33:02 +0000217provided by an attribute:
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000218
Fred Drake585775b2002-06-14 00:33:02 +0000219\begin{memberdesc}[Binary]{data}
220The binary data encapsulated by the \class{Binary} instance. The data
221is provided as an 8-bit string.
222\end{memberdesc}
223
224\class{Binary} objects have the following methods, supported mainly
225for internal use by the marshalling/unmarshalling code:
226
227\begin{methoddesc}[Binary]{decode}{string}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000228Accept a base64 string and decode it as the instance's new data.
229\end{methoddesc}
230
Fred Drake585775b2002-06-14 00:33:02 +0000231\begin{methoddesc}[Binary]{encode}{out}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000232Write the XML-RPC base 64 encoding of this binary item to the out
233stream object.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000234\end{methoddesc}
235
236It also supports certain of Python's built-in operators through a
Johannes Gijsbersf4a70f32004-12-12 16:52:40 +0000237\method{__cmp__()} method.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000238
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000239
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000240\subsection{Fault Objects \label{fault-objects}}
241
242A \class{Fault} object encapsulates the content of an XML-RPC fault tag.
243Fault objects have the following members:
244
245\begin{memberdesc}{faultCode}
246A string indicating the fault type.
247\end{memberdesc}
248
249\begin{memberdesc}{faultString}
250A string containing a diagnostic message associated with the fault.
251\end{memberdesc}
252
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000253
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000254\subsection{ProtocolError Objects \label{protocol-error-objects}}
255
256A \class{ProtocolError} object describes a protocol error in the
257underlying transport layer (such as a 404 `not found' error if the
258server named by the URI does not exist). It has the following
259members:
260
261\begin{memberdesc}{url}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000262The URI or URL that triggered the error.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000263\end{memberdesc}
264
265\begin{memberdesc}{errcode}
266The error code.
267\end{memberdesc}
268
269\begin{memberdesc}{errmsg}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000270The error message or diagnostic string.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000271\end{memberdesc}
272
273\begin{memberdesc}{headers}
274A string containing the headers of the HTTP/HTTPS request that
275triggered the error.
276\end{memberdesc}
277
Martin v. Löwis45394c22003-10-31 13:49:36 +0000278\subsection{MultiCall Objects}
279
280\versionadded{2.4}
281
Michael W. Hudson1baa2482004-08-07 17:39:35 +0000282In \url{http://www.xmlrpc.com/discuss/msgReader\%241208}, an approach
283is presented to encapsulate multiple calls to a remote server into a
284single request.
Martin v. Löwis45394c22003-10-31 13:49:36 +0000285
286\begin{classdesc}{MultiCall}{server}
287
288Create an object used to boxcar method calls. \var{server} is the
289eventual target of the call. Calls can be made to the result object,
290but they will immediately return \var{None}, and only store the
291call name and parameters in the \class{MultiCall} object. Calling
292the object itself causes all stored calls to be transmitted as
293a single \code{system.multicall} request. The result of this call
294is a generator; iterating over this generator yields the individual
295results.
296
297\end{classdesc}
298
299A usage example of this class is
300
301\begin{verbatim}
302multicall = MultiCall(server_proxy)
303multicall.add(2,3)
304multicall.get_address("Guido")
305add_result, address = multicall()
306\end{verbatim}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000307
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000308\subsection{Convenience Functions}
309
Fred Draked90f5092001-10-01 21:05:30 +0000310\begin{funcdesc}{boolean}{value}
311Convert any Python value to one of the XML-RPC Boolean constants,
312\code{True} or \code{False}.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000313\end{funcdesc}
314
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000315\begin{funcdesc}{dumps}{params\optional{, methodname\optional{,
316 methodresponse\optional{, encoding\optional{,
317 allow_none}}}}}
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000318Convert \var{params} into an XML-RPC request.
319or into a response if \var{methodresponse} is true.
320\var{params} can be either a tuple of arguments or an instance of the
321\exception{Fault} exception class. If \var{methodresponse} is true,
322only a single value can be returned, meaning that \var{params} must be of length 1.
323\var{encoding}, if supplied, is the encoding to use in the generated
324XML; the default is UTF-8. Python's \constant{None} value cannot be
325used in standard XML-RPC; to allow using it via an extension,
326provide a true value for \var{allow_none}.
327\end{funcdesc}
328
Skip Montanaro174dd222005-05-14 20:54:16 +0000329\begin{funcdesc}{loads}{data\optional{, use_datetime}}
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000330Convert an XML-RPC request or response into Python objects, a
331\code{(\var{params}, \var{methodname})}. \var{params} is a tuple of argument; \var{methodname}
332is a string, or \code{None} if no method name is present in the packet.
333If the XML-RPC packet represents a fault condition, this
334function will raise a \exception{Fault} exception.
Skip Montanaro174dd222005-05-14 20:54:16 +0000335The \var{use_datetime} flag can be used to cause date/time values to be
336presented as \class{\refmodule{datetime}.datetime} objects; this is false
337by default.
338Note that even if you call an XML-RPC method with
339\class{\refmodule{datetime}.date} or \class{\refmodule{datetime}.time}
340objects, they are converted to \class{DateTime} objects internally, so only
341{}\class{\refmodule{datetime}.datetime} objects will be returned.
342
343\versionchanged[The \var{use_datetime} flag was added]{2.5}
Andrew M. Kuchling38afcef2003-10-22 14:12:03 +0000344\end{funcdesc}
345
346
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000347
348\subsection{Example of Client Usage \label{xmlrpc-client-example}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000349
350\begin{verbatim}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000351# simple test program (from the XML-RPC specification)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000352from xmlrpclib import ServerProxy, Error
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000353
Skip Montanarodc8d4072002-03-14 17:35:25 +0000354# server = ServerProxy("http://localhost:8000") # local server
355server = ServerProxy("http://betty.userland.com")
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000356
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000357print server
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000358
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000359try:
360 print server.examples.getStateName(41)
361except Error, v:
362 print "ERROR", v
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000363\end{verbatim}
Andrew M. Kuchling432be362005-12-08 14:54:20 +0000364
365To access an XML-RPC server through a proxy, you need to define
366a custom transport. The following example,
367written by NoboNobo, % fill in original author's name if we ever learn it
368shows how:
369
370% Example taken from http://lowlife.jp/nobonobo/wiki/xmlrpcwithproxy.html
371\begin{verbatim}
372import xmlrpclib, httplib
373
374class ProxiedTransport(xmlrpclib.Transport):
375 def set_proxy(self, proxy):
376 self.proxy = proxy
377 def make_connection(self, host):
378 self.realhost = host
379 h = httplib.HTTP(self.proxy)
380 return h
381 def send_request(self, connection, handler, request_body):
382 connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
383 def send_host(self, connection, host):
384 connection.putheader('Host', self.realhost)
385
386p = ProxiedTransport()
387p.set_proxy('proxy-server:8080')
388server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)
389print server.currentTime.getCurrentTime()
390\end{verbatim}