blob: ca80aab2a84c67360fb4c992c0eb0262095e585b [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
Christian Heimes7380a672013-03-26 17:35:55 +010019.. warning::
20
Georg Brandl32fbd3a2016-02-26 19:16:40 +010021 The :mod:`xmlrpc.server` module is not secure against maliciously
Christian Heimes7380a672013-03-26 17:35:55 +010022 constructed data. If you need to parse untrusted or unauthenticated data see
23 :ref:`xml-vulnerabilities`.
24
25
Florent Xicluna1b7458b2011-12-09 22:35:06 +010026.. class:: SimpleXMLRPCServer(addr, requestHandler=SimpleXMLRPCRequestHandler,\
27 logRequests=True, allow_none=False, encoding=None,\
28 bind_and_activate=True, use_builtin_types=False)
Georg Brandl116aa622007-08-15 14:28:22 +000029
30 Create a new server instance. This class provides methods for registration of
31 functions that can be called by the XML-RPC protocol. The *requestHandler*
32 parameter should be a factory for request handler instances; it defaults to
33 :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters
Alexandre Vassalottice261952008-05-12 02:31:37 +000034 are passed to the :class:`socketserver.TCPServer` constructor. If *logRequests*
Georg Brandl116aa622007-08-15 14:28:22 +000035 is true (the default), requests will be logged; setting this parameter to false
36 will turn off logging. The *allow_none* and *encoding* parameters are passed
Florent Xicluna1b7458b2011-12-09 22:35:06 +010037 on to :mod:`xmlrpc.client` and control the XML-RPC responses that will be returned
Georg Brandl116aa622007-08-15 14:28:22 +000038 from the server. The *bind_and_activate* parameter controls whether
39 :meth:`server_bind` and :meth:`server_activate` are called immediately by the
40 constructor; it defaults to true. Setting it to false allows code to manipulate
41 the *allow_reuse_address* class variable before the address is bound.
Florent Xicluna1b7458b2011-12-09 22:35:06 +010042 The *use_builtin_types* parameter is passed to the
43 :func:`~xmlrpc.client.loads` function and controls which types are processed
44 when date/times values or binary data are received; it defaults to false.
45
46 .. versionchanged:: 3.3
47 The *use_builtin_types* flag was added.
Georg Brandl116aa622007-08-15 14:28:22 +000048
Georg Brandl116aa622007-08-15 14:28:22 +000049
Florent Xicluna1b7458b2011-12-09 22:35:06 +010050.. class:: CGIXMLRPCRequestHandler(allow_none=False, encoding=None,\
51 use_builtin_types=False)
Georg Brandl116aa622007-08-15 14:28:22 +000052
53 Create a new instance to handle XML-RPC requests in a CGI environment. The
Georg Brandl38eceaa2008-05-26 11:14:17 +000054 *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpc.client`
55 and control the XML-RPC responses that will be returned from the server.
Florent Xicluna1b7458b2011-12-09 22:35:06 +010056 The *use_builtin_types* parameter is passed to the
57 :func:`~xmlrpc.client.loads` function and controls which types are processed
58 when date/times values or binary data are received; it defaults to false.
59
60 .. versionchanged:: 3.3
61 The *use_builtin_types* flag was added.
Georg Brandl116aa622007-08-15 14:28:22 +000062
Georg Brandl116aa622007-08-15 14:28:22 +000063
64.. class:: SimpleXMLRPCRequestHandler()
65
66 Create a new request handler instance. This request handler supports ``POST``
67 requests and modifies logging so that the *logRequests* parameter to the
68 :class:`SimpleXMLRPCServer` constructor parameter is honored.
69
70
71.. _simple-xmlrpc-servers:
72
73SimpleXMLRPCServer Objects
74--------------------------
75
76The :class:`SimpleXMLRPCServer` class is based on
Alexandre Vassalottice261952008-05-12 02:31:37 +000077:class:`socketserver.TCPServer` and provides a means of creating simple, stand
Georg Brandl116aa622007-08-15 14:28:22 +000078alone XML-RPC servers.
79
80
Georg Brandl7f01a132009-09-16 15:58:14 +000081.. method:: SimpleXMLRPCServer.register_function(function, name=None)
Georg Brandl116aa622007-08-15 14:28:22 +000082
83 Register a function that can respond to XML-RPC requests. If *name* is given,
84 it will be the method name associated with *function*, otherwise
85 ``function.__name__`` will be used. *name* can be either a normal or Unicode
86 string, and may contain characters not legal in Python identifiers, including
87 the period character.
88
89
Georg Brandl7f01a132009-09-16 15:58:14 +000090.. method:: SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False)
Georg Brandl116aa622007-08-15 14:28:22 +000091
92 Register an object which is used to expose method names which have not been
93 registered using :meth:`register_function`. If *instance* contains a
94 :meth:`_dispatch` method, it is called with the requested method name and the
95 parameters from the request. Its API is ``def _dispatch(self, method, params)``
96 (note that *params* does not represent a variable argument list). If it calls
97 an underlying function to perform its task, that function is called as
98 ``func(*params)``, expanding the parameter list. The return value from
99 :meth:`_dispatch` is returned to the client as the result. If *instance* does
100 not have a :meth:`_dispatch` method, it is searched for an attribute matching
101 the name of the requested method.
102
103 If the optional *allow_dotted_names* argument is true and the instance does not
104 have a :meth:`_dispatch` method, then if the requested method name contains
105 periods, each component of the method name is searched for individually, with
106 the effect that a simple hierarchical search is performed. The value found from
107 this search is then called with the parameters from the request, and the return
108 value is passed back to the client.
109
110 .. warning::
111
112 Enabling the *allow_dotted_names* option allows intruders to access your
113 module's global variables and may allow intruders to execute arbitrary code on
114 your machine. Only use this option on a secure, closed network.
115
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117.. method:: SimpleXMLRPCServer.register_introspection_functions()
118
119 Registers the XML-RPC introspection functions ``system.listMethods``,
120 ``system.methodHelp`` and ``system.methodSignature``.
121
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123.. method:: SimpleXMLRPCServer.register_multicall_functions()
124
125 Registers the XML-RPC multicall function system.multicall.
126
127
Christian Heimes8640e742008-02-23 16:23:06 +0000128.. attribute:: SimpleXMLRPCRequestHandler.rpc_paths
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130 An attribute value that must be a tuple listing valid path portions of the URL
131 for receiving XML-RPC requests. Requests posted to other paths will result in a
132 404 "no such page" HTTP error. If this tuple is empty, all paths will be
133 considered valid. The default value is ``('/', '/RPC2')``.
134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000136.. _simplexmlrpcserver-example:
137
138SimpleXMLRPCServer Example
139^^^^^^^^^^^^^^^^^^^^^^^^^^
140Server code::
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Georg Brandl38eceaa2008-05-26 11:14:17 +0000142 from xmlrpc.server import SimpleXMLRPCServer
143 from xmlrpc.server import SimpleXMLRPCRequestHandler
Christian Heimes8640e742008-02-23 16:23:06 +0000144
145 # Restrict to a particular path.
146 class RequestHandler(SimpleXMLRPCRequestHandler):
147 rpc_paths = ('/RPC2',)
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149 # Create server
Martin Panter0cab9c12016-04-13 00:36:52 +0000150 with SimpleXMLRPCServer(("localhost", 8000),
151 requestHandler=RequestHandler) as server:
152 server.register_introspection_functions()
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Martin Panter0cab9c12016-04-13 00:36:52 +0000154 # Register pow() function; this will use the value of
155 # pow.__name__ as the name, which is just 'pow'.
156 server.register_function(pow)
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Martin Panter0cab9c12016-04-13 00:36:52 +0000158 # Register a function under a different name
159 def adder_function(x,y):
160 return x + y
161 server.register_function(adder_function, 'add')
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Martin Panter0cab9c12016-04-13 00:36:52 +0000163 # Register an instance; all the methods of the instance are
164 # published as XML-RPC methods (in this case, just 'mul').
165 class MyFuncs:
166 def mul(self, x, y):
167 return x * y
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Martin Panter0cab9c12016-04-13 00:36:52 +0000169 server.register_instance(MyFuncs())
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Martin Panter0cab9c12016-04-13 00:36:52 +0000171 # Run the server's main loop
172 server.serve_forever()
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000174The following client code will call the methods made available by the preceding
Georg Brandl116aa622007-08-15 14:28:22 +0000175server::
176
Georg Brandl38eceaa2008-05-26 11:14:17 +0000177 import xmlrpc.client
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Georg Brandl38eceaa2008-05-26 11:14:17 +0000179 s = xmlrpc.client.ServerProxy('http://localhost:8000')
Georg Brandl6911e3c2007-09-04 07:15:32 +0000180 print(s.pow(2,3)) # Returns 2**3 = 8
181 print(s.add(2,3)) # Returns 5
Georg Brandlf6945182008-02-01 11:56:49 +0000182 print(s.mul(5,2)) # Returns 5*2 = 10
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184 # Print list of available methods
Georg Brandl6911e3c2007-09-04 07:15:32 +0000185 print(s.system.listMethods())
Georg Brandl116aa622007-08-15 14:28:22 +0000186
Georg Brandl93a56cd2014-10-30 22:25:41 +0100187The following example included in the :file:`Lib/xmlrpc/server.py` module shows
188a server allowing dotted names and registering a multicall function.
Senthil Kumaran939e2db2014-01-12 16:06:58 -0800189
190.. warning::
191
192 Enabling the *allow_dotted_names* option allows intruders to access your
193 module's global variables and may allow intruders to execute arbitrary code on
194 your machine. Only use this example only within a secure, closed network.
195
196::
197
198 import datetime
199
200 class ExampleService:
201 def getData(self):
202 return '42'
203
204 class currentTime:
205 @staticmethod
206 def getCurrentTime():
207 return datetime.datetime.now()
208
Martin Panter0cab9c12016-04-13 00:36:52 +0000209 with SimpleXMLRPCServer(("localhost", 8000)) as server:
210 server.register_function(pow)
211 server.register_function(lambda x,y: x+y, 'add')
212 server.register_instance(ExampleService(), allow_dotted_names=True)
213 server.register_multicall_functions()
214 print('Serving XML-RPC on localhost port 8000')
215 try:
216 server.serve_forever()
217 except KeyboardInterrupt:
218 print("\nKeyboard interrupt received, exiting.")
219 sys.exit(0)
Senthil Kumaran939e2db2014-01-12 16:06:58 -0800220
221This ExampleService demo can be invoked from the command line::
222
223 python -m xmlrpc.server
224
225
226The client that interacts with the above server is included in
227`Lib/xmlrpc/client.py`::
228
229 server = ServerProxy("http://localhost:8000")
230
231 try:
232 print(server.currentTime.getCurrentTime())
233 except Error as v:
234 print("ERROR", v)
235
236 multi = MultiCall(server)
237 multi.getData()
238 multi.pow(2,9)
239 multi.add(1,2)
240 try:
241 for response in multi():
242 print(response)
243 except Error as v:
244 print("ERROR", v)
245
246This client which interacts with the demo XMLRPC server can be invoked as::
247
248 python -m xmlrpc.client
249
Georg Brandl116aa622007-08-15 14:28:22 +0000250
251CGIXMLRPCRequestHandler
252-----------------------
253
254The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
255requests sent to Python CGI scripts.
256
257
Georg Brandl7f01a132009-09-16 15:58:14 +0000258.. method:: CGIXMLRPCRequestHandler.register_function(function, name=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000259
260 Register a function that can respond to XML-RPC requests. If *name* is given,
261 it will be the method name associated with function, otherwise
262 *function.__name__* will be used. *name* can be either a normal or Unicode
263 string, and may contain characters not legal in Python identifiers, including
264 the period character.
265
266
267.. method:: CGIXMLRPCRequestHandler.register_instance(instance)
268
269 Register an object which is used to expose method names which have not been
270 registered using :meth:`register_function`. If instance contains a
271 :meth:`_dispatch` method, it is called with the requested method name and the
272 parameters from the request; the return value is returned to the client as the
273 result. If instance does not have a :meth:`_dispatch` method, it is searched
274 for an attribute matching the name of the requested method; if the requested
275 method name contains periods, each component of the method name is searched for
276 individually, with the effect that a simple hierarchical search is performed.
277 The value found from this search is then called with the parameters from the
278 request, and the return value is passed back to the client.
279
280
281.. method:: CGIXMLRPCRequestHandler.register_introspection_functions()
282
283 Register the XML-RPC introspection functions ``system.listMethods``,
284 ``system.methodHelp`` and ``system.methodSignature``.
285
286
287.. method:: CGIXMLRPCRequestHandler.register_multicall_functions()
288
289 Register the XML-RPC multicall function ``system.multicall``.
290
291
Georg Brandl7f01a132009-09-16 15:58:14 +0000292.. method:: CGIXMLRPCRequestHandler.handle_request(request_text=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294 Handle a XML-RPC request. If *request_text* is given, it should be the POST
295 data provided by the HTTP server, otherwise the contents of stdin will be used.
296
297Example::
298
299 class MyFuncs:
Georg Brandl167543c2010-01-30 17:54:04 +0000300 def mul(self, x, y):
301 return x * y
Georg Brandl116aa622007-08-15 14:28:22 +0000302
303
304 handler = CGIXMLRPCRequestHandler()
305 handler.register_function(pow)
306 handler.register_function(lambda x,y: x+y, 'add')
307 handler.register_introspection_functions()
308 handler.register_instance(MyFuncs())
309 handler.handle_request()
310
Georg Brandl38eceaa2008-05-26 11:14:17 +0000311
312Documenting XMLRPC server
313-------------------------
314
315These classes extend the above classes to serve HTML documentation in response
316to HTTP GET requests. Servers can either be free standing, using
317:class:`DocXMLRPCServer`, or embedded in a CGI environment, using
318:class:`DocCGIXMLRPCRequestHandler`.
319
320
Florent Xicluna1b7458b2011-12-09 22:35:06 +0100321.. class:: DocXMLRPCServer(addr, requestHandler=DocXMLRPCRequestHandler,\
322 logRequests=True, allow_none=False, encoding=None,\
323 bind_and_activate=True, use_builtin_types=True)
Georg Brandl38eceaa2008-05-26 11:14:17 +0000324
325 Create a new server instance. All parameters have the same meaning as for
326 :class:`SimpleXMLRPCServer`; *requestHandler* defaults to
327 :class:`DocXMLRPCRequestHandler`.
328
Florent Xicluna1b7458b2011-12-09 22:35:06 +0100329 .. versionchanged:: 3.3
330 The *use_builtin_types* flag was added.
331
Georg Brandl38eceaa2008-05-26 11:14:17 +0000332
333.. class:: DocCGIXMLRPCRequestHandler()
334
335 Create a new instance to handle XML-RPC requests in a CGI environment.
336
337
338.. class:: DocXMLRPCRequestHandler()
339
340 Create a new request handler instance. This request handler supports XML-RPC
341 POST requests, documentation GET requests, and modifies logging so that the
342 *logRequests* parameter to the :class:`DocXMLRPCServer` constructor parameter is
343 honored.
344
345
346.. _doc-xmlrpc-servers:
347
348DocXMLRPCServer Objects
349-----------------------
350
351The :class:`DocXMLRPCServer` class is derived from :class:`SimpleXMLRPCServer`
352and provides a means of creating self-documenting, stand alone XML-RPC
353servers. HTTP POST requests are handled as XML-RPC method calls. HTTP GET
354requests are handled by generating pydoc-style HTML documentation. This allows a
355server to provide its own web-based documentation.
356
357
358.. method:: DocXMLRPCServer.set_server_title(server_title)
359
360 Set the title used in the generated HTML documentation. This title will be used
361 inside the HTML "title" element.
362
363
364.. method:: DocXMLRPCServer.set_server_name(server_name)
365
366 Set the name used in the generated HTML documentation. This name will appear at
367 the top of the generated documentation inside a "h1" element.
368
369
370.. method:: DocXMLRPCServer.set_server_documentation(server_documentation)
371
372 Set the description used in the generated HTML documentation. This description
373 will appear as a paragraph, below the server name, in the documentation.
374
375
376DocCGIXMLRPCRequestHandler
377--------------------------
378
379The :class:`DocCGIXMLRPCRequestHandler` class is derived from
380:class:`CGIXMLRPCRequestHandler` and provides a means of creating
381self-documenting, XML-RPC CGI scripts. HTTP POST requests are handled as XML-RPC
382method calls. HTTP GET requests are handled by generating pydoc-style HTML
383documentation. This allows a server to provide its own web-based documentation.
384
385
386.. method:: DocCGIXMLRPCRequestHandler.set_server_title(server_title)
387
388 Set the title used in the generated HTML documentation. This title will be used
389 inside the HTML "title" element.
390
391
392.. method:: DocCGIXMLRPCRequestHandler.set_server_name(server_name)
393
394 Set the name used in the generated HTML documentation. This name will appear at
395 the top of the generated documentation inside a "h1" element.
396
397
398.. method:: DocCGIXMLRPCRequestHandler.set_server_documentation(server_documentation)
399
400 Set the description used in the generated HTML documentation. This description
401 will appear as a paragraph, below the server name, in the documentation.