blob: 6b4c202ca0bbc4a4a89aaed5d0cf483e08103530 [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
21 The :mod:`xmlrpc.client` module is not secure against maliciously
22 constructed data. If you need to parse untrusted or unauthenticated data see
23 :ref:`xml-vulnerabilities`.
24
25
Georg Brandl7f01a132009-09-16 15:58:14 +000026.. class:: SimpleXMLRPCServer(addr, requestHandler=SimpleXMLRPCRequestHandler, logRequests=True, allow_none=False, encoding=None, bind_and_activate=True)
Georg Brandl116aa622007-08-15 14:28:22 +000027
28 Create a new server instance. This class provides methods for registration of
29 functions that can be called by the XML-RPC protocol. The *requestHandler*
30 parameter should be a factory for request handler instances; it defaults to
31 :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters
Alexandre Vassalottice261952008-05-12 02:31:37 +000032 are passed to the :class:`socketserver.TCPServer` constructor. If *logRequests*
Georg Brandl116aa622007-08-15 14:28:22 +000033 is true (the default), requests will be logged; setting this parameter to false
34 will turn off logging. The *allow_none* and *encoding* parameters are passed
Georg Brandl38eceaa2008-05-26 11:14:17 +000035 on to :mod:`xmlrpc.client` and control the XML-RPC responses that will be returned
Georg Brandl116aa622007-08-15 14:28:22 +000036 from the server. The *bind_and_activate* parameter controls whether
37 :meth:`server_bind` and :meth:`server_activate` are called immediately by the
38 constructor; it defaults to true. Setting it to false allows code to manipulate
39 the *allow_reuse_address* class variable before the address is bound.
40
Georg Brandl116aa622007-08-15 14:28:22 +000041
Georg Brandl7f01a132009-09-16 15:58:14 +000042.. class:: CGIXMLRPCRequestHandler(allow_none=False, encoding=None)
Georg Brandl116aa622007-08-15 14:28:22 +000043
44 Create a new instance to handle XML-RPC requests in a CGI environment. The
Georg Brandl38eceaa2008-05-26 11:14:17 +000045 *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpc.client`
46 and control the XML-RPC responses that will be returned from the server.
Georg Brandl116aa622007-08-15 14:28:22 +000047
Georg Brandl116aa622007-08-15 14:28:22 +000048
49.. class:: SimpleXMLRPCRequestHandler()
50
51 Create a new request handler instance. This request handler supports ``POST``
52 requests and modifies logging so that the *logRequests* parameter to the
53 :class:`SimpleXMLRPCServer` constructor parameter is honored.
54
55
56.. _simple-xmlrpc-servers:
57
58SimpleXMLRPCServer Objects
59--------------------------
60
61The :class:`SimpleXMLRPCServer` class is based on
Alexandre Vassalottice261952008-05-12 02:31:37 +000062:class:`socketserver.TCPServer` and provides a means of creating simple, stand
Georg Brandl116aa622007-08-15 14:28:22 +000063alone XML-RPC servers.
64
65
Georg Brandl7f01a132009-09-16 15:58:14 +000066.. method:: SimpleXMLRPCServer.register_function(function, name=None)
Georg Brandl116aa622007-08-15 14:28:22 +000067
68 Register a function that can respond to XML-RPC requests. If *name* is given,
69 it will be the method name associated with *function*, otherwise
70 ``function.__name__`` will be used. *name* can be either a normal or Unicode
71 string, and may contain characters not legal in Python identifiers, including
72 the period character.
73
74
Georg Brandl7f01a132009-09-16 15:58:14 +000075.. method:: SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False)
Georg Brandl116aa622007-08-15 14:28:22 +000076
77 Register an object which is used to expose method names which have not been
78 registered using :meth:`register_function`. If *instance* contains a
79 :meth:`_dispatch` method, it is called with the requested method name and the
80 parameters from the request. Its API is ``def _dispatch(self, method, params)``
81 (note that *params* does not represent a variable argument list). If it calls
82 an underlying function to perform its task, that function is called as
83 ``func(*params)``, expanding the parameter list. The return value from
84 :meth:`_dispatch` is returned to the client as the result. If *instance* does
85 not have a :meth:`_dispatch` method, it is searched for an attribute matching
86 the name of the requested method.
87
88 If the optional *allow_dotted_names* argument is true and the instance does not
89 have a :meth:`_dispatch` method, then if the requested method name contains
90 periods, each component of the method name is searched for individually, with
91 the effect that a simple hierarchical search is performed. The value found from
92 this search is then called with the parameters from the request, and the return
93 value is passed back to the client.
94
95 .. warning::
96
97 Enabling the *allow_dotted_names* option allows intruders to access your
98 module's global variables and may allow intruders to execute arbitrary code on
99 your machine. Only use this option on a secure, closed network.
100
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102.. method:: SimpleXMLRPCServer.register_introspection_functions()
103
104 Registers the XML-RPC introspection functions ``system.listMethods``,
105 ``system.methodHelp`` and ``system.methodSignature``.
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108.. method:: SimpleXMLRPCServer.register_multicall_functions()
109
110 Registers the XML-RPC multicall function system.multicall.
111
112
Christian Heimes8640e742008-02-23 16:23:06 +0000113.. attribute:: SimpleXMLRPCRequestHandler.rpc_paths
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115 An attribute value that must be a tuple listing valid path portions of the URL
116 for receiving XML-RPC requests. Requests posted to other paths will result in a
117 404 "no such page" HTTP error. If this tuple is empty, all paths will be
118 considered valid. The default value is ``('/', '/RPC2')``.
119
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000121.. _simplexmlrpcserver-example:
122
123SimpleXMLRPCServer Example
124^^^^^^^^^^^^^^^^^^^^^^^^^^
125Server code::
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Georg Brandl38eceaa2008-05-26 11:14:17 +0000127 from xmlrpc.server import SimpleXMLRPCServer
128 from xmlrpc.server import SimpleXMLRPCRequestHandler
Christian Heimes8640e742008-02-23 16:23:06 +0000129
130 # Restrict to a particular path.
131 class RequestHandler(SimpleXMLRPCRequestHandler):
132 rpc_paths = ('/RPC2',)
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134 # Create server
Christian Heimes8640e742008-02-23 16:23:06 +0000135 server = SimpleXMLRPCServer(("localhost", 8000),
136 requestHandler=RequestHandler)
Georg Brandl116aa622007-08-15 14:28:22 +0000137 server.register_introspection_functions()
138
Georg Brandl48310cd2009-01-03 21:18:54 +0000139 # Register pow() function; this will use the value of
Georg Brandl116aa622007-08-15 14:28:22 +0000140 # pow.__name__ as the name, which is just 'pow'.
141 server.register_function(pow)
142
143 # Register a function under a different name
144 def adder_function(x,y):
145 return x + y
146 server.register_function(adder_function, 'add')
147
Georg Brandl48310cd2009-01-03 21:18:54 +0000148 # Register an instance; all the methods of the instance are
Georg Brandl167543c2010-01-30 17:54:04 +0000149 # published as XML-RPC methods (in this case, just 'mul').
Georg Brandl116aa622007-08-15 14:28:22 +0000150 class MyFuncs:
Georg Brandl167543c2010-01-30 17:54:04 +0000151 def mul(self, x, y):
152 return x * y
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154 server.register_instance(MyFuncs())
155
156 # Run the server's main loop
157 server.serve_forever()
158
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000159The following client code will call the methods made available by the preceding
Georg Brandl116aa622007-08-15 14:28:22 +0000160server::
161
Georg Brandl38eceaa2008-05-26 11:14:17 +0000162 import xmlrpc.client
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Georg Brandl38eceaa2008-05-26 11:14:17 +0000164 s = xmlrpc.client.ServerProxy('http://localhost:8000')
Georg Brandl6911e3c2007-09-04 07:15:32 +0000165 print(s.pow(2,3)) # Returns 2**3 = 8
166 print(s.add(2,3)) # Returns 5
Georg Brandlf6945182008-02-01 11:56:49 +0000167 print(s.mul(5,2)) # Returns 5*2 = 10
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169 # Print list of available methods
Georg Brandl6911e3c2007-09-04 07:15:32 +0000170 print(s.system.listMethods())
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172
173CGIXMLRPCRequestHandler
174-----------------------
175
176The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
177requests sent to Python CGI scripts.
178
179
Georg Brandl7f01a132009-09-16 15:58:14 +0000180.. method:: CGIXMLRPCRequestHandler.register_function(function, name=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000181
182 Register a function that can respond to XML-RPC requests. If *name* is given,
183 it will be the method name associated with function, otherwise
184 *function.__name__* will be used. *name* can be either a normal or Unicode
185 string, and may contain characters not legal in Python identifiers, including
186 the period character.
187
188
189.. method:: CGIXMLRPCRequestHandler.register_instance(instance)
190
191 Register an object which is used to expose method names which have not been
192 registered using :meth:`register_function`. If instance contains a
193 :meth:`_dispatch` method, it is called with the requested method name and the
194 parameters from the request; the return value is returned to the client as the
195 result. If instance does not have a :meth:`_dispatch` method, it is searched
196 for an attribute matching the name of the requested method; if the requested
197 method name contains periods, each component of the method name is searched for
198 individually, with the effect that a simple hierarchical search is performed.
199 The value found from this search is then called with the parameters from the
200 request, and the return value is passed back to the client.
201
202
203.. method:: CGIXMLRPCRequestHandler.register_introspection_functions()
204
205 Register the XML-RPC introspection functions ``system.listMethods``,
206 ``system.methodHelp`` and ``system.methodSignature``.
207
208
209.. method:: CGIXMLRPCRequestHandler.register_multicall_functions()
210
211 Register the XML-RPC multicall function ``system.multicall``.
212
213
Georg Brandl7f01a132009-09-16 15:58:14 +0000214.. method:: CGIXMLRPCRequestHandler.handle_request(request_text=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000215
216 Handle a XML-RPC request. If *request_text* is given, it should be the POST
217 data provided by the HTTP server, otherwise the contents of stdin will be used.
218
219Example::
220
221 class MyFuncs:
Georg Brandl167543c2010-01-30 17:54:04 +0000222 def mul(self, x, y):
223 return x * y
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225
226 handler = CGIXMLRPCRequestHandler()
227 handler.register_function(pow)
228 handler.register_function(lambda x,y: x+y, 'add')
229 handler.register_introspection_functions()
230 handler.register_instance(MyFuncs())
231 handler.handle_request()
232
Georg Brandl38eceaa2008-05-26 11:14:17 +0000233
234Documenting XMLRPC server
235-------------------------
236
237These classes extend the above classes to serve HTML documentation in response
238to HTTP GET requests. Servers can either be free standing, using
239:class:`DocXMLRPCServer`, or embedded in a CGI environment, using
240:class:`DocCGIXMLRPCRequestHandler`.
241
242
Georg Brandl7f01a132009-09-16 15:58:14 +0000243.. class:: DocXMLRPCServer(addr, requestHandler=DocXMLRPCRequestHandler, logRequests=True, allow_none=False, encoding=None, bind_and_activate=True)
Georg Brandl38eceaa2008-05-26 11:14:17 +0000244
245 Create a new server instance. All parameters have the same meaning as for
246 :class:`SimpleXMLRPCServer`; *requestHandler* defaults to
247 :class:`DocXMLRPCRequestHandler`.
248
249
250.. class:: DocCGIXMLRPCRequestHandler()
251
252 Create a new instance to handle XML-RPC requests in a CGI environment.
253
254
255.. class:: DocXMLRPCRequestHandler()
256
257 Create a new request handler instance. This request handler supports XML-RPC
258 POST requests, documentation GET requests, and modifies logging so that the
259 *logRequests* parameter to the :class:`DocXMLRPCServer` constructor parameter is
260 honored.
261
262
263.. _doc-xmlrpc-servers:
264
265DocXMLRPCServer Objects
266-----------------------
267
268The :class:`DocXMLRPCServer` class is derived from :class:`SimpleXMLRPCServer`
269and provides a means of creating self-documenting, stand alone XML-RPC
270servers. HTTP POST requests are handled as XML-RPC method calls. HTTP GET
271requests are handled by generating pydoc-style HTML documentation. This allows a
272server to provide its own web-based documentation.
273
274
275.. method:: DocXMLRPCServer.set_server_title(server_title)
276
277 Set the title used in the generated HTML documentation. This title will be used
278 inside the HTML "title" element.
279
280
281.. method:: DocXMLRPCServer.set_server_name(server_name)
282
283 Set the name used in the generated HTML documentation. This name will appear at
284 the top of the generated documentation inside a "h1" element.
285
286
287.. method:: DocXMLRPCServer.set_server_documentation(server_documentation)
288
289 Set the description used in the generated HTML documentation. This description
290 will appear as a paragraph, below the server name, in the documentation.
291
292
293DocCGIXMLRPCRequestHandler
294--------------------------
295
296The :class:`DocCGIXMLRPCRequestHandler` class is derived from
297:class:`CGIXMLRPCRequestHandler` and provides a means of creating
298self-documenting, XML-RPC CGI scripts. HTTP POST requests are handled as XML-RPC
299method calls. HTTP GET requests are handled by generating pydoc-style HTML
300documentation. This allows a server to provide its own web-based documentation.
301
302
303.. method:: DocCGIXMLRPCRequestHandler.set_server_title(server_title)
304
305 Set the title used in the generated HTML documentation. This title will be used
306 inside the HTML "title" element.
307
308
309.. method:: DocCGIXMLRPCRequestHandler.set_server_name(server_name)
310
311 Set the name used in the generated HTML documentation. This name will appear at
312 the top of the generated documentation inside a "h1" element.
313
314
315.. method:: DocCGIXMLRPCRequestHandler.set_server_documentation(server_documentation)
316
317 Set the description used in the generated HTML documentation. This description
318 will appear as a paragraph, below the server name, in the documentation.