Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 1 | :mod:`xmlrpc.server` --- Basic XML-RPC servers |
| 2 | ============================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 4 | .. module:: xmlrpc.server |
| 5 | :synopsis: Basic XML-RPC server implementations. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | .. moduleauthor:: Brian Quinlan <brianq@activestate.com> |
| 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | |
Raymond Hettinger | 3029aff | 2011-02-10 08:09:36 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/xmlrpc/server.py` |
| 10 | |
| 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 13 | The :mod:`xmlrpc.server` module provides a basic server framework for XML-RPC |
| 14 | servers written in Python. Servers can either be free standing, using |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | :class:`SimpleXMLRPCServer`, or embedded in a CGI environment, using |
| 16 | :class:`CGIXMLRPCRequestHandler`. |
| 17 | |
| 18 | |
Florent Xicluna | 1b7458b | 2011-12-09 22:35:06 +0100 | [diff] [blame] | 19 | .. class:: SimpleXMLRPCServer(addr, requestHandler=SimpleXMLRPCRequestHandler,\ |
| 20 | logRequests=True, allow_none=False, encoding=None,\ |
| 21 | bind_and_activate=True, use_builtin_types=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
| 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 Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 27 | are passed to the :class:`socketserver.TCPServer` constructor. If *logRequests* |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | 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 Xicluna | 1b7458b | 2011-12-09 22:35:06 +0100 | [diff] [blame] | 30 | on to :mod:`xmlrpc.client` and control the XML-RPC responses that will be returned |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 31 | 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 Xicluna | 1b7458b | 2011-12-09 22:35:06 +0100 | [diff] [blame] | 35 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
Florent Xicluna | 1b7458b | 2011-12-09 22:35:06 +0100 | [diff] [blame] | 43 | .. class:: CGIXMLRPCRequestHandler(allow_none=False, encoding=None,\ |
| 44 | use_builtin_types=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
| 46 | Create a new instance to handle XML-RPC requests in a CGI environment. The |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 47 | *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 Xicluna | 1b7458b | 2011-12-09 22:35:06 +0100 | [diff] [blame] | 49 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
| 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 | |
| 66 | SimpleXMLRPCServer Objects |
| 67 | -------------------------- |
| 68 | |
| 69 | The :class:`SimpleXMLRPCServer` class is based on |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 70 | :class:`socketserver.TCPServer` and provides a means of creating simple, stand |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | alone XML-RPC servers. |
| 72 | |
| 73 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 74 | .. method:: SimpleXMLRPCServer.register_function(function, name=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
| 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 Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 83 | .. method:: SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
| 110 | .. method:: SimpleXMLRPCServer.register_introspection_functions() |
| 111 | |
| 112 | Registers the XML-RPC introspection functions ``system.listMethods``, |
| 113 | ``system.methodHelp`` and ``system.methodSignature``. |
| 114 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | |
| 116 | .. method:: SimpleXMLRPCServer.register_multicall_functions() |
| 117 | |
| 118 | Registers the XML-RPC multicall function system.multicall. |
| 119 | |
| 120 | |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 121 | .. attribute:: SimpleXMLRPCRequestHandler.rpc_paths |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 128 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 129 | .. _simplexmlrpcserver-example: |
| 130 | |
| 131 | SimpleXMLRPCServer Example |
| 132 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 133 | Server code:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 134 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 135 | from xmlrpc.server import SimpleXMLRPCServer |
| 136 | from xmlrpc.server import SimpleXMLRPCRequestHandler |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 137 | |
| 138 | # Restrict to a particular path. |
| 139 | class RequestHandler(SimpleXMLRPCRequestHandler): |
| 140 | rpc_paths = ('/RPC2',) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
| 142 | # Create server |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 143 | server = SimpleXMLRPCServer(("localhost", 8000), |
| 144 | requestHandler=RequestHandler) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | server.register_introspection_functions() |
| 146 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 147 | # Register pow() function; this will use the value of |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | # 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 Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 156 | # Register an instance; all the methods of the instance are |
Georg Brandl | 167543c | 2010-01-30 17:54:04 +0000 | [diff] [blame] | 157 | # published as XML-RPC methods (in this case, just 'mul'). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | class MyFuncs: |
Georg Brandl | 167543c | 2010-01-30 17:54:04 +0000 | [diff] [blame] | 159 | def mul(self, x, y): |
| 160 | return x * y |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
| 162 | server.register_instance(MyFuncs()) |
| 163 | |
| 164 | # Run the server's main loop |
| 165 | server.serve_forever() |
| 166 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 167 | The following client code will call the methods made available by the preceding |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | server:: |
| 169 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 170 | import xmlrpc.client |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 171 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 172 | s = xmlrpc.client.ServerProxy('http://localhost:8000') |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 173 | print(s.pow(2,3)) # Returns 2**3 = 8 |
| 174 | print(s.add(2,3)) # Returns 5 |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 175 | print(s.mul(5,2)) # Returns 5*2 = 10 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 176 | |
| 177 | # Print list of available methods |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 178 | print(s.system.listMethods()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | CGIXMLRPCRequestHandler |
| 182 | ----------------------- |
| 183 | |
| 184 | The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC |
| 185 | requests sent to Python CGI scripts. |
| 186 | |
| 187 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 188 | .. method:: CGIXMLRPCRequestHandler.register_function(function, name=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 189 | |
| 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 Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 222 | .. method:: CGIXMLRPCRequestHandler.handle_request(request_text=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | |
| 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 | |
| 227 | Example:: |
| 228 | |
| 229 | class MyFuncs: |
Georg Brandl | 167543c | 2010-01-30 17:54:04 +0000 | [diff] [blame] | 230 | def mul(self, x, y): |
| 231 | return x * y |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 232 | |
| 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 Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 241 | |
| 242 | Documenting XMLRPC server |
| 243 | ------------------------- |
| 244 | |
| 245 | These classes extend the above classes to serve HTML documentation in response |
| 246 | to 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 Xicluna | 1b7458b | 2011-12-09 22:35:06 +0100 | [diff] [blame] | 251 | .. class:: DocXMLRPCServer(addr, requestHandler=DocXMLRPCRequestHandler,\ |
| 252 | logRequests=True, allow_none=False, encoding=None,\ |
| 253 | bind_and_activate=True, use_builtin_types=True) |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 254 | |
| 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 Xicluna | 1b7458b | 2011-12-09 22:35:06 +0100 | [diff] [blame] | 259 | .. versionchanged:: 3.3 |
| 260 | The *use_builtin_types* flag was added. |
| 261 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 262 | |
| 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 | |
| 278 | DocXMLRPCServer Objects |
| 279 | ----------------------- |
| 280 | |
| 281 | The :class:`DocXMLRPCServer` class is derived from :class:`SimpleXMLRPCServer` |
| 282 | and provides a means of creating self-documenting, stand alone XML-RPC |
| 283 | servers. HTTP POST requests are handled as XML-RPC method calls. HTTP GET |
| 284 | requests are handled by generating pydoc-style HTML documentation. This allows a |
| 285 | server 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 | |
| 306 | DocCGIXMLRPCRequestHandler |
| 307 | -------------------------- |
| 308 | |
| 309 | The :class:`DocCGIXMLRPCRequestHandler` class is derived from |
| 310 | :class:`CGIXMLRPCRequestHandler` and provides a means of creating |
| 311 | self-documenting, XML-RPC CGI scripts. HTTP POST requests are handled as XML-RPC |
| 312 | method calls. HTTP GET requests are handled by generating pydoc-style HTML |
| 313 | documentation. 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. |