blob: b7bccdb51d51ec1e214c1e0967a6fccc21afa8f2 [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{,
21 encoding\optional{, verbose}}}}
22A \class{ServerProxy} instance is an object that manages communication
Eric S. Raymonde304bb92001-07-12 02:39:45 +000023with a remote XML-RPC server. The required first argument is a URI
24(Uniform Resource Indicator), and will normally be the URL of the
25server. The optional second argument is a transport factory instance;
26by default it is an internal \class{SafeTransport} instance for https:
27URLs and an internal HTTP \class{Transport} instance otherwise. The
28optional third argument is an encoding, by default UTF-8. The optional
29fourth argument is a debugging flag.
30
Fredrik Lundh1303c7c2002-10-22 18:23:00 +000031Both the HTTP and HTTPS transports support the URL syntax extension for
Fredrik Lundh019bd4a2002-10-22 18:26:28 +000032HTTP Basic Authentication: \code{http://user:pass@host:port/path}. The
Fredrik Lundh1303c7c2002-10-22 18:23:00 +000033\code{user:pass} portion will be base64-encoded as an HTTP `Authorization'
34header, and sent to the remote server as part of the connection process
35when invoking an XML-RPC method. You only need to use this if the
36remote server requires a Basic Authentication user and password.
37
Eric S. Raymonde304bb92001-07-12 02:39:45 +000038The returned instance is a proxy object with methods that can be used
39to invoke corresponding RPC calls on the remote server. If the remote
40server supports the introspection API, the proxy can also be used to query
41the remote server for the methods it supports (service discovery) and
42fetch other server-associated metadata.
43
Skip Montanarodc8d4072002-03-14 17:35:25 +000044\class{ServerProxy} instance methods take Python basic types and objects as
Eric S. Raymonde304bb92001-07-12 02:39:45 +000045arguments and return Python basic types and classes. Types that are
46conformable (e.g. that can be marshalled through XML), include the
47following (and except where noted, they are unmarshalled as the same
48Python type):
49
50\begin{tableii}{l|l}{constant}{Name}{Meaning}
Fred Drake5ddf7ad2001-07-12 23:39:24 +000051 \lineii{boolean}{The \constant{True} and \constant{False} constants}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000052 \lineii{integers}{Pass in directly}
53 \lineii{floating-point numbers}{Pass in directly}
54 \lineii{strings}{Pass in directly}
55 \lineii{arrays}{Any Python sequence type containing conformable
Fred Drake5ddf7ad2001-07-12 23:39:24 +000056 elements. Arrays are returned as lists}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000057 \lineii{structures}{A Python dictionary. Keys must be strings,
Fred Drake5ddf7ad2001-07-12 23:39:24 +000058 values may be any conformable type.}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000059 \lineii{dates}{in seconds since the epoch; pass in an instance of the
Fred Drake5ddf7ad2001-07-12 23:39:24 +000060 \class{DateTime} wrapper class}
61 \lineii{binary data}{pass in an instance of the \class{Binary}
62 wrapper class}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000063\end{tableii}
64
65This is the full set of data types supported by XML-RPC. Method calls
Fred Drake5ddf7ad2001-07-12 23:39:24 +000066may also raise a special \exception{Fault} instance, used to signal
67XML-RPC server errors, or \exception{ProtocolError} used to signal an
Skip Montanaro10acc8f2002-03-17 23:15:02 +000068error in the HTTP/HTTPS transport layer. Note that even though starting
69with Python 2.2 you can subclass builtin types, the xmlrpclib module
70currently does not marshal instances of such subclasses.
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +000071
72When passing strings, characters special to XML such as \samp{<},
73\samp{>}, and \samp{\&} will be automatically escaped. However, it's
74the caller's responsibility to ensure that the string is free of
75characters that aren't allowed in XML, such as the control characters
76with ASCII values between 0 and 31; failing to do this will result in
77an XML-RPC request that isn't well-formed XML. If you have to pass
78arbitrary strings via XML-RPC, use the \class{Binary} wrapper class
79described below.
80
Skip Montanarodc8d4072002-03-14 17:35:25 +000081\class{Server} is retained as an alias for \class{ServerProxy} for backwards
82compatibility. New code should use \class{ServerProxy}.
83
Eric S. Raymonde304bb92001-07-12 02:39:45 +000084\end{classdesc}
85
Fred Drake5ddf7ad2001-07-12 23:39:24 +000086
87\begin{seealso}
88 \seetitle[http://xmlrpc-c.sourceforge.net/xmlrpc-howto/xmlrpc-howto.html]
89 {XML-RPC HOWTO}{A good description of XML operation and
90 client software in several languages. Contains pretty much
91 everything an XML-RPC client developer needs to know.}
92 \seetitle[http://xmlrpc-c.sourceforge.net/hacks.php]
93 {XML-RPC-Hacks page}{Extensions for various open-source
94 libraries to support instrospection and multicall.}
95\end{seealso}
96
97
Skip Montanarodc8d4072002-03-14 17:35:25 +000098\subsection{ServerProxy Objects \label{serverproxy-objects}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000099
Skip Montanarodc8d4072002-03-14 17:35:25 +0000100A \class{ServerProxy} instance has a method corresponding to
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000101each remote procedure call accepted by the XML-RPC server. Calling
102the method performs an RPC, dispatched by both name and argument
103signature (e.g. the same method name can be overloaded with multiple
104argument signatures). The RPC finishes by returning a value, which
105may be either returned data in a conformant type or a \class{Fault} or
106\class{ProtocolError} object indicating an error.
107
108Servers that support the XML introspection API support some common
109methods grouped under the reserved \member{system} member:
110
111\begin{methoddesc}{system.listMethods}{}
112This method returns a list of strings, one for each (non-system)
113method supported by the XML-RPC server.
114\end{methoddesc}
115
Fred Drake4124a0b2001-07-14 02:46:01 +0000116\begin{methoddesc}{system.methodSignature}{name}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000117This method takes one parameter, the name of a method implemented by
118the XML-RPC server.It returns an array of possible signatures for this
119method. A signature is an array of types. The first of these types is
120the return type of the method, the rest are parameters.
121
122Because multiple signatures (ie. overloading) is permitted, this method
123returns a list of signatures rather than a singleton.
124
125Signatures themselves are restricted to the top level parameters
126expected by a method. For instance if a method expects one array of
127structs as a parameter, and it returns a string, its signature is
128simply "string, array". If it expects three integers and returns a
129string, its signature is "string, int, int, int".
130
131If no signature is defined for the method, a non-array value is
132returned. In Python this means that the type of the returned
133value will be something other that list.
134\end{methoddesc}
135
136\begin{methoddesc}{system.methodHelp}{name}
137This method takes one parameter, the name of a method implemented by
138the XML-RPC server. It returns a documentation string describing the
139use of that method. If no such string is available, an empty string is
140returned. The documentation string may contain HTML markup.
141\end{methoddesc}
142
143Introspection methods are currently supported by servers written in
144PHP, C and Microsoft .NET. Partial introspection support is included
145in recent updates to UserLand Frontier. Introspection support for
146Perl, Python and Java is available at the XML-RPC Hacks page.
147
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000148
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000149\subsection{Boolean Objects \label{boolean-objects}}
150
151This class may be initialized from any Python value; the instance
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000152returned depends only on its truth value. It supports various Python
153operators through \method{__cmp__()}, \method{__repr__()},
154\method{__int__()}, and \method{__nonzero__()} methods, all
155implemented in the obvious ways.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000156
157It also has the following method, supported mainly for internal use by
158the unmarshalling code:
159
160\begin{methoddesc}{encode}{out}
161Write the XML-RPC encoding of this Boolean item to the out stream object.
162\end{methoddesc}
163
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000164
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000165\subsection{DateTime Objects \label{datetime-objects}}
166
167This class may initialized from date in seconds since the epoch, a
168time tuple, or an ISO 8601 time/date string. It has the following
169methods, supported mainly for internal use by the
170marshalling/unmarshalling code:
171
172\begin{methoddesc}{decode}{string}
173Accept a string as the instance's new time value.
174\end{methoddesc}
175
176\begin{methoddesc}{encode}{out}
177Write the XML-RPC encoding of this DateTime item to the out stream object.
178\end{methoddesc}
179
180It also supports certain of Python's built-in operators through
181\method{_cmp__} and \method{__repr__} methods.
182
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000183
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000184\subsection{Binary Objects \label{binary-objects}}
185
186This class may initialized from string data (which may include NULs).
Fred Drake585775b2002-06-14 00:33:02 +0000187The primary acess to the content of a \class{Binary} object is
188provided by an attribute:
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000189
Fred Drake585775b2002-06-14 00:33:02 +0000190\begin{memberdesc}[Binary]{data}
191The binary data encapsulated by the \class{Binary} instance. The data
192is provided as an 8-bit string.
193\end{memberdesc}
194
195\class{Binary} objects have the following methods, supported mainly
196for internal use by the marshalling/unmarshalling code:
197
198\begin{methoddesc}[Binary]{decode}{string}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000199Accept a base64 string and decode it as the instance's new data.
200\end{methoddesc}
201
Fred Drake585775b2002-06-14 00:33:02 +0000202\begin{methoddesc}[Binary]{encode}{out}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000203Write the XML-RPC base 64 encoding of this binary item to the out
204stream object.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000205\end{methoddesc}
206
207It also supports certain of Python's built-in operators through a
Fred Drake585775b2002-06-14 00:33:02 +0000208\method{_cmp__()} method.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000209
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000210
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000211\subsection{Fault Objects \label{fault-objects}}
212
213A \class{Fault} object encapsulates the content of an XML-RPC fault tag.
214Fault objects have the following members:
215
216\begin{memberdesc}{faultCode}
217A string indicating the fault type.
218\end{memberdesc}
219
220\begin{memberdesc}{faultString}
221A string containing a diagnostic message associated with the fault.
222\end{memberdesc}
223
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000224
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000225\subsection{ProtocolError Objects \label{protocol-error-objects}}
226
227A \class{ProtocolError} object describes a protocol error in the
228underlying transport layer (such as a 404 `not found' error if the
229server named by the URI does not exist). It has the following
230members:
231
232\begin{memberdesc}{url}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000233The URI or URL that triggered the error.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000234\end{memberdesc}
235
236\begin{memberdesc}{errcode}
237The error code.
238\end{memberdesc}
239
240\begin{memberdesc}{errmsg}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000241The error message or diagnostic string.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000242\end{memberdesc}
243
244\begin{memberdesc}{headers}
245A string containing the headers of the HTTP/HTTPS request that
246triggered the error.
247\end{memberdesc}
248
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000249
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000250\subsection{Convenience Functions}
251
Fred Draked90f5092001-10-01 21:05:30 +0000252\begin{funcdesc}{boolean}{value}
253Convert any Python value to one of the XML-RPC Boolean constants,
254\code{True} or \code{False}.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000255\end{funcdesc}
256
257\begin{funcdesc}{binary}{data}
258Trivially convert any Python string to a \class{Binary} object.
259\end{funcdesc}
260
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000261
262\subsection{Example of Client Usage \label{xmlrpc-client-example}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000263
264\begin{verbatim}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000265# simple test program (from the XML-RPC specification)
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000266
Skip Montanarodc8d4072002-03-14 17:35:25 +0000267# server = ServerProxy("http://localhost:8000") # local server
268server = ServerProxy("http://betty.userland.com")
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000269
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000270print server
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000271
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000272try:
273 print server.examples.getStateName(41)
274except Error, v:
275 print "ERROR", v
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000276\end{verbatim}