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