1 | \section{\module{xmlrpclib} --- XML-RPC client access}
|
---|
2 |
|
---|
3 | \declaremodule{standard}{xmlrpclib}
|
---|
4 | \modulesynopsis{XML-RPC client access.}
|
---|
5 | \moduleauthor{Fredrik Lundh}{fredrik@pythonware.com}
|
---|
6 | \sectionauthor{Eric S. Raymond}{esr@snark.thyrsus.com}
|
---|
7 |
|
---|
8 | % Not everything is documented yet. It might be good to describe
|
---|
9 | % Marshaller, Unmarshaller, getparser, dumps, loads, and Transport.
|
---|
10 |
|
---|
11 | \versionadded{2.2}
|
---|
12 |
|
---|
13 | XML-RPC is a Remote Procedure Call method that uses XML passed via
|
---|
14 | HTTP as a transport. With it, a client can call methods with
|
---|
15 | parameters on a remote server (the server is named by a URI) and get back
|
---|
16 | structured data. This module supports writing XML-RPC client code; it
|
---|
17 | handles all the details of translating between conformable Python
|
---|
18 | objects and XML on the wire.
|
---|
19 |
|
---|
20 | \begin{classdesc}{ServerProxy}{uri\optional{, transport\optional{,
|
---|
21 | encoding\optional{, verbose\optional{,
|
---|
22 | allow_none\optional{, use_datetime}}}}}}
|
---|
23 | A \class{ServerProxy} instance is an object that manages communication
|
---|
24 | with a remote XML-RPC server. The required first argument is a URI
|
---|
25 | (Uniform Resource Indicator), and will normally be the URL of the
|
---|
26 | server. The optional second argument is a transport factory instance;
|
---|
27 | by default it is an internal \class{SafeTransport} instance for https:
|
---|
28 | URLs and an internal HTTP \class{Transport} instance otherwise. The
|
---|
29 | optional third argument is an encoding, by default UTF-8. The optional
|
---|
30 | fourth argument is a debugging flag. If \var{allow_none} is true,
|
---|
31 | the Python constant \code{None} will be translated into XML; the
|
---|
32 | default behaviour is for \code{None} to raise a \exception{TypeError}.
|
---|
33 | This is a commonly-used extension to the XML-RPC specification, but isn't
|
---|
34 | supported by all clients and servers; see
|
---|
35 | \url{http://ontosys.com/xml-rpc/extensions.php} for a description.
|
---|
36 | The \var{use_datetime} flag can be used to cause date/time values to be
|
---|
37 | presented as \class{\refmodule{datetime}.datetime} objects; this is false
|
---|
38 | by default. \class{\refmodule{datetime}.datetime},
|
---|
39 | \class{\refmodule{datetime}.date} and \class{\refmodule{datetime}.time}
|
---|
40 | objects may be passed to calls. \class{\refmodule{datetime}.date} objects
|
---|
41 | are converted with a time of ``00:00:00''.
|
---|
42 | \class{\refmodule{datetime}.time} objects are converted using today's date.
|
---|
43 |
|
---|
44 | Both the HTTP and HTTPS transports support the URL syntax extension for
|
---|
45 | HTTP Basic Authentication: \code{http://user:pass@host:port/path}. The
|
---|
46 | \code{user:pass} portion will be base64-encoded as an HTTP `Authorization'
|
---|
47 | header, and sent to the remote server as part of the connection process
|
---|
48 | when invoking an XML-RPC method. You only need to use this if the
|
---|
49 | remote server requires a Basic Authentication user and password.
|
---|
50 |
|
---|
51 | The returned instance is a proxy object with methods that can be used
|
---|
52 | to invoke corresponding RPC calls on the remote server. If the remote
|
---|
53 | server supports the introspection API, the proxy can also be used to query
|
---|
54 | the remote server for the methods it supports (service discovery) and
|
---|
55 | fetch other server-associated metadata.
|
---|
56 |
|
---|
57 | \class{ServerProxy} instance methods take Python basic types and objects as
|
---|
58 | arguments and return Python basic types and classes. Types that are
|
---|
59 | conformable (e.g. that can be marshalled through XML), include the
|
---|
60 | following (and except where noted, they are unmarshalled as the same
|
---|
61 | Python type):
|
---|
62 |
|
---|
63 | \begin{tableii}{l|l}{constant}{Name}{Meaning}
|
---|
64 | \lineii{boolean}{The \constant{True} and \constant{False} constants}
|
---|
65 | \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
|
---|
69 | elements. Arrays are returned as lists}
|
---|
70 | \lineii{structures}{A Python dictionary. Keys must be strings,
|
---|
71 | values may be any conformable type.}
|
---|
72 | \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}
|
---|
77 | \lineii{binary data}{pass in an instance of the \class{Binary}
|
---|
78 | wrapper class}
|
---|
79 | \end{tableii}
|
---|
80 |
|
---|
81 | This is the full set of data types supported by XML-RPC. Method calls
|
---|
82 | may also raise a special \exception{Fault} instance, used to signal
|
---|
83 | XML-RPC server errors, or \exception{ProtocolError} used to signal an
|
---|
84 | error 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
|
---|
87 | can subclass builtin types, the xmlrpclib module currently does not
|
---|
88 | marshal instances of such subclasses.
|
---|
89 |
|
---|
90 | When passing strings, characters special to XML such as \samp{<},
|
---|
91 | \samp{>}, and \samp{\&} will be automatically escaped. However, it's
|
---|
92 | the caller's responsibility to ensure that the string is free of
|
---|
93 | characters that aren't allowed in XML, such as the control characters
|
---|
94 | with ASCII values between 0 and 31; failing to do this will result in
|
---|
95 | an XML-RPC request that isn't well-formed XML. If you have to pass
|
---|
96 | arbitrary strings via XML-RPC, use the \class{Binary} wrapper class
|
---|
97 | described below.
|
---|
98 |
|
---|
99 | \class{Server} is retained as an alias for \class{ServerProxy} for backwards
|
---|
100 | compatibility. New code should use \class{ServerProxy}.
|
---|
101 |
|
---|
102 | \versionchanged[The \var{use_datetime} flag was added]{2.5}
|
---|
103 | \end{classdesc}
|
---|
104 |
|
---|
105 |
|
---|
106 | \begin{seealso}
|
---|
107 | \seetitle[http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html]
|
---|
108 | {XML-RPC HOWTO}{A good description of XML operation and
|
---|
109 | client software in several languages. Contains pretty much
|
---|
110 | everything an XML-RPC client developer needs to know.}
|
---|
111 | \seetitle[http://xmlrpc-c.sourceforge.net/hacks.php]
|
---|
112 | {XML-RPC Hacks page}{Extensions for various open-source
|
---|
113 | libraries to support introspection and multicall.}
|
---|
114 | \end{seealso}
|
---|
115 |
|
---|
116 |
|
---|
117 | \subsection{ServerProxy Objects \label{serverproxy-objects}}
|
---|
118 |
|
---|
119 | A \class{ServerProxy} instance has a method corresponding to
|
---|
120 | each remote procedure call accepted by the XML-RPC server. Calling
|
---|
121 | the method performs an RPC, dispatched by both name and argument
|
---|
122 | signature (e.g. the same method name can be overloaded with multiple
|
---|
123 | argument signatures). The RPC finishes by returning a value, which
|
---|
124 | may be either returned data in a conformant type or a \class{Fault} or
|
---|
125 | \class{ProtocolError} object indicating an error.
|
---|
126 |
|
---|
127 | Servers that support the XML introspection API support some common
|
---|
128 | methods grouped under the reserved \member{system} member:
|
---|
129 |
|
---|
130 | \begin{methoddesc}{system.listMethods}{}
|
---|
131 | This method returns a list of strings, one for each (non-system)
|
---|
132 | method supported by the XML-RPC server.
|
---|
133 | \end{methoddesc}
|
---|
134 |
|
---|
135 | \begin{methoddesc}{system.methodSignature}{name}
|
---|
136 | This method takes one parameter, the name of a method implemented by
|
---|
137 | the XML-RPC server.It returns an array of possible signatures for this
|
---|
138 | method. A signature is an array of types. The first of these types is
|
---|
139 | the return type of the method, the rest are parameters.
|
---|
140 |
|
---|
141 | Because multiple signatures (ie. overloading) is permitted, this method
|
---|
142 | returns a list of signatures rather than a singleton.
|
---|
143 |
|
---|
144 | Signatures themselves are restricted to the top level parameters
|
---|
145 | expected by a method. For instance if a method expects one array of
|
---|
146 | structs as a parameter, and it returns a string, its signature is
|
---|
147 | simply "string, array". If it expects three integers and returns a
|
---|
148 | string, its signature is "string, int, int, int".
|
---|
149 |
|
---|
150 | If no signature is defined for the method, a non-array value is
|
---|
151 | returned. In Python this means that the type of the returned
|
---|
152 | value will be something other that list.
|
---|
153 | \end{methoddesc}
|
---|
154 |
|
---|
155 | \begin{methoddesc}{system.methodHelp}{name}
|
---|
156 | This method takes one parameter, the name of a method implemented by
|
---|
157 | the XML-RPC server. It returns a documentation string describing the
|
---|
158 | use of that method. If no such string is available, an empty string is
|
---|
159 | returned. The documentation string may contain HTML markup.
|
---|
160 | \end{methoddesc}
|
---|
161 |
|
---|
162 | Introspection methods are currently supported by servers written in
|
---|
163 | PHP, C and Microsoft .NET. Partial introspection support is included
|
---|
164 | in recent updates to UserLand Frontier. Introspection support for
|
---|
165 | Perl, Python and Java is available at the \ulink{XML-RPC
|
---|
166 | Hacks}{http://xmlrpc-c.sourceforge.net/hacks.php} page.
|
---|
167 |
|
---|
168 |
|
---|
169 | \subsection{Boolean Objects \label{boolean-objects}}
|
---|
170 |
|
---|
171 | This class may be initialized from any Python value; the instance
|
---|
172 | returned depends only on its truth value. It supports various Python
|
---|
173 | operators through \method{__cmp__()}, \method{__repr__()},
|
---|
174 | \method{__int__()}, and \method{__nonzero__()} methods, all
|
---|
175 | implemented in the obvious ways.
|
---|
176 |
|
---|
177 | It also has the following method, supported mainly for internal use by
|
---|
178 | the unmarshalling code:
|
---|
179 |
|
---|
180 | \begin{methoddesc}{encode}{out}
|
---|
181 | Write the XML-RPC encoding of this Boolean item to the out stream object.
|
---|
182 | \end{methoddesc}
|
---|
183 |
|
---|
184 |
|
---|
185 | \subsection{DateTime Objects \label{datetime-objects}}
|
---|
186 |
|
---|
187 | This class may be initialized with seconds since the epoch, a time tuple, an
|
---|
188 | ISO 8601 time/date string, or a {}\class{\refmodule{datetime}.datetime},
|
---|
189 | {}\class{\refmodule{datetime}.date} or {}\class{\refmodule{datetime}.time}
|
---|
190 | instance. It has the following methods, supported mainly for internal use
|
---|
191 | by the marshalling/unmarshalling code:
|
---|
192 |
|
---|
193 | \begin{methoddesc}{decode}{string}
|
---|
194 | Accept a string as the instance's new time value.
|
---|
195 | \end{methoddesc}
|
---|
196 |
|
---|
197 | \begin{methoddesc}{encode}{out}
|
---|
198 | Write the XML-RPC encoding of this \class{DateTime} item to the
|
---|
199 | \var{out} stream object.
|
---|
200 | \end{methoddesc}
|
---|
201 |
|
---|
202 | It also supports certain of Python's built-in operators through
|
---|
203 | \method{__cmp__()} and \method{__repr__()} methods.
|
---|
204 |
|
---|
205 |
|
---|
206 | \subsection{Binary Objects \label{binary-objects}}
|
---|
207 |
|
---|
208 | This class may be initialized from string data (which may include NULs).
|
---|
209 | The primary access to the content of a \class{Binary} object is
|
---|
210 | provided by an attribute:
|
---|
211 |
|
---|
212 | \begin{memberdesc}[Binary]{data}
|
---|
213 | The binary data encapsulated by the \class{Binary} instance. The data
|
---|
214 | is provided as an 8-bit string.
|
---|
215 | \end{memberdesc}
|
---|
216 |
|
---|
217 | \class{Binary} objects have the following methods, supported mainly
|
---|
218 | for internal use by the marshalling/unmarshalling code:
|
---|
219 |
|
---|
220 | \begin{methoddesc}[Binary]{decode}{string}
|
---|
221 | Accept a base64 string and decode it as the instance's new data.
|
---|
222 | \end{methoddesc}
|
---|
223 |
|
---|
224 | \begin{methoddesc}[Binary]{encode}{out}
|
---|
225 | Write the XML-RPC base 64 encoding of this binary item to the out
|
---|
226 | stream object.
|
---|
227 | \end{methoddesc}
|
---|
228 |
|
---|
229 | It also supports certain of Python's built-in operators through a
|
---|
230 | \method{__cmp__()} method.
|
---|
231 |
|
---|
232 |
|
---|
233 | \subsection{Fault Objects \label{fault-objects}}
|
---|
234 |
|
---|
235 | A \class{Fault} object encapsulates the content of an XML-RPC fault tag.
|
---|
236 | Fault objects have the following members:
|
---|
237 |
|
---|
238 | \begin{memberdesc}{faultCode}
|
---|
239 | A string indicating the fault type.
|
---|
240 | \end{memberdesc}
|
---|
241 |
|
---|
242 | \begin{memberdesc}{faultString}
|
---|
243 | A string containing a diagnostic message associated with the fault.
|
---|
244 | \end{memberdesc}
|
---|
245 |
|
---|
246 |
|
---|
247 | \subsection{ProtocolError Objects \label{protocol-error-objects}}
|
---|
248 |
|
---|
249 | A \class{ProtocolError} object describes a protocol error in the
|
---|
250 | underlying transport layer (such as a 404 `not found' error if the
|
---|
251 | server named by the URI does not exist). It has the following
|
---|
252 | members:
|
---|
253 |
|
---|
254 | \begin{memberdesc}{url}
|
---|
255 | The URI or URL that triggered the error.
|
---|
256 | \end{memberdesc}
|
---|
257 |
|
---|
258 | \begin{memberdesc}{errcode}
|
---|
259 | The error code.
|
---|
260 | \end{memberdesc}
|
---|
261 |
|
---|
262 | \begin{memberdesc}{errmsg}
|
---|
263 | The error message or diagnostic string.
|
---|
264 | \end{memberdesc}
|
---|
265 |
|
---|
266 | \begin{memberdesc}{headers}
|
---|
267 | A string containing the headers of the HTTP/HTTPS request that
|
---|
268 | triggered the error.
|
---|
269 | \end{memberdesc}
|
---|
270 |
|
---|
271 | \subsection{MultiCall Objects}
|
---|
272 |
|
---|
273 | \versionadded{2.4}
|
---|
274 |
|
---|
275 | In \url{http://www.xmlrpc.com/discuss/msgReader\%241208}, an approach
|
---|
276 | is presented to encapsulate multiple calls to a remote server into a
|
---|
277 | single request.
|
---|
278 |
|
---|
279 | \begin{classdesc}{MultiCall}{server}
|
---|
280 |
|
---|
281 | Create an object used to boxcar method calls. \var{server} is the
|
---|
282 | eventual target of the call. Calls can be made to the result object,
|
---|
283 | but they will immediately return \var{None}, and only store the
|
---|
284 | call name and parameters in the \class{MultiCall} object. Calling
|
---|
285 | the object itself causes all stored calls to be transmitted as
|
---|
286 | a single \code{system.multicall} request. The result of this call
|
---|
287 | is a generator; iterating over this generator yields the individual
|
---|
288 | results.
|
---|
289 |
|
---|
290 | \end{classdesc}
|
---|
291 |
|
---|
292 | A usage example of this class is
|
---|
293 |
|
---|
294 | \begin{verbatim}
|
---|
295 | multicall = MultiCall(server_proxy)
|
---|
296 | multicall.add(2,3)
|
---|
297 | multicall.get_address("Guido")
|
---|
298 | add_result, address = multicall()
|
---|
299 | \end{verbatim}
|
---|
300 |
|
---|
301 | \subsection{Convenience Functions}
|
---|
302 |
|
---|
303 | \begin{funcdesc}{boolean}{value}
|
---|
304 | Convert any Python value to one of the XML-RPC Boolean constants,
|
---|
305 | \code{True} or \code{False}.
|
---|
306 | \end{funcdesc}
|
---|
307 |
|
---|
308 | \begin{funcdesc}{dumps}{params\optional{, methodname\optional{,
|
---|
309 | methodresponse\optional{, encoding\optional{,
|
---|
310 | allow_none}}}}}
|
---|
311 | Convert \var{params} into an XML-RPC request.
|
---|
312 | or into a response if \var{methodresponse} is true.
|
---|
313 | \var{params} can be either a tuple of arguments or an instance of the
|
---|
314 | \exception{Fault} exception class. If \var{methodresponse} is true,
|
---|
315 | only a single value can be returned, meaning that \var{params} must be of length 1.
|
---|
316 | \var{encoding}, if supplied, is the encoding to use in the generated
|
---|
317 | XML; the default is UTF-8. Python's \constant{None} value cannot be
|
---|
318 | used in standard XML-RPC; to allow using it via an extension,
|
---|
319 | provide a true value for \var{allow_none}.
|
---|
320 | \end{funcdesc}
|
---|
321 |
|
---|
322 | \begin{funcdesc}{loads}{data\optional{, use_datetime}}
|
---|
323 | Convert an XML-RPC request or response into Python objects, a
|
---|
324 | \code{(\var{params}, \var{methodname})}. \var{params} is a tuple of argument; \var{methodname}
|
---|
325 | is a string, or \code{None} if no method name is present in the packet.
|
---|
326 | If the XML-RPC packet represents a fault condition, this
|
---|
327 | function will raise a \exception{Fault} exception.
|
---|
328 | The \var{use_datetime} flag can be used to cause date/time values to be
|
---|
329 | presented as \class{\refmodule{datetime}.datetime} objects; this is false
|
---|
330 | by default.
|
---|
331 | Note that even if you call an XML-RPC method with
|
---|
332 | \class{\refmodule{datetime}.date} or \class{\refmodule{datetime}.time}
|
---|
333 | objects, they are converted to \class{DateTime} objects internally, so only
|
---|
334 | {}\class{\refmodule{datetime}.datetime} objects will be returned.
|
---|
335 |
|
---|
336 | \versionchanged[The \var{use_datetime} flag was added]{2.5}
|
---|
337 | \end{funcdesc}
|
---|
338 |
|
---|
339 |
|
---|
340 |
|
---|
341 | \subsection{Example of Client Usage \label{xmlrpc-client-example}}
|
---|
342 |
|
---|
343 | \begin{verbatim}
|
---|
344 | # simple test program (from the XML-RPC specification)
|
---|
345 | from xmlrpclib import ServerProxy, Error
|
---|
346 |
|
---|
347 | # server = ServerProxy("http://localhost:8000") # local server
|
---|
348 | server = ServerProxy("http://betty.userland.com")
|
---|
349 |
|
---|
350 | print server
|
---|
351 |
|
---|
352 | try:
|
---|
353 | print server.examples.getStateName(41)
|
---|
354 | except Error, v:
|
---|
355 | print "ERROR", v
|
---|
356 | \end{verbatim}
|
---|
357 |
|
---|
358 | To access an XML-RPC server through a proxy, you need to define
|
---|
359 | a custom transport. The following example,
|
---|
360 | written by NoboNobo, % fill in original author's name if we ever learn it
|
---|
361 | shows how:
|
---|
362 |
|
---|
363 | % Example taken from http://lowlife.jp/nobonobo/wiki/xmlrpcwithproxy.html
|
---|
364 | \begin{verbatim}
|
---|
365 | import xmlrpclib, httplib
|
---|
366 |
|
---|
367 | class ProxiedTransport(xmlrpclib.Transport):
|
---|
368 | def set_proxy(self, proxy):
|
---|
369 | self.proxy = proxy
|
---|
370 | def make_connection(self, host):
|
---|
371 | self.realhost = host
|
---|
372 | h = httplib.HTTP(self.proxy)
|
---|
373 | return h
|
---|
374 | def send_request(self, connection, handler, request_body):
|
---|
375 | connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
|
---|
376 | def send_host(self, connection, host):
|
---|
377 | connection.putheader('Host', self.realhost)
|
---|
378 |
|
---|
379 | p = ProxiedTransport()
|
---|
380 | p.set_proxy('proxy-server:8080')
|
---|
381 | server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=p)
|
---|
382 | print server.currentTime.getCurrentTime()
|
---|
383 | \end{verbatim}
|
---|