blob: 690e669e7fd2ee5fa49af13a8268364303bfc4ab [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.}
5\moduleauthor{Fredrik Lundh}{effbot@telia.com}
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
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
Fred Drake4124a0b2001-07-14 02:46:01 +000020\begin{classdesc}{Server}{uri\optional{, transport\optional{,
21 encoding\optional{, verbose}}}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000022A \class{Server} instance is a server proxy that manages communication
23with 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
31The returned instance is a proxy object with methods that can be used
32to invoke corresponding RPC calls on the remote server. If the remote
33server supports the introspection API, the proxy can also be used to query
34the remote server for the methods it supports (service discovery) and
35fetch other server-associated metadata.
36
37\class{Server} instance methods take Python basic types and objects as
38arguments and return Python basic types and classes. Types that are
39conformable (e.g. that can be marshalled through XML), include the
40following (and except where noted, they are unmarshalled as the same
41Python type):
42
43\begin{tableii}{l|l}{constant}{Name}{Meaning}
Fred Drake5ddf7ad2001-07-12 23:39:24 +000044 \lineii{boolean}{The \constant{True} and \constant{False} constants}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000045 \lineii{integers}{Pass in directly}
46 \lineii{floating-point numbers}{Pass in directly}
47 \lineii{strings}{Pass in directly}
48 \lineii{arrays}{Any Python sequence type containing conformable
Fred Drake5ddf7ad2001-07-12 23:39:24 +000049 elements. Arrays are returned as lists}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000050 \lineii{structures}{A Python dictionary. Keys must be strings,
Fred Drake5ddf7ad2001-07-12 23:39:24 +000051 values may be any conformable type.}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000052 \lineii{dates}{in seconds since the epoch; pass in an instance of the
Fred Drake5ddf7ad2001-07-12 23:39:24 +000053 \class{DateTime} wrapper class}
54 \lineii{binary data}{pass in an instance of the \class{Binary}
55 wrapper class}
Eric S. Raymonde304bb92001-07-12 02:39:45 +000056\end{tableii}
57
58This is the full set of data types supported by XML-RPC. Method calls
Fred Drake5ddf7ad2001-07-12 23:39:24 +000059may also raise a special \exception{Fault} instance, used to signal
60XML-RPC server errors, or \exception{ProtocolError} used to signal an
61error in the HTTP/HTTPS transport layer.
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +000062
63When passing strings, characters special to XML such as \samp{<},
64\samp{>}, and \samp{\&} will be automatically escaped. However, it's
65the caller's responsibility to ensure that the string is free of
66characters that aren't allowed in XML, such as the control characters
67with ASCII values between 0 and 31; failing to do this will result in
68an XML-RPC request that isn't well-formed XML. If you have to pass
69arbitrary strings via XML-RPC, use the \class{Binary} wrapper class
70described below.
71
Eric S. Raymonde304bb92001-07-12 02:39:45 +000072\end{classdesc}
73
Fred Drake5ddf7ad2001-07-12 23:39:24 +000074
75\begin{seealso}
76 \seetitle[http://xmlrpc-c.sourceforge.net/xmlrpc-howto/xmlrpc-howto.html]
77 {XML-RPC HOWTO}{A good description of XML operation and
78 client software in several languages. Contains pretty much
79 everything an XML-RPC client developer needs to know.}
80 \seetitle[http://xmlrpc-c.sourceforge.net/hacks.php]
81 {XML-RPC-Hacks page}{Extensions for various open-source
82 libraries to support instrospection and multicall.}
83\end{seealso}
84
85
Eric S. Raymonde304bb92001-07-12 02:39:45 +000086\subsection{Server Objects \label{server-objects}}
87
88A \class{Server} instance proxy object has a method corresponding to
89each remote procedure call accepted by the XML-RPC server. Calling
90the method performs an RPC, dispatched by both name and argument
91signature (e.g. the same method name can be overloaded with multiple
92argument signatures). The RPC finishes by returning a value, which
93may be either returned data in a conformant type or a \class{Fault} or
94\class{ProtocolError} object indicating an error.
95
96Servers that support the XML introspection API support some common
97methods grouped under the reserved \member{system} member:
98
99\begin{methoddesc}{system.listMethods}{}
100This method returns a list of strings, one for each (non-system)
101method supported by the XML-RPC server.
102\end{methoddesc}
103
Fred Drake4124a0b2001-07-14 02:46:01 +0000104\begin{methoddesc}{system.methodSignature}{name}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000105This method takes one parameter, the name of a method implemented by
106the XML-RPC server.It returns an array of possible signatures for this
107method. A signature is an array of types. The first of these types is
108the return type of the method, the rest are parameters.
109
110Because multiple signatures (ie. overloading) is permitted, this method
111returns a list of signatures rather than a singleton.
112
113Signatures themselves are restricted to the top level parameters
114expected by a method. For instance if a method expects one array of
115structs as a parameter, and it returns a string, its signature is
116simply "string, array". If it expects three integers and returns a
117string, its signature is "string, int, int, int".
118
119If no signature is defined for the method, a non-array value is
120returned. In Python this means that the type of the returned
121value will be something other that list.
122\end{methoddesc}
123
124\begin{methoddesc}{system.methodHelp}{name}
125This method takes one parameter, the name of a method implemented by
126the XML-RPC server. It returns a documentation string describing the
127use of that method. If no such string is available, an empty string is
128returned. The documentation string may contain HTML markup.
129\end{methoddesc}
130
131Introspection methods are currently supported by servers written in
132PHP, C and Microsoft .NET. Partial introspection support is included
133in recent updates to UserLand Frontier. Introspection support for
134Perl, Python and Java is available at the XML-RPC Hacks page.
135
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000136
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000137\subsection{Boolean Objects \label{boolean-objects}}
138
139This class may be initialized from any Python value; the instance
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000140returned depends only on its truth value. It supports various Python
141operators through \method{__cmp__()}, \method{__repr__()},
142\method{__int__()}, and \method{__nonzero__()} methods, all
143implemented in the obvious ways.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000144
145It also has the following method, supported mainly for internal use by
146the unmarshalling code:
147
148\begin{methoddesc}{encode}{out}
149Write the XML-RPC encoding of this Boolean item to the out stream object.
150\end{methoddesc}
151
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000152
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000153\subsection{DateTime Objects \label{datetime-objects}}
154
155This class may initialized from date in seconds since the epoch, a
156time tuple, or an ISO 8601 time/date string. It has the following
157methods, supported mainly for internal use by the
158marshalling/unmarshalling code:
159
160\begin{methoddesc}{decode}{string}
161Accept a string as the instance's new time value.
162\end{methoddesc}
163
164\begin{methoddesc}{encode}{out}
165Write the XML-RPC encoding of this DateTime item to the out stream object.
166\end{methoddesc}
167
168It also supports certain of Python's built-in operators through
169\method{_cmp__} and \method{__repr__} methods.
170
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000171
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000172\subsection{Binary Objects \label{binary-objects}}
173
174This class may initialized from string data (which may include NULs).
175It has the following methods, supported mainly for internal use by the
176marshalling/unmarshalling code:
177
178\begin{methoddesc}{decode}{string}
179Accept a base64 string and decode it as the instance's new data.
180\end{methoddesc}
181
182\begin{methoddesc}{encode}{out}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000183Write the XML-RPC base 64 encoding of this binary item to the out
184stream object.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000185\end{methoddesc}
186
187It also supports certain of Python's built-in operators through a
188\method{_cmp__} method.
189
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000190
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000191\subsection{Fault Objects \label{fault-objects}}
192
193A \class{Fault} object encapsulates the content of an XML-RPC fault tag.
194Fault objects have the following members:
195
196\begin{memberdesc}{faultCode}
197A string indicating the fault type.
198\end{memberdesc}
199
200\begin{memberdesc}{faultString}
201A string containing a diagnostic message associated with the fault.
202\end{memberdesc}
203
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000204
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000205\subsection{ProtocolError Objects \label{protocol-error-objects}}
206
207A \class{ProtocolError} object describes a protocol error in the
208underlying transport layer (such as a 404 `not found' error if the
209server named by the URI does not exist). It has the following
210members:
211
212\begin{memberdesc}{url}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000213The URI or URL that triggered the error.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000214\end{memberdesc}
215
216\begin{memberdesc}{errcode}
217The error code.
218\end{memberdesc}
219
220\begin{memberdesc}{errmsg}
Andrew M. Kuchling10b3eac2002-03-08 17:46:02 +0000221The error message or diagnostic string.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000222\end{memberdesc}
223
224\begin{memberdesc}{headers}
225A string containing the headers of the HTTP/HTTPS request that
226triggered the error.
227\end{memberdesc}
228
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000229
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000230\subsection{Convenience Functions}
231
Fred Draked90f5092001-10-01 21:05:30 +0000232\begin{funcdesc}{boolean}{value}
233Convert any Python value to one of the XML-RPC Boolean constants,
234\code{True} or \code{False}.
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000235\end{funcdesc}
236
237\begin{funcdesc}{binary}{data}
238Trivially convert any Python string to a \class{Binary} object.
239\end{funcdesc}
240
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000241
242\subsection{Example of Client Usage \label{xmlrpc-client-example}}
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000243
244\begin{verbatim}
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000245# simple test program (from the XML-RPC specification)
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000246
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000247# server = Server("http://localhost:8000") # local server
248server = Server("http://betty.userland.com")
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000249
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000250print server
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000251
Fred Drake5ddf7ad2001-07-12 23:39:24 +0000252try:
253 print server.examples.getStateName(41)
254except Error, v:
255 print "ERROR", v
Eric S. Raymonde304bb92001-07-12 02:39:45 +0000256\end{verbatim}