| 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 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 19 | .. class:: SimpleXMLRPCServer(addr, requestHandler=SimpleXMLRPCRequestHandler, logRequests=True, allow_none=False, encoding=None, bind_and_activate=True) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 20 |  | 
 | 21 |    Create a new server instance.  This class provides methods for registration of | 
 | 22 |    functions that can be called by the XML-RPC protocol.  The *requestHandler* | 
 | 23 |    parameter should be a factory for request handler instances; it defaults to | 
 | 24 |    :class:`SimpleXMLRPCRequestHandler`.  The *addr* and *requestHandler* parameters | 
| Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 25 |    are passed to the :class:`socketserver.TCPServer` constructor.  If *logRequests* | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 |    is true (the default), requests will be logged; setting this parameter to false | 
 | 27 |    will turn off logging.   The *allow_none* and *encoding* parameters are passed | 
| Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 28 |    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] | 29 |    from the server. The *bind_and_activate* parameter controls whether | 
 | 30 |    :meth:`server_bind` and :meth:`server_activate` are called immediately by the | 
 | 31 |    constructor; it defaults to true. Setting it to false allows code to manipulate | 
 | 32 |    the *allow_reuse_address* class variable before the address is bound. | 
 | 33 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 35 | .. class:: CGIXMLRPCRequestHandler(allow_none=False, encoding=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 |  | 
 | 37 |    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] | 38 |    *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpc.client` | 
 | 39 |    and control the XML-RPC responses that will be returned from the server. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 |  | 
 | 42 | .. class:: SimpleXMLRPCRequestHandler() | 
 | 43 |  | 
 | 44 |    Create a new request handler instance.  This request handler supports ``POST`` | 
 | 45 |    requests and modifies logging so that the *logRequests* parameter to the | 
 | 46 |    :class:`SimpleXMLRPCServer` constructor parameter is honored. | 
 | 47 |  | 
 | 48 |  | 
 | 49 | .. _simple-xmlrpc-servers: | 
 | 50 |  | 
 | 51 | SimpleXMLRPCServer Objects | 
 | 52 | -------------------------- | 
 | 53 |  | 
 | 54 | The :class:`SimpleXMLRPCServer` class is based on | 
| Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 55 | :class:`socketserver.TCPServer` and provides a means of creating simple, stand | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | alone XML-RPC servers. | 
 | 57 |  | 
 | 58 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 59 | .. method:: SimpleXMLRPCServer.register_function(function, name=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 |  | 
 | 61 |    Register a function that can respond to XML-RPC requests.  If *name* is given, | 
 | 62 |    it will be the method name associated with *function*, otherwise | 
 | 63 |    ``function.__name__`` will be used.  *name* can be either a normal or Unicode | 
 | 64 |    string, and may contain characters not legal in Python identifiers, including | 
 | 65 |    the period character. | 
 | 66 |  | 
 | 67 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 68 | .. method:: SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 |  | 
 | 70 |    Register an object which is used to expose method names which have not been | 
 | 71 |    registered using :meth:`register_function`.  If *instance* contains a | 
 | 72 |    :meth:`_dispatch` method, it is called with the requested method name and the | 
 | 73 |    parameters from the request.  Its API is ``def _dispatch(self, method, params)`` | 
 | 74 |    (note that *params* does not represent a variable argument list).  If it calls | 
 | 75 |    an underlying function to perform its task, that function is called as | 
 | 76 |    ``func(*params)``, expanding the parameter list. The return value from | 
 | 77 |    :meth:`_dispatch` is returned to the client as the result.  If *instance* does | 
 | 78 |    not have a :meth:`_dispatch` method, it is searched for an attribute matching | 
 | 79 |    the name of the requested method. | 
 | 80 |  | 
 | 81 |    If the optional *allow_dotted_names* argument is true and the instance does not | 
 | 82 |    have a :meth:`_dispatch` method, then if the requested method name contains | 
 | 83 |    periods, each component of the method name is searched for individually, with | 
 | 84 |    the effect that a simple hierarchical search is performed.  The value found from | 
 | 85 |    this search is then called with the parameters from the request, and the return | 
 | 86 |    value is passed back to the client. | 
 | 87 |  | 
 | 88 |    .. warning:: | 
 | 89 |  | 
 | 90 |       Enabling the *allow_dotted_names* option allows intruders to access your | 
 | 91 |       module's global variables and may allow intruders to execute arbitrary code on | 
 | 92 |       your machine.  Only use this option on a secure, closed network. | 
 | 93 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 |  | 
 | 95 | .. method:: SimpleXMLRPCServer.register_introspection_functions() | 
 | 96 |  | 
 | 97 |    Registers the XML-RPC introspection functions ``system.listMethods``, | 
 | 98 |    ``system.methodHelp`` and ``system.methodSignature``. | 
 | 99 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 |  | 
 | 101 | .. method:: SimpleXMLRPCServer.register_multicall_functions() | 
 | 102 |  | 
 | 103 |    Registers the XML-RPC multicall function system.multicall. | 
 | 104 |  | 
 | 105 |  | 
| Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 106 | .. attribute:: SimpleXMLRPCRequestHandler.rpc_paths | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 |  | 
 | 108 |    An attribute value that must be a tuple listing valid path portions of the URL | 
 | 109 |    for receiving XML-RPC requests.  Requests posted to other paths will result in a | 
 | 110 |    404 "no such page" HTTP error.  If this tuple is empty, all paths will be | 
 | 111 |    considered valid. The default value is ``('/', '/RPC2')``. | 
 | 112 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 |  | 
| Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 114 | .. _simplexmlrpcserver-example: | 
 | 115 |  | 
 | 116 | SimpleXMLRPCServer Example | 
 | 117 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | 
 | 118 | Server code:: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 |  | 
| Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 120 |    from xmlrpc.server import SimpleXMLRPCServer | 
 | 121 |    from xmlrpc.server import SimpleXMLRPCRequestHandler | 
| Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 122 |  | 
 | 123 |    # Restrict to a particular path. | 
 | 124 |    class RequestHandler(SimpleXMLRPCRequestHandler): | 
 | 125 |        rpc_paths = ('/RPC2',) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 |  | 
 | 127 |    # Create server | 
| Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 128 |    server = SimpleXMLRPCServer(("localhost", 8000), | 
 | 129 |                                requestHandler=RequestHandler) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 130 |    server.register_introspection_functions() | 
 | 131 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 132 |    # Register pow() function; this will use the value of | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 |    # pow.__name__ as the name, which is just 'pow'. | 
 | 134 |    server.register_function(pow) | 
 | 135 |  | 
 | 136 |    # Register a function under a different name | 
 | 137 |    def adder_function(x,y): | 
 | 138 |        return x + y | 
 | 139 |    server.register_function(adder_function, 'add') | 
 | 140 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 141 |    # Register an instance; all the methods of the instance are | 
| Georg Brandl | 167543c | 2010-01-30 17:54:04 +0000 | [diff] [blame] | 142 |    # published as XML-RPC methods (in this case, just 'mul'). | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 143 |    class MyFuncs: | 
| Georg Brandl | 167543c | 2010-01-30 17:54:04 +0000 | [diff] [blame] | 144 |        def mul(self, x, y): | 
 | 145 |            return x * y | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 |  | 
 | 147 |    server.register_instance(MyFuncs()) | 
 | 148 |  | 
 | 149 |    # Run the server's main loop | 
 | 150 |    server.serve_forever() | 
 | 151 |  | 
| Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 152 | 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] | 153 | server:: | 
 | 154 |  | 
| Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 155 |    import xmlrpc.client | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 |  | 
| Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 157 |    s = xmlrpc.client.ServerProxy('http://localhost:8000') | 
| Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 158 |    print(s.pow(2,3))  # Returns 2**3 = 8 | 
 | 159 |    print(s.add(2,3))  # Returns 5 | 
| Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 160 |    print(s.mul(5,2))  # Returns 5*2 = 10 | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 |  | 
 | 162 |    # Print list of available methods | 
| Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 163 |    print(s.system.listMethods()) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 164 |  | 
 | 165 |  | 
 | 166 | CGIXMLRPCRequestHandler | 
 | 167 | ----------------------- | 
 | 168 |  | 
 | 169 | The :class:`CGIXMLRPCRequestHandler` class can be used to  handle XML-RPC | 
 | 170 | requests sent to Python CGI scripts. | 
 | 171 |  | 
 | 172 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 173 | .. method:: CGIXMLRPCRequestHandler.register_function(function, name=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 |  | 
 | 175 |    Register a function that can respond to XML-RPC requests. If  *name* is given, | 
 | 176 |    it will be the method name associated with  function, otherwise | 
 | 177 |    *function.__name__* will be used. *name* can be either a normal or Unicode | 
 | 178 |    string, and may contain  characters not legal in Python identifiers, including | 
 | 179 |    the period character. | 
 | 180 |  | 
 | 181 |  | 
 | 182 | .. method:: CGIXMLRPCRequestHandler.register_instance(instance) | 
 | 183 |  | 
 | 184 |    Register an object which is used to expose method names  which have not been | 
 | 185 |    registered using :meth:`register_function`. If  instance contains a | 
 | 186 |    :meth:`_dispatch` method, it is called with the  requested method name and the | 
 | 187 |    parameters from the  request; the return value is returned to the client as the | 
 | 188 |    result. If instance does not have a :meth:`_dispatch` method, it is searched | 
 | 189 |    for an attribute matching the name of the requested method; if  the requested | 
 | 190 |    method name contains periods, each  component of the method name is searched for | 
 | 191 |    individually,  with the effect that a simple hierarchical search is performed. | 
 | 192 |    The value found from this search is then called with the  parameters from the | 
 | 193 |    request, and the return value is passed  back to the client. | 
 | 194 |  | 
 | 195 |  | 
 | 196 | .. method:: CGIXMLRPCRequestHandler.register_introspection_functions() | 
 | 197 |  | 
 | 198 |    Register the XML-RPC introspection functions  ``system.listMethods``, | 
 | 199 |    ``system.methodHelp`` and  ``system.methodSignature``. | 
 | 200 |  | 
 | 201 |  | 
 | 202 | .. method:: CGIXMLRPCRequestHandler.register_multicall_functions() | 
 | 203 |  | 
 | 204 |    Register the XML-RPC multicall function ``system.multicall``. | 
 | 205 |  | 
 | 206 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 207 | .. method:: CGIXMLRPCRequestHandler.handle_request(request_text=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 208 |  | 
 | 209 |    Handle a XML-RPC request. If *request_text* is given, it  should be the POST | 
 | 210 |    data provided by the HTTP server,  otherwise the contents of stdin will be used. | 
 | 211 |  | 
 | 212 | Example:: | 
 | 213 |  | 
 | 214 |    class MyFuncs: | 
| Georg Brandl | 167543c | 2010-01-30 17:54:04 +0000 | [diff] [blame] | 215 |        def mul(self, x, y): | 
 | 216 |            return x * y | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 |  | 
 | 218 |  | 
 | 219 |    handler = CGIXMLRPCRequestHandler() | 
 | 220 |    handler.register_function(pow) | 
 | 221 |    handler.register_function(lambda x,y: x+y, 'add') | 
 | 222 |    handler.register_introspection_functions() | 
 | 223 |    handler.register_instance(MyFuncs()) | 
 | 224 |    handler.handle_request() | 
 | 225 |  | 
| Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 226 |  | 
 | 227 | Documenting XMLRPC server | 
 | 228 | ------------------------- | 
 | 229 |  | 
 | 230 | These classes extend the above classes to serve HTML documentation in response | 
 | 231 | to HTTP GET requests.  Servers can either be free standing, using | 
 | 232 | :class:`DocXMLRPCServer`, or embedded in a CGI environment, using | 
 | 233 | :class:`DocCGIXMLRPCRequestHandler`. | 
 | 234 |  | 
 | 235 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 236 | .. class:: DocXMLRPCServer(addr, requestHandler=DocXMLRPCRequestHandler, logRequests=True, allow_none=False, encoding=None, bind_and_activate=True) | 
| Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 237 |  | 
 | 238 |    Create a new server instance. All parameters have the same meaning as for | 
 | 239 |    :class:`SimpleXMLRPCServer`; *requestHandler* defaults to | 
 | 240 |    :class:`DocXMLRPCRequestHandler`. | 
 | 241 |  | 
 | 242 |  | 
 | 243 | .. class:: DocCGIXMLRPCRequestHandler() | 
 | 244 |  | 
 | 245 |    Create a new instance to handle XML-RPC requests in a CGI environment. | 
 | 246 |  | 
 | 247 |  | 
 | 248 | .. class:: DocXMLRPCRequestHandler() | 
 | 249 |  | 
 | 250 |    Create a new request handler instance. This request handler supports XML-RPC | 
 | 251 |    POST requests, documentation GET requests, and modifies logging so that the | 
 | 252 |    *logRequests* parameter to the :class:`DocXMLRPCServer` constructor parameter is | 
 | 253 |    honored. | 
 | 254 |  | 
 | 255 |  | 
 | 256 | .. _doc-xmlrpc-servers: | 
 | 257 |  | 
 | 258 | DocXMLRPCServer Objects | 
 | 259 | ----------------------- | 
 | 260 |  | 
 | 261 | The :class:`DocXMLRPCServer` class is derived from :class:`SimpleXMLRPCServer` | 
 | 262 | and provides a means of creating self-documenting, stand alone XML-RPC | 
 | 263 | servers. HTTP POST requests are handled as XML-RPC method calls. HTTP GET | 
 | 264 | requests are handled by generating pydoc-style HTML documentation. This allows a | 
 | 265 | server to provide its own web-based documentation. | 
 | 266 |  | 
 | 267 |  | 
 | 268 | .. method:: DocXMLRPCServer.set_server_title(server_title) | 
 | 269 |  | 
 | 270 |    Set the title used in the generated HTML documentation. This title will be used | 
 | 271 |    inside the HTML "title" element. | 
 | 272 |  | 
 | 273 |  | 
 | 274 | .. method:: DocXMLRPCServer.set_server_name(server_name) | 
 | 275 |  | 
 | 276 |    Set the name used in the generated HTML documentation. This name will appear at | 
 | 277 |    the top of the generated documentation inside a "h1" element. | 
 | 278 |  | 
 | 279 |  | 
 | 280 | .. method:: DocXMLRPCServer.set_server_documentation(server_documentation) | 
 | 281 |  | 
 | 282 |    Set the description used in the generated HTML documentation. This description | 
 | 283 |    will appear as a paragraph, below the server name, in the documentation. | 
 | 284 |  | 
 | 285 |  | 
 | 286 | DocCGIXMLRPCRequestHandler | 
 | 287 | -------------------------- | 
 | 288 |  | 
 | 289 | The :class:`DocCGIXMLRPCRequestHandler` class is derived from | 
 | 290 | :class:`CGIXMLRPCRequestHandler` and provides a means of creating | 
 | 291 | self-documenting, XML-RPC CGI scripts. HTTP POST requests are handled as XML-RPC | 
 | 292 | method calls. HTTP GET requests are handled by generating pydoc-style HTML | 
 | 293 | documentation. This allows a server to provide its own web-based documentation. | 
 | 294 |  | 
 | 295 |  | 
 | 296 | .. method:: DocCGIXMLRPCRequestHandler.set_server_title(server_title) | 
 | 297 |  | 
 | 298 |    Set the title used in the generated HTML documentation. This title will be used | 
 | 299 |    inside the HTML "title" element. | 
 | 300 |  | 
 | 301 |  | 
 | 302 | .. method:: DocCGIXMLRPCRequestHandler.set_server_name(server_name) | 
 | 303 |  | 
 | 304 |    Set the name used in the generated HTML documentation. This name will appear at | 
 | 305 |    the top of the generated documentation inside a "h1" element. | 
 | 306 |  | 
 | 307 |  | 
 | 308 | .. method:: DocCGIXMLRPCRequestHandler.set_server_documentation(server_documentation) | 
 | 309 |  | 
 | 310 |    Set the description used in the generated HTML documentation. This description | 
 | 311 |    will appear as a paragraph, below the server name, in the documentation. |