Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`SimpleXMLRPCServer` --- Basic XML-RPC server |
| 3 | ================================================== |
| 4 | |
| 5 | .. module:: SimpleXMLRPCServer |
| 6 | :synopsis: Basic XML-RPC server implementation. |
| 7 | .. moduleauthor:: Brian Quinlan <brianq@activestate.com> |
| 8 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 9 | |
| 10 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | The :mod:`SimpleXMLRPCServer` module provides a basic server framework for |
| 12 | XML-RPC servers written in Python. Servers can either be free standing, using |
| 13 | :class:`SimpleXMLRPCServer`, or embedded in a CGI environment, using |
| 14 | :class:`CGIXMLRPCRequestHandler`. |
| 15 | |
| 16 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 17 | .. class:: SimpleXMLRPCServer(addr[, requestHandler[, logRequests[, allow_none[, encoding[, bind_and_activate]]]]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
| 19 | Create a new server instance. This class provides methods for registration of |
| 20 | functions that can be called by the XML-RPC protocol. The *requestHandler* |
| 21 | parameter should be a factory for request handler instances; it defaults to |
| 22 | :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters |
| 23 | are passed to the :class:`SocketServer.TCPServer` constructor. If *logRequests* |
| 24 | is true (the default), requests will be logged; setting this parameter to false |
| 25 | will turn off logging. The *allow_none* and *encoding* parameters are passed |
| 26 | on to :mod:`xmlrpclib` and control the XML-RPC responses that will be returned |
| 27 | from the server. The *bind_and_activate* parameter controls whether |
| 28 | :meth:`server_bind` and :meth:`server_activate` are called immediately by the |
| 29 | constructor; it defaults to true. Setting it to false allows code to manipulate |
| 30 | the *allow_reuse_address* class variable before the address is bound. |
| 31 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | .. class:: CGIXMLRPCRequestHandler([allow_none[, encoding]]) |
| 34 | |
| 35 | Create a new instance to handle XML-RPC requests in a CGI environment. The |
| 36 | *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpclib` and |
| 37 | control the XML-RPC responses that will be returned from the server. |
| 38 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 39 | |
| 40 | .. class:: SimpleXMLRPCRequestHandler() |
| 41 | |
| 42 | Create a new request handler instance. This request handler supports ``POST`` |
| 43 | requests and modifies logging so that the *logRequests* parameter to the |
| 44 | :class:`SimpleXMLRPCServer` constructor parameter is honored. |
| 45 | |
| 46 | |
| 47 | .. _simple-xmlrpc-servers: |
| 48 | |
| 49 | SimpleXMLRPCServer Objects |
| 50 | -------------------------- |
| 51 | |
| 52 | The :class:`SimpleXMLRPCServer` class is based on |
| 53 | :class:`SocketServer.TCPServer` and provides a means of creating simple, stand |
| 54 | alone XML-RPC servers. |
| 55 | |
| 56 | |
| 57 | .. method:: SimpleXMLRPCServer.register_function(function[, name]) |
| 58 | |
| 59 | Register a function that can respond to XML-RPC requests. If *name* is given, |
| 60 | it will be the method name associated with *function*, otherwise |
| 61 | ``function.__name__`` will be used. *name* can be either a normal or Unicode |
| 62 | string, and may contain characters not legal in Python identifiers, including |
| 63 | the period character. |
| 64 | |
| 65 | |
| 66 | .. method:: SimpleXMLRPCServer.register_instance(instance[, allow_dotted_names]) |
| 67 | |
| 68 | Register an object which is used to expose method names which have not been |
| 69 | registered using :meth:`register_function`. If *instance* contains a |
| 70 | :meth:`_dispatch` method, it is called with the requested method name and the |
| 71 | parameters from the request. Its API is ``def _dispatch(self, method, params)`` |
| 72 | (note that *params* does not represent a variable argument list). If it calls |
| 73 | an underlying function to perform its task, that function is called as |
| 74 | ``func(*params)``, expanding the parameter list. The return value from |
| 75 | :meth:`_dispatch` is returned to the client as the result. If *instance* does |
| 76 | not have a :meth:`_dispatch` method, it is searched for an attribute matching |
| 77 | the name of the requested method. |
| 78 | |
| 79 | If the optional *allow_dotted_names* argument is true and the instance does not |
| 80 | have a :meth:`_dispatch` method, then if the requested method name contains |
| 81 | periods, each component of the method name is searched for individually, with |
| 82 | the effect that a simple hierarchical search is performed. The value found from |
| 83 | this search is then called with the parameters from the request, and the return |
| 84 | value is passed back to the client. |
| 85 | |
| 86 | .. warning:: |
| 87 | |
| 88 | Enabling the *allow_dotted_names* option allows intruders to access your |
| 89 | module's global variables and may allow intruders to execute arbitrary code on |
| 90 | your machine. Only use this option on a secure, closed network. |
| 91 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | |
| 93 | .. method:: SimpleXMLRPCServer.register_introspection_functions() |
| 94 | |
| 95 | Registers the XML-RPC introspection functions ``system.listMethods``, |
| 96 | ``system.methodHelp`` and ``system.methodSignature``. |
| 97 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | |
| 99 | .. method:: SimpleXMLRPCServer.register_multicall_functions() |
| 100 | |
| 101 | Registers the XML-RPC multicall function system.multicall. |
| 102 | |
| 103 | |
| 104 | .. attribute:: SimpleXMLRPCServer.rpc_paths |
| 105 | |
| 106 | An attribute value that must be a tuple listing valid path portions of the URL |
| 107 | for receiving XML-RPC requests. Requests posted to other paths will result in a |
| 108 | 404 "no such page" HTTP error. If this tuple is empty, all paths will be |
| 109 | considered valid. The default value is ``('/', '/RPC2')``. |
| 110 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 112 | .. _simplexmlrpcserver-example: |
| 113 | |
| 114 | SimpleXMLRPCServer Example |
| 115 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 116 | Server code:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
| 118 | from SimpleXMLRPCServer import SimpleXMLRPCServer |
| 119 | |
| 120 | # Create server |
| 121 | server = SimpleXMLRPCServer(("localhost", 8000)) |
| 122 | server.register_introspection_functions() |
| 123 | |
| 124 | # Register pow() function; this will use the value of |
| 125 | # pow.__name__ as the name, which is just 'pow'. |
| 126 | server.register_function(pow) |
| 127 | |
| 128 | # Register a function under a different name |
| 129 | def adder_function(x,y): |
| 130 | return x + y |
| 131 | server.register_function(adder_function, 'add') |
| 132 | |
| 133 | # Register an instance; all the methods of the instance are |
| 134 | # published as XML-RPC methods (in this case, just 'div'). |
| 135 | class MyFuncs: |
| 136 | def div(self, x, y): |
| 137 | return x // y |
| 138 | |
| 139 | server.register_instance(MyFuncs()) |
| 140 | |
| 141 | # Run the server's main loop |
| 142 | server.serve_forever() |
| 143 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 144 | 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] | 145 | server:: |
| 146 | |
| 147 | import xmlrpclib |
| 148 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 149 | s = xmlrpclib.ServerProxy('http://localhost:8000') |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 150 | print(s.pow(2,3)) # Returns 2**3 = 8 |
| 151 | print(s.add(2,3)) # Returns 5 |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 152 | print(s.mul(5,2)) # Returns 5*2 = 10 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 153 | |
| 154 | # Print list of available methods |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 155 | print(s.system.listMethods()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | CGIXMLRPCRequestHandler |
| 159 | ----------------------- |
| 160 | |
| 161 | The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC |
| 162 | requests sent to Python CGI scripts. |
| 163 | |
| 164 | |
| 165 | .. method:: CGIXMLRPCRequestHandler.register_function(function[, name]) |
| 166 | |
| 167 | Register a function that can respond to XML-RPC requests. If *name* is given, |
| 168 | it will be the method name associated with function, otherwise |
| 169 | *function.__name__* will be used. *name* can be either a normal or Unicode |
| 170 | string, and may contain characters not legal in Python identifiers, including |
| 171 | the period character. |
| 172 | |
| 173 | |
| 174 | .. method:: CGIXMLRPCRequestHandler.register_instance(instance) |
| 175 | |
| 176 | Register an object which is used to expose method names which have not been |
| 177 | registered using :meth:`register_function`. If instance contains a |
| 178 | :meth:`_dispatch` method, it is called with the requested method name and the |
| 179 | parameters from the request; the return value is returned to the client as the |
| 180 | result. If instance does not have a :meth:`_dispatch` method, it is searched |
| 181 | for an attribute matching the name of the requested method; if the requested |
| 182 | method name contains periods, each component of the method name is searched for |
| 183 | individually, with the effect that a simple hierarchical search is performed. |
| 184 | The value found from this search is then called with the parameters from the |
| 185 | request, and the return value is passed back to the client. |
| 186 | |
| 187 | |
| 188 | .. method:: CGIXMLRPCRequestHandler.register_introspection_functions() |
| 189 | |
| 190 | Register the XML-RPC introspection functions ``system.listMethods``, |
| 191 | ``system.methodHelp`` and ``system.methodSignature``. |
| 192 | |
| 193 | |
| 194 | .. method:: CGIXMLRPCRequestHandler.register_multicall_functions() |
| 195 | |
| 196 | Register the XML-RPC multicall function ``system.multicall``. |
| 197 | |
| 198 | |
| 199 | .. method:: CGIXMLRPCRequestHandler.handle_request([request_text = None]) |
| 200 | |
| 201 | Handle a XML-RPC request. If *request_text* is given, it should be the POST |
| 202 | data provided by the HTTP server, otherwise the contents of stdin will be used. |
| 203 | |
| 204 | Example:: |
| 205 | |
| 206 | class MyFuncs: |
| 207 | def div(self, x, y) : return x // y |
| 208 | |
| 209 | |
| 210 | handler = CGIXMLRPCRequestHandler() |
| 211 | handler.register_function(pow) |
| 212 | handler.register_function(lambda x,y: x+y, 'add') |
| 213 | handler.register_introspection_functions() |
| 214 | handler.register_instance(MyFuncs()) |
| 215 | handler.handle_request() |
| 216 | |