blob: 1c77e84c9b48616db4cfb8c8f2702800680eda3b [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Brian Quinlan <brianq@activestate.com>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
Raymond Hettinger3029aff2011-02-10 08:09:36 +000010**Source code:** :source:`Lib/xmlrpc/server.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandl38eceaa2008-05-26 11:14:17 +000014The :mod:`xmlrpc.server` module provides a basic server framework for XML-RPC
15servers written in Python. Servers can either be free standing, using
Georg Brandl116aa622007-08-15 14:28:22 +000016:class:`SimpleXMLRPCServer`, or embedded in a CGI environment, using
17:class:`CGIXMLRPCRequestHandler`.
18
19
Christian Heimes7380a672013-03-26 17:35:55 +010020.. warning::
21
Georg Brandl32fbd3a2016-02-26 19:16:40 +010022 The :mod:`xmlrpc.server` module is not secure against maliciously
Christian Heimes7380a672013-03-26 17:35:55 +010023 constructed data. If you need to parse untrusted or unauthenticated data see
24 :ref:`xml-vulnerabilities`.
25
26
Florent Xicluna1b7458b2011-12-09 22:35:06 +010027.. class:: SimpleXMLRPCServer(addr, requestHandler=SimpleXMLRPCRequestHandler,\
28 logRequests=True, allow_none=False, encoding=None,\
29 bind_and_activate=True, use_builtin_types=False)
Georg Brandl116aa622007-08-15 14:28:22 +000030
31 Create a new server instance. This class provides methods for registration of
32 functions that can be called by the XML-RPC protocol. The *requestHandler*
33 parameter should be a factory for request handler instances; it defaults to
34 :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters
Alexandre Vassalottice261952008-05-12 02:31:37 +000035 are passed to the :class:`socketserver.TCPServer` constructor. If *logRequests*
Georg Brandl116aa622007-08-15 14:28:22 +000036 is true (the default), requests will be logged; setting this parameter to false
37 will turn off logging. The *allow_none* and *encoding* parameters are passed
Florent Xicluna1b7458b2011-12-09 22:35:06 +010038 on to :mod:`xmlrpc.client` and control the XML-RPC responses that will be returned
Georg Brandl116aa622007-08-15 14:28:22 +000039 from the server. The *bind_and_activate* parameter controls whether
40 :meth:`server_bind` and :meth:`server_activate` are called immediately by the
41 constructor; it defaults to true. Setting it to false allows code to manipulate
42 the *allow_reuse_address* class variable before the address is bound.
Florent Xicluna1b7458b2011-12-09 22:35:06 +010043 The *use_builtin_types* parameter is passed to the
44 :func:`~xmlrpc.client.loads` function and controls which types are processed
45 when date/times values or binary data are received; it defaults to false.
46
47 .. versionchanged:: 3.3
48 The *use_builtin_types* flag was added.
Georg Brandl116aa622007-08-15 14:28:22 +000049
Georg Brandl116aa622007-08-15 14:28:22 +000050
Florent Xicluna1b7458b2011-12-09 22:35:06 +010051.. class:: CGIXMLRPCRequestHandler(allow_none=False, encoding=None,\
52 use_builtin_types=False)
Georg Brandl116aa622007-08-15 14:28:22 +000053
54 Create a new instance to handle XML-RPC requests in a CGI environment. The
Georg Brandl38eceaa2008-05-26 11:14:17 +000055 *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpc.client`
56 and control the XML-RPC responses that will be returned from the server.
Florent Xicluna1b7458b2011-12-09 22:35:06 +010057 The *use_builtin_types* parameter is passed to the
58 :func:`~xmlrpc.client.loads` function and controls which types are processed
59 when date/times values or binary data are received; it defaults to false.
60
61 .. versionchanged:: 3.3
62 The *use_builtin_types* flag was added.
Georg Brandl116aa622007-08-15 14:28:22 +000063
Georg Brandl116aa622007-08-15 14:28:22 +000064
65.. class:: SimpleXMLRPCRequestHandler()
66
67 Create a new request handler instance. This request handler supports ``POST``
68 requests and modifies logging so that the *logRequests* parameter to the
69 :class:`SimpleXMLRPCServer` constructor parameter is honored.
70
71
72.. _simple-xmlrpc-servers:
73
74SimpleXMLRPCServer Objects
75--------------------------
76
77The :class:`SimpleXMLRPCServer` class is based on
Alexandre Vassalottice261952008-05-12 02:31:37 +000078:class:`socketserver.TCPServer` and provides a means of creating simple, stand
Georg Brandl116aa622007-08-15 14:28:22 +000079alone XML-RPC servers.
80
81
Georg Brandl7f01a132009-09-16 15:58:14 +000082.. method:: SimpleXMLRPCServer.register_function(function, name=None)
Georg Brandl116aa622007-08-15 14:28:22 +000083
84 Register a function that can respond to XML-RPC requests. If *name* is given,
85 it will be the method name associated with *function*, otherwise
86 ``function.__name__`` will be used. *name* can be either a normal or Unicode
87 string, and may contain characters not legal in Python identifiers, including
88 the period character.
89
90
Georg Brandl7f01a132009-09-16 15:58:14 +000091.. method:: SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False)
Georg Brandl116aa622007-08-15 14:28:22 +000092
93 Register an object which is used to expose method names which have not been
94 registered using :meth:`register_function`. If *instance* contains a
95 :meth:`_dispatch` method, it is called with the requested method name and the
96 parameters from the request. Its API is ``def _dispatch(self, method, params)``
97 (note that *params* does not represent a variable argument list). If it calls
98 an underlying function to perform its task, that function is called as
99 ``func(*params)``, expanding the parameter list. The return value from
100 :meth:`_dispatch` is returned to the client as the result. If *instance* does
101 not have a :meth:`_dispatch` method, it is searched for an attribute matching
102 the name of the requested method.
103
104 If the optional *allow_dotted_names* argument is true and the instance does not
105 have a :meth:`_dispatch` method, then if the requested method name contains
106 periods, each component of the method name is searched for individually, with
107 the effect that a simple hierarchical search is performed. The value found from
108 this search is then called with the parameters from the request, and the return
109 value is passed back to the client.
110
111 .. warning::
112
113 Enabling the *allow_dotted_names* option allows intruders to access your
114 module's global variables and may allow intruders to execute arbitrary code on
115 your machine. Only use this option on a secure, closed network.
116
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118.. method:: SimpleXMLRPCServer.register_introspection_functions()
119
120 Registers the XML-RPC introspection functions ``system.listMethods``,
121 ``system.methodHelp`` and ``system.methodSignature``.
122
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124.. method:: SimpleXMLRPCServer.register_multicall_functions()
125
126 Registers the XML-RPC multicall function system.multicall.
127
128
Christian Heimes8640e742008-02-23 16:23:06 +0000129.. attribute:: SimpleXMLRPCRequestHandler.rpc_paths
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131 An attribute value that must be a tuple listing valid path portions of the URL
132 for receiving XML-RPC requests. Requests posted to other paths will result in a
133 404 "no such page" HTTP error. If this tuple is empty, all paths will be
134 considered valid. The default value is ``('/', '/RPC2')``.
135
Georg Brandl116aa622007-08-15 14:28:22 +0000136
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000137.. _simplexmlrpcserver-example:
138
139SimpleXMLRPCServer Example
140^^^^^^^^^^^^^^^^^^^^^^^^^^
141Server code::
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Georg Brandl38eceaa2008-05-26 11:14:17 +0000143 from xmlrpc.server import SimpleXMLRPCServer
144 from xmlrpc.server import SimpleXMLRPCRequestHandler
Christian Heimes8640e742008-02-23 16:23:06 +0000145
146 # Restrict to a particular path.
147 class RequestHandler(SimpleXMLRPCRequestHandler):
148 rpc_paths = ('/RPC2',)
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150 # Create server
Christian Heimes8640e742008-02-23 16:23:06 +0000151 server = SimpleXMLRPCServer(("localhost", 8000),
152 requestHandler=RequestHandler)
Georg Brandl116aa622007-08-15 14:28:22 +0000153 server.register_introspection_functions()
154
Georg Brandl48310cd2009-01-03 21:18:54 +0000155 # Register pow() function; this will use the value of
Georg Brandl116aa622007-08-15 14:28:22 +0000156 # pow.__name__ as the name, which is just 'pow'.
157 server.register_function(pow)
158
159 # Register a function under a different name
160 def adder_function(x,y):
161 return x + y
162 server.register_function(adder_function, 'add')
163
Georg Brandl48310cd2009-01-03 21:18:54 +0000164 # Register an instance; all the methods of the instance are
Georg Brandl167543c2010-01-30 17:54:04 +0000165 # published as XML-RPC methods (in this case, just 'mul').
Georg Brandl116aa622007-08-15 14:28:22 +0000166 class MyFuncs:
Georg Brandl167543c2010-01-30 17:54:04 +0000167 def mul(self, x, y):
168 return x * y
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170 server.register_instance(MyFuncs())
171
172 # Run the server's main loop
173 server.serve_forever()
174
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000175The following client code will call the methods made available by the preceding
Georg Brandl116aa622007-08-15 14:28:22 +0000176server::
177
Georg Brandl38eceaa2008-05-26 11:14:17 +0000178 import xmlrpc.client
Georg Brandl116aa622007-08-15 14:28:22 +0000179
Georg Brandl38eceaa2008-05-26 11:14:17 +0000180 s = xmlrpc.client.ServerProxy('http://localhost:8000')
Georg Brandl6911e3c2007-09-04 07:15:32 +0000181 print(s.pow(2,3)) # Returns 2**3 = 8
182 print(s.add(2,3)) # Returns 5
Georg Brandlf6945182008-02-01 11:56:49 +0000183 print(s.mul(5,2)) # Returns 5*2 = 10
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185 # Print list of available methods
Georg Brandl6911e3c2007-09-04 07:15:32 +0000186 print(s.system.listMethods())
Georg Brandl116aa622007-08-15 14:28:22 +0000187
Georg Brandl93a56cd2014-10-30 22:25:41 +0100188The following example included in the :file:`Lib/xmlrpc/server.py` module shows
189a server allowing dotted names and registering a multicall function.
Senthil Kumaran939e2db2014-01-12 16:06:58 -0800190
191.. warning::
192
193 Enabling the *allow_dotted_names* option allows intruders to access your
194 module's global variables and may allow intruders to execute arbitrary code on
195 your machine. Only use this example only within a secure, closed network.
196
197::
198
199 import datetime
200
201 class ExampleService:
202 def getData(self):
203 return '42'
204
205 class currentTime:
206 @staticmethod
207 def getCurrentTime():
208 return datetime.datetime.now()
209
210 server = SimpleXMLRPCServer(("localhost", 8000))
211 server.register_function(pow)
212 server.register_function(lambda x,y: x+y, 'add')
213 server.register_instance(ExampleService(), allow_dotted_names=True)
214 server.register_multicall_functions()
215 print('Serving XML-RPC on localhost port 8000')
216 try:
217 server.serve_forever()
218 except KeyboardInterrupt:
219 print("\nKeyboard interrupt received, exiting.")
220 server.server_close()
221 sys.exit(0)
222
223This ExampleService demo can be invoked from the command line::
224
225 python -m xmlrpc.server
226
227
228The client that interacts with the above server is included in
229`Lib/xmlrpc/client.py`::
230
231 server = ServerProxy("http://localhost:8000")
232
233 try:
234 print(server.currentTime.getCurrentTime())
235 except Error as v:
236 print("ERROR", v)
237
238 multi = MultiCall(server)
239 multi.getData()
240 multi.pow(2,9)
241 multi.add(1,2)
242 try:
243 for response in multi():
244 print(response)
245 except Error as v:
246 print("ERROR", v)
247
248This client which interacts with the demo XMLRPC server can be invoked as::
249
250 python -m xmlrpc.client
251
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253CGIXMLRPCRequestHandler
254-----------------------
255
256The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
257requests sent to Python CGI scripts.
258
259
Georg Brandl7f01a132009-09-16 15:58:14 +0000260.. method:: CGIXMLRPCRequestHandler.register_function(function, name=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262 Register a function that can respond to XML-RPC requests. If *name* is given,
263 it will be the method name associated with function, otherwise
264 *function.__name__* will be used. *name* can be either a normal or Unicode
265 string, and may contain characters not legal in Python identifiers, including
266 the period character.
267
268
269.. method:: CGIXMLRPCRequestHandler.register_instance(instance)
270
271 Register an object which is used to expose method names which have not been
272 registered using :meth:`register_function`. If instance contains a
273 :meth:`_dispatch` method, it is called with the requested method name and the
274 parameters from the request; the return value is returned to the client as the
275 result. If instance does not have a :meth:`_dispatch` method, it is searched
276 for an attribute matching the name of the requested method; if the requested
277 method name contains periods, each component of the method name is searched for
278 individually, with the effect that a simple hierarchical search is performed.
279 The value found from this search is then called with the parameters from the
280 request, and the return value is passed back to the client.
281
282
283.. method:: CGIXMLRPCRequestHandler.register_introspection_functions()
284
285 Register the XML-RPC introspection functions ``system.listMethods``,
286 ``system.methodHelp`` and ``system.methodSignature``.
287
288
289.. method:: CGIXMLRPCRequestHandler.register_multicall_functions()
290
291 Register the XML-RPC multicall function ``system.multicall``.
292
293
Georg Brandl7f01a132009-09-16 15:58:14 +0000294.. method:: CGIXMLRPCRequestHandler.handle_request(request_text=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000295
Martin Panter204bf0b2016-07-11 07:51:37 +0000296 Handle an XML-RPC request. If *request_text* is given, it should be the POST
Georg Brandl116aa622007-08-15 14:28:22 +0000297 data provided by the HTTP server, otherwise the contents of stdin will be used.
298
299Example::
300
301 class MyFuncs:
Georg Brandl167543c2010-01-30 17:54:04 +0000302 def mul(self, x, y):
303 return x * y
Georg Brandl116aa622007-08-15 14:28:22 +0000304
305
306 handler = CGIXMLRPCRequestHandler()
307 handler.register_function(pow)
308 handler.register_function(lambda x,y: x+y, 'add')
309 handler.register_introspection_functions()
310 handler.register_instance(MyFuncs())
311 handler.handle_request()
312
Georg Brandl38eceaa2008-05-26 11:14:17 +0000313
314Documenting XMLRPC server
315-------------------------
316
317These classes extend the above classes to serve HTML documentation in response
318to HTTP GET requests. Servers can either be free standing, using
319:class:`DocXMLRPCServer`, or embedded in a CGI environment, using
320:class:`DocCGIXMLRPCRequestHandler`.
321
322
Florent Xicluna1b7458b2011-12-09 22:35:06 +0100323.. class:: DocXMLRPCServer(addr, requestHandler=DocXMLRPCRequestHandler,\
324 logRequests=True, allow_none=False, encoding=None,\
325 bind_and_activate=True, use_builtin_types=True)
Georg Brandl38eceaa2008-05-26 11:14:17 +0000326
327 Create a new server instance. All parameters have the same meaning as for
328 :class:`SimpleXMLRPCServer`; *requestHandler* defaults to
329 :class:`DocXMLRPCRequestHandler`.
330
Florent Xicluna1b7458b2011-12-09 22:35:06 +0100331 .. versionchanged:: 3.3
332 The *use_builtin_types* flag was added.
333
Georg Brandl38eceaa2008-05-26 11:14:17 +0000334
335.. class:: DocCGIXMLRPCRequestHandler()
336
337 Create a new instance to handle XML-RPC requests in a CGI environment.
338
339
340.. class:: DocXMLRPCRequestHandler()
341
342 Create a new request handler instance. This request handler supports XML-RPC
343 POST requests, documentation GET requests, and modifies logging so that the
344 *logRequests* parameter to the :class:`DocXMLRPCServer` constructor parameter is
345 honored.
346
347
348.. _doc-xmlrpc-servers:
349
350DocXMLRPCServer Objects
351-----------------------
352
353The :class:`DocXMLRPCServer` class is derived from :class:`SimpleXMLRPCServer`
354and provides a means of creating self-documenting, stand alone XML-RPC
355servers. HTTP POST requests are handled as XML-RPC method calls. HTTP GET
356requests are handled by generating pydoc-style HTML documentation. This allows a
357server to provide its own web-based documentation.
358
359
360.. method:: DocXMLRPCServer.set_server_title(server_title)
361
362 Set the title used in the generated HTML documentation. This title will be used
363 inside the HTML "title" element.
364
365
366.. method:: DocXMLRPCServer.set_server_name(server_name)
367
368 Set the name used in the generated HTML documentation. This name will appear at
369 the top of the generated documentation inside a "h1" element.
370
371
372.. method:: DocXMLRPCServer.set_server_documentation(server_documentation)
373
374 Set the description used in the generated HTML documentation. This description
375 will appear as a paragraph, below the server name, in the documentation.
376
377
378DocCGIXMLRPCRequestHandler
379--------------------------
380
381The :class:`DocCGIXMLRPCRequestHandler` class is derived from
382:class:`CGIXMLRPCRequestHandler` and provides a means of creating
383self-documenting, XML-RPC CGI scripts. HTTP POST requests are handled as XML-RPC
384method calls. HTTP GET requests are handled by generating pydoc-style HTML
385documentation. This allows a server to provide its own web-based documentation.
386
387
388.. method:: DocCGIXMLRPCRequestHandler.set_server_title(server_title)
389
390 Set the title used in the generated HTML documentation. This title will be used
391 inside the HTML "title" element.
392
393
394.. method:: DocCGIXMLRPCRequestHandler.set_server_name(server_name)
395
396 Set the name used in the generated HTML documentation. This name will appear at
397 the top of the generated documentation inside a "h1" element.
398
399
400.. method:: DocCGIXMLRPCRequestHandler.set_server_documentation(server_documentation)
401
402 Set the description used in the generated HTML documentation. This description
403 will appear as a paragraph, below the server name, in the documentation.