blob: 6493fd4d93b1e89b34a0cdc2a92cd554ba97d6f1 [file] [log] [blame]
Georg Brandl38eceaa2008-05-26 11:14:17 +00001:mod:`xmlrpc.server` --- Basic XML-RPC servers
2==============================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
Georg Brandl38eceaa2008-05-26 11:14:17 +00004.. module:: xmlrpc.server
5 :synopsis: Basic XML-RPC server implementations.
Georg Brandl116aa622007-08-15 14:28:22 +00006.. moduleauthor:: Brian Quinlan <brianq@activestate.com>
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
Raymond Hettinger3029aff2011-02-10 08:09:36 +00009**Source code:** :source:`Lib/xmlrpc/server.py`
10
11--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl38eceaa2008-05-26 11:14:17 +000013The :mod:`xmlrpc.server` module provides a basic server framework for XML-RPC
14servers written in Python. Servers can either be free standing, using
Georg Brandl116aa622007-08-15 14:28:22 +000015:class:`SimpleXMLRPCServer`, or embedded in a CGI environment, using
16:class:`CGIXMLRPCRequestHandler`.
17
18
Florent Xicluna1b7458b2011-12-09 22:35:06 +010019.. class:: SimpleXMLRPCServer(addr, requestHandler=SimpleXMLRPCRequestHandler,\
20 logRequests=True, allow_none=False, encoding=None,\
21 bind_and_activate=True, use_builtin_types=False)
Georg Brandl116aa622007-08-15 14:28:22 +000022
23 Create a new server instance. This class provides methods for registration of
24 functions that can be called by the XML-RPC protocol. The *requestHandler*
25 parameter should be a factory for request handler instances; it defaults to
26 :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters
Alexandre Vassalottice261952008-05-12 02:31:37 +000027 are passed to the :class:`socketserver.TCPServer` constructor. If *logRequests*
Georg Brandl116aa622007-08-15 14:28:22 +000028 is true (the default), requests will be logged; setting this parameter to false
29 will turn off logging. The *allow_none* and *encoding* parameters are passed
Florent Xicluna1b7458b2011-12-09 22:35:06 +010030 on to :mod:`xmlrpc.client` and control the XML-RPC responses that will be returned
Georg Brandl116aa622007-08-15 14:28:22 +000031 from the server. The *bind_and_activate* parameter controls whether
32 :meth:`server_bind` and :meth:`server_activate` are called immediately by the
33 constructor; it defaults to true. Setting it to false allows code to manipulate
34 the *allow_reuse_address* class variable before the address is bound.
Florent Xicluna1b7458b2011-12-09 22:35:06 +010035 The *use_builtin_types* parameter is passed to the
36 :func:`~xmlrpc.client.loads` function and controls which types are processed
37 when date/times values or binary data are received; it defaults to false.
38
39 .. versionchanged:: 3.3
40 The *use_builtin_types* flag was added.
Georg Brandl116aa622007-08-15 14:28:22 +000041
Georg Brandl116aa622007-08-15 14:28:22 +000042
Florent Xicluna1b7458b2011-12-09 22:35:06 +010043.. class:: CGIXMLRPCRequestHandler(allow_none=False, encoding=None,\
44 use_builtin_types=False)
Georg Brandl116aa622007-08-15 14:28:22 +000045
46 Create a new instance to handle XML-RPC requests in a CGI environment. The
Georg Brandl38eceaa2008-05-26 11:14:17 +000047 *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpc.client`
48 and control the XML-RPC responses that will be returned from the server.
Florent Xicluna1b7458b2011-12-09 22:35:06 +010049 The *use_builtin_types* parameter is passed to the
50 :func:`~xmlrpc.client.loads` function and controls which types are processed
51 when date/times values or binary data are received; it defaults to false.
52
53 .. versionchanged:: 3.3
54 The *use_builtin_types* flag was added.
Georg Brandl116aa622007-08-15 14:28:22 +000055
Georg Brandl116aa622007-08-15 14:28:22 +000056
57.. class:: SimpleXMLRPCRequestHandler()
58
59 Create a new request handler instance. This request handler supports ``POST``
60 requests and modifies logging so that the *logRequests* parameter to the
61 :class:`SimpleXMLRPCServer` constructor parameter is honored.
62
63
64.. _simple-xmlrpc-servers:
65
66SimpleXMLRPCServer Objects
67--------------------------
68
69The :class:`SimpleXMLRPCServer` class is based on
Alexandre Vassalottice261952008-05-12 02:31:37 +000070:class:`socketserver.TCPServer` and provides a means of creating simple, stand
Georg Brandl116aa622007-08-15 14:28:22 +000071alone XML-RPC servers.
72
73
Georg Brandl7f01a132009-09-16 15:58:14 +000074.. method:: SimpleXMLRPCServer.register_function(function, name=None)
Georg Brandl116aa622007-08-15 14:28:22 +000075
76 Register a function that can respond to XML-RPC requests. If *name* is given,
77 it will be the method name associated with *function*, otherwise
78 ``function.__name__`` will be used. *name* can be either a normal or Unicode
79 string, and may contain characters not legal in Python identifiers, including
80 the period character.
81
82
Georg Brandl7f01a132009-09-16 15:58:14 +000083.. method:: SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False)
Georg Brandl116aa622007-08-15 14:28:22 +000084
85 Register an object which is used to expose method names which have not been
86 registered using :meth:`register_function`. If *instance* contains a
87 :meth:`_dispatch` method, it is called with the requested method name and the
88 parameters from the request. Its API is ``def _dispatch(self, method, params)``
89 (note that *params* does not represent a variable argument list). If it calls
90 an underlying function to perform its task, that function is called as
91 ``func(*params)``, expanding the parameter list. The return value from
92 :meth:`_dispatch` is returned to the client as the result. If *instance* does
93 not have a :meth:`_dispatch` method, it is searched for an attribute matching
94 the name of the requested method.
95
96 If the optional *allow_dotted_names* argument is true and the instance does not
97 have a :meth:`_dispatch` method, then if the requested method name contains
98 periods, each component of the method name is searched for individually, with
99 the effect that a simple hierarchical search is performed. The value found from
100 this search is then called with the parameters from the request, and the return
101 value is passed back to the client.
102
103 .. warning::
104
105 Enabling the *allow_dotted_names* option allows intruders to access your
106 module's global variables and may allow intruders to execute arbitrary code on
107 your machine. Only use this option on a secure, closed network.
108
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110.. method:: SimpleXMLRPCServer.register_introspection_functions()
111
112 Registers the XML-RPC introspection functions ``system.listMethods``,
113 ``system.methodHelp`` and ``system.methodSignature``.
114
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116.. method:: SimpleXMLRPCServer.register_multicall_functions()
117
118 Registers the XML-RPC multicall function system.multicall.
119
120
Christian Heimes8640e742008-02-23 16:23:06 +0000121.. attribute:: SimpleXMLRPCRequestHandler.rpc_paths
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123 An attribute value that must be a tuple listing valid path portions of the URL
124 for receiving XML-RPC requests. Requests posted to other paths will result in a
125 404 "no such page" HTTP error. If this tuple is empty, all paths will be
126 considered valid. The default value is ``('/', '/RPC2')``.
127
Georg Brandl116aa622007-08-15 14:28:22 +0000128
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000129.. _simplexmlrpcserver-example:
130
131SimpleXMLRPCServer Example
132^^^^^^^^^^^^^^^^^^^^^^^^^^
133Server code::
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Georg Brandl38eceaa2008-05-26 11:14:17 +0000135 from xmlrpc.server import SimpleXMLRPCServer
136 from xmlrpc.server import SimpleXMLRPCRequestHandler
Christian Heimes8640e742008-02-23 16:23:06 +0000137
138 # Restrict to a particular path.
139 class RequestHandler(SimpleXMLRPCRequestHandler):
140 rpc_paths = ('/RPC2',)
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142 # Create server
Christian Heimes8640e742008-02-23 16:23:06 +0000143 server = SimpleXMLRPCServer(("localhost", 8000),
144 requestHandler=RequestHandler)
Georg Brandl116aa622007-08-15 14:28:22 +0000145 server.register_introspection_functions()
146
Georg Brandl48310cd2009-01-03 21:18:54 +0000147 # Register pow() function; this will use the value of
Georg Brandl116aa622007-08-15 14:28:22 +0000148 # pow.__name__ as the name, which is just 'pow'.
149 server.register_function(pow)
150
151 # Register a function under a different name
152 def adder_function(x,y):
153 return x + y
154 server.register_function(adder_function, 'add')
155
Georg Brandl48310cd2009-01-03 21:18:54 +0000156 # Register an instance; all the methods of the instance are
Georg Brandl167543c2010-01-30 17:54:04 +0000157 # published as XML-RPC methods (in this case, just 'mul').
Georg Brandl116aa622007-08-15 14:28:22 +0000158 class MyFuncs:
Georg Brandl167543c2010-01-30 17:54:04 +0000159 def mul(self, x, y):
160 return x * y
Georg Brandl116aa622007-08-15 14:28:22 +0000161
162 server.register_instance(MyFuncs())
163
164 # Run the server's main loop
165 server.serve_forever()
166
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000167The following client code will call the methods made available by the preceding
Georg Brandl116aa622007-08-15 14:28:22 +0000168server::
169
Georg Brandl38eceaa2008-05-26 11:14:17 +0000170 import xmlrpc.client
Georg Brandl116aa622007-08-15 14:28:22 +0000171
Georg Brandl38eceaa2008-05-26 11:14:17 +0000172 s = xmlrpc.client.ServerProxy('http://localhost:8000')
Georg Brandl6911e3c2007-09-04 07:15:32 +0000173 print(s.pow(2,3)) # Returns 2**3 = 8
174 print(s.add(2,3)) # Returns 5
Georg Brandlf6945182008-02-01 11:56:49 +0000175 print(s.mul(5,2)) # Returns 5*2 = 10
Georg Brandl116aa622007-08-15 14:28:22 +0000176
177 # Print list of available methods
Georg Brandl6911e3c2007-09-04 07:15:32 +0000178 print(s.system.listMethods())
Georg Brandl116aa622007-08-15 14:28:22 +0000179
180
181CGIXMLRPCRequestHandler
182-----------------------
183
184The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
185requests sent to Python CGI scripts.
186
187
Georg Brandl7f01a132009-09-16 15:58:14 +0000188.. method:: CGIXMLRPCRequestHandler.register_function(function, name=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000189
190 Register a function that can respond to XML-RPC requests. If *name* is given,
191 it will be the method name associated with function, otherwise
192 *function.__name__* will be used. *name* can be either a normal or Unicode
193 string, and may contain characters not legal in Python identifiers, including
194 the period character.
195
196
197.. method:: CGIXMLRPCRequestHandler.register_instance(instance)
198
199 Register an object which is used to expose method names which have not been
200 registered using :meth:`register_function`. If instance contains a
201 :meth:`_dispatch` method, it is called with the requested method name and the
202 parameters from the request; the return value is returned to the client as the
203 result. If instance does not have a :meth:`_dispatch` method, it is searched
204 for an attribute matching the name of the requested method; if the requested
205 method name contains periods, each component of the method name is searched for
206 individually, with the effect that a simple hierarchical search is performed.
207 The value found from this search is then called with the parameters from the
208 request, and the return value is passed back to the client.
209
210
211.. method:: CGIXMLRPCRequestHandler.register_introspection_functions()
212
213 Register the XML-RPC introspection functions ``system.listMethods``,
214 ``system.methodHelp`` and ``system.methodSignature``.
215
216
217.. method:: CGIXMLRPCRequestHandler.register_multicall_functions()
218
219 Register the XML-RPC multicall function ``system.multicall``.
220
221
Georg Brandl7f01a132009-09-16 15:58:14 +0000222.. method:: CGIXMLRPCRequestHandler.handle_request(request_text=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224 Handle a XML-RPC request. If *request_text* is given, it should be the POST
225 data provided by the HTTP server, otherwise the contents of stdin will be used.
226
227Example::
228
229 class MyFuncs:
Georg Brandl167543c2010-01-30 17:54:04 +0000230 def mul(self, x, y):
231 return x * y
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233
234 handler = CGIXMLRPCRequestHandler()
235 handler.register_function(pow)
236 handler.register_function(lambda x,y: x+y, 'add')
237 handler.register_introspection_functions()
238 handler.register_instance(MyFuncs())
239 handler.handle_request()
240
Georg Brandl38eceaa2008-05-26 11:14:17 +0000241
242Documenting XMLRPC server
243-------------------------
244
245These classes extend the above classes to serve HTML documentation in response
246to HTTP GET requests. Servers can either be free standing, using
247:class:`DocXMLRPCServer`, or embedded in a CGI environment, using
248:class:`DocCGIXMLRPCRequestHandler`.
249
250
Florent Xicluna1b7458b2011-12-09 22:35:06 +0100251.. class:: DocXMLRPCServer(addr, requestHandler=DocXMLRPCRequestHandler,\
252 logRequests=True, allow_none=False, encoding=None,\
253 bind_and_activate=True, use_builtin_types=True)
Georg Brandl38eceaa2008-05-26 11:14:17 +0000254
255 Create a new server instance. All parameters have the same meaning as for
256 :class:`SimpleXMLRPCServer`; *requestHandler* defaults to
257 :class:`DocXMLRPCRequestHandler`.
258
Florent Xicluna1b7458b2011-12-09 22:35:06 +0100259 .. versionchanged:: 3.3
260 The *use_builtin_types* flag was added.
261
Georg Brandl38eceaa2008-05-26 11:14:17 +0000262
263.. class:: DocCGIXMLRPCRequestHandler()
264
265 Create a new instance to handle XML-RPC requests in a CGI environment.
266
267
268.. class:: DocXMLRPCRequestHandler()
269
270 Create a new request handler instance. This request handler supports XML-RPC
271 POST requests, documentation GET requests, and modifies logging so that the
272 *logRequests* parameter to the :class:`DocXMLRPCServer` constructor parameter is
273 honored.
274
275
276.. _doc-xmlrpc-servers:
277
278DocXMLRPCServer Objects
279-----------------------
280
281The :class:`DocXMLRPCServer` class is derived from :class:`SimpleXMLRPCServer`
282and provides a means of creating self-documenting, stand alone XML-RPC
283servers. HTTP POST requests are handled as XML-RPC method calls. HTTP GET
284requests are handled by generating pydoc-style HTML documentation. This allows a
285server to provide its own web-based documentation.
286
287
288.. method:: DocXMLRPCServer.set_server_title(server_title)
289
290 Set the title used in the generated HTML documentation. This title will be used
291 inside the HTML "title" element.
292
293
294.. method:: DocXMLRPCServer.set_server_name(server_name)
295
296 Set the name used in the generated HTML documentation. This name will appear at
297 the top of the generated documentation inside a "h1" element.
298
299
300.. method:: DocXMLRPCServer.set_server_documentation(server_documentation)
301
302 Set the description used in the generated HTML documentation. This description
303 will appear as a paragraph, below the server name, in the documentation.
304
305
306DocCGIXMLRPCRequestHandler
307--------------------------
308
309The :class:`DocCGIXMLRPCRequestHandler` class is derived from
310:class:`CGIXMLRPCRequestHandler` and provides a means of creating
311self-documenting, XML-RPC CGI scripts. HTTP POST requests are handled as XML-RPC
312method calls. HTTP GET requests are handled by generating pydoc-style HTML
313documentation. This allows a server to provide its own web-based documentation.
314
315
316.. method:: DocCGIXMLRPCRequestHandler.set_server_title(server_title)
317
318 Set the title used in the generated HTML documentation. This title will be used
319 inside the HTML "title" element.
320
321
322.. method:: DocCGIXMLRPCRequestHandler.set_server_name(server_name)
323
324 Set the name used in the generated HTML documentation. This name will appear at
325 the top of the generated documentation inside a "h1" element.
326
327
328.. method:: DocCGIXMLRPCRequestHandler.set_server_documentation(server_documentation)
329
330 Set the description used in the generated HTML documentation. This description
331 will appear as a paragraph, below the server name, in the documentation.