Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 1 | """Simple XML-RPC Server. |
| 2 | |
| 3 | This module can be used to create simple XML-RPC servers |
| 4 | by creating a server and either installing functions, a |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 5 | class instance, or by extending the SimpleXMLRPCServer |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 6 | class. |
| 7 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 8 | It can also be used to handle XML-RPC requests in a CGI |
| 9 | environment using CGIXMLRPCRequestHandler. |
| 10 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 11 | A list of possible usage patterns follows: |
| 12 | |
| 13 | 1. Install functions: |
| 14 | |
| 15 | server = SimpleXMLRPCServer(("localhost", 8000)) |
| 16 | server.register_function(pow) |
| 17 | server.register_function(lambda x,y: x+y, 'add') |
| 18 | server.serve_forever() |
| 19 | |
| 20 | 2. Install an instance: |
| 21 | |
| 22 | class MyFuncs: |
| 23 | def __init__(self): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 24 | # make all of the sys functions available through sys.func_name |
| 25 | import sys |
| 26 | self.sys = sys |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 27 | def _listMethods(self): |
| 28 | # implement this method so that system.listMethods |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 29 | # knows to advertise the sys methods |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 30 | return list_public_methods(self) + \ |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 31 | ['sys.' + method for method in list_public_methods(self.sys)] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 32 | def pow(self, x, y): return pow(x, y) |
| 33 | def add(self, x, y) : return x + y |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 34 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 35 | server = SimpleXMLRPCServer(("localhost", 8000)) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 36 | server.register_introspection_functions() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 37 | server.register_instance(MyFuncs()) |
| 38 | server.serve_forever() |
| 39 | |
| 40 | 3. Install an instance with custom dispatch method: |
| 41 | |
| 42 | class Math: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 43 | def _listMethods(self): |
| 44 | # this method must be present for system.listMethods |
| 45 | # to work |
| 46 | return ['add', 'pow'] |
| 47 | def _methodHelp(self, method): |
| 48 | # this method must be present for system.methodHelp |
| 49 | # to work |
| 50 | if method == 'add': |
| 51 | return "add(2,3) => 5" |
| 52 | elif method == 'pow': |
| 53 | return "pow(x, y[, z]) => number" |
| 54 | else: |
| 55 | # By convention, return empty |
| 56 | # string if no help is available |
| 57 | return "" |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 58 | def _dispatch(self, method, params): |
| 59 | if method == 'pow': |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 60 | return pow(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 61 | elif method == 'add': |
| 62 | return params[0] + params[1] |
| 63 | else: |
| 64 | raise 'bad method' |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 65 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 66 | server = SimpleXMLRPCServer(("localhost", 8000)) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 67 | server.register_introspection_functions() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 68 | server.register_instance(Math()) |
| 69 | server.serve_forever() |
| 70 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 71 | 4. Subclass SimpleXMLRPCServer: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 72 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 73 | class MathServer(SimpleXMLRPCServer): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 74 | def _dispatch(self, method, params): |
| 75 | try: |
| 76 | # We are forcing the 'export_' prefix on methods that are |
| 77 | # callable through XML-RPC to prevent potential security |
| 78 | # problems |
| 79 | func = getattr(self, 'export_' + method) |
| 80 | except AttributeError: |
| 81 | raise Exception('method "%s" is not supported' % method) |
| 82 | else: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 83 | return func(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 84 | |
| 85 | def export_add(self, x, y): |
| 86 | return x + y |
| 87 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 88 | server = MathServer(("localhost", 8000)) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 89 | server.serve_forever() |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 90 | |
| 91 | 5. CGI script: |
| 92 | |
| 93 | server = CGIXMLRPCRequestHandler() |
| 94 | server.register_function(pow) |
| 95 | server.handle_request() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 96 | """ |
| 97 | |
| 98 | # Written by Brian Quinlan (brian@sweetapp.com). |
| 99 | # Based on code written by Fredrik Lundh. |
| 100 | |
| 101 | import xmlrpclib |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 102 | from xmlrpclib import Fault |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 103 | import SocketServer |
| 104 | import BaseHTTPServer |
| 105 | import sys |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 106 | import os |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 107 | import traceback |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | try: |
| 109 | import fcntl |
| 110 | except ImportError: |
| 111 | fcntl = None |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 113 | def resolve_dotted_attribute(obj, attr, allow_dotted_names=True): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 114 | """resolve_dotted_attribute(a, 'b.c.d') => a.b.c.d |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 115 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 116 | Resolves a dotted attribute name to an object. Raises |
| 117 | an AttributeError if any attribute in the chain starts with a '_'. |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 118 | |
| 119 | If the optional allow_dotted_names argument is false, dots are not |
| 120 | supported and this function operates similar to getattr(obj, attr). |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 121 | """ |
| 122 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 123 | if allow_dotted_names: |
| 124 | attrs = attr.split('.') |
| 125 | else: |
| 126 | attrs = [attr] |
| 127 | |
| 128 | for i in attrs: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 129 | if i.startswith('_'): |
| 130 | raise AttributeError( |
| 131 | 'attempt to access private attribute "%s"' % i |
| 132 | ) |
| 133 | else: |
| 134 | obj = getattr(obj,i) |
| 135 | return obj |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 136 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 137 | def list_public_methods(obj): |
| 138 | """Returns a list of attribute strings, found in the specified |
| 139 | object, which represent callable attributes""" |
| 140 | |
| 141 | return [member for member in dir(obj) |
| 142 | if not member.startswith('_') and |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 143 | hasattr(getattr(obj, member), '__call__')] |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 144 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 145 | class SimpleXMLRPCDispatcher: |
| 146 | """Mix-in class that dispatches XML-RPC requests. |
| 147 | |
| 148 | This class is used to register XML-RPC method handlers |
| 149 | and then to dispatch them. There should never be any |
| 150 | reason to instantiate this class directly. |
| 151 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 152 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 153 | def __init__(self, allow_none, encoding): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 154 | self.funcs = {} |
| 155 | self.instance = None |
Andrew M. Kuchling | 10a16de | 2005-12-04 16:34:40 +0000 | [diff] [blame] | 156 | self.allow_none = allow_none |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 157 | self.encoding = encoding |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 158 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 159 | def register_instance(self, instance, allow_dotted_names=False): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 160 | """Registers an instance to respond to XML-RPC requests. |
| 161 | |
| 162 | Only one instance can be installed at a time. |
| 163 | |
| 164 | If the registered instance has a _dispatch method then that |
| 165 | method will be called with the name of the XML-RPC method and |
Georg Brandl | 7eb4b7d | 2005-07-22 21:49:32 +0000 | [diff] [blame] | 166 | its parameters as a tuple |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 167 | e.g. instance._dispatch('add',(2,3)) |
| 168 | |
| 169 | If the registered instance does not have a _dispatch method |
| 170 | then the instance will be searched to find a matching method |
| 171 | and, if found, will be called. Methods beginning with an '_' |
| 172 | are considered private and will not be called by |
| 173 | SimpleXMLRPCServer. |
| 174 | |
| 175 | If a registered function matches a XML-RPC request, then it |
| 176 | will be called instead of the registered instance. |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 177 | |
| 178 | If the optional allow_dotted_names argument is true and the |
| 179 | instance does not have a _dispatch method, method names |
| 180 | containing dots are supported and resolved, as long as none of |
| 181 | the name segments start with an '_'. |
| 182 | |
| 183 | *** SECURITY WARNING: *** |
| 184 | |
| 185 | Enabling the allow_dotted_names options allows intruders |
| 186 | to access your module's global variables and may allow |
| 187 | intruders to execute arbitrary code on your machine. Only |
| 188 | use this option on a secure, closed network. |
| 189 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 190 | """ |
| 191 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 192 | self.instance = instance |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 193 | self.allow_dotted_names = allow_dotted_names |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 194 | |
| 195 | def register_function(self, function, name = None): |
| 196 | """Registers a function to respond to XML-RPC requests. |
| 197 | |
| 198 | The optional name argument can be used to set a Unicode name |
| 199 | for the function. |
| 200 | """ |
| 201 | |
| 202 | if name is None: |
| 203 | name = function.__name__ |
| 204 | self.funcs[name] = function |
| 205 | |
| 206 | def register_introspection_functions(self): |
| 207 | """Registers the XML-RPC introspection methods in the system |
| 208 | namespace. |
| 209 | |
| 210 | see http://xmlrpc.usefulinc.com/doc/reserved.html |
| 211 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 212 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 213 | self.funcs.update({'system.listMethods' : self.system_listMethods, |
| 214 | 'system.methodSignature' : self.system_methodSignature, |
| 215 | 'system.methodHelp' : self.system_methodHelp}) |
| 216 | |
| 217 | def register_multicall_functions(self): |
| 218 | """Registers the XML-RPC multicall method in the system |
| 219 | namespace. |
| 220 | |
| 221 | see http://www.xmlrpc.com/discuss/msgReader$1208""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 222 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 223 | self.funcs.update({'system.multicall' : self.system_multicall}) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 224 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 225 | def _marshaled_dispatch(self, data, dispatch_method = None): |
| 226 | """Dispatches an XML-RPC method from marshalled (XML) data. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 227 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 228 | XML-RPC methods are dispatched from the marshalled (XML) data |
| 229 | using the _dispatch method and the result is returned as |
| 230 | marshalled data. For backwards compatibility, a dispatch |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 231 | function can be provided as an argument (see comment in |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 232 | SimpleXMLRPCRequestHandler.do_POST) but overriding the |
| 233 | existing method through subclassing is the prefered means |
| 234 | of changing method dispatch behavior. |
| 235 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 236 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 237 | try: |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 238 | params, method = xmlrpclib.loads(data) |
| 239 | |
| 240 | # generate response |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 241 | if dispatch_method is not None: |
| 242 | response = dispatch_method(method, params) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 243 | else: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 244 | response = self._dispatch(method, params) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 245 | # wrap response in a singleton tuple |
| 246 | response = (response,) |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 247 | response = xmlrpclib.dumps(response, methodresponse=1, |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 248 | allow_none=self.allow_none, encoding=self.encoding) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 249 | except Fault as fault: |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 250 | response = xmlrpclib.dumps(fault, allow_none=self.allow_none, |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 251 | encoding=self.encoding) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 252 | except: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 253 | # report exception back to server |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 254 | exc_type, exc_value, exc_tb = sys.exc_info() |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 255 | response = xmlrpclib.dumps( |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 256 | xmlrpclib.Fault(1, "%s:%s" % (exc_type, exc_value)), |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 257 | encoding=self.encoding, allow_none=self.allow_none, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 258 | ) |
| 259 | |
| 260 | return response |
| 261 | |
| 262 | def system_listMethods(self): |
| 263 | """system.listMethods() => ['add', 'subtract', 'multiple'] |
| 264 | |
| 265 | Returns a list of the methods supported by the server.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 266 | |
Hye-Shik Chang | 9604286 | 2007-08-19 10:49:11 +0000 | [diff] [blame] | 267 | methods = set(self.funcs.keys()) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 268 | if self.instance is not None: |
| 269 | # Instance can implement _listMethod to return a list of |
| 270 | # methods |
| 271 | if hasattr(self.instance, '_listMethods'): |
Hye-Shik Chang | 9604286 | 2007-08-19 10:49:11 +0000 | [diff] [blame] | 272 | methods |= set(self.instance._listMethods()) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 273 | # if the instance has a _dispatch method then we |
| 274 | # don't have enough information to provide a list |
| 275 | # of methods |
| 276 | elif not hasattr(self.instance, '_dispatch'): |
Hye-Shik Chang | 9604286 | 2007-08-19 10:49:11 +0000 | [diff] [blame] | 277 | methods |= set(list_public_methods(self.instance)) |
| 278 | return sorted(methods) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 279 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 280 | def system_methodSignature(self, method_name): |
| 281 | """system.methodSignature('add') => [double, int, int] |
| 282 | |
Brett Cannon | b9b5f16 | 2004-10-03 23:21:44 +0000 | [diff] [blame] | 283 | Returns a list describing the signature of the method. In the |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 284 | above example, the add method takes two integers as arguments |
| 285 | and returns a double result. |
| 286 | |
| 287 | This server does NOT support system.methodSignature.""" |
| 288 | |
| 289 | # See http://xmlrpc.usefulinc.com/doc/sysmethodsig.html |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 290 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 291 | return 'signatures not supported' |
| 292 | |
| 293 | def system_methodHelp(self, method_name): |
| 294 | """system.methodHelp('add') => "Adds two integers together" |
| 295 | |
| 296 | Returns a string containing documentation for the specified method.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 297 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 298 | method = None |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 299 | if method_name in self.funcs: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 300 | method = self.funcs[method_name] |
| 301 | elif self.instance is not None: |
| 302 | # Instance can implement _methodHelp to return help for a method |
| 303 | if hasattr(self.instance, '_methodHelp'): |
| 304 | return self.instance._methodHelp(method_name) |
| 305 | # if the instance has a _dispatch method then we |
| 306 | # don't have enough information to provide help |
| 307 | elif not hasattr(self.instance, '_dispatch'): |
| 308 | try: |
| 309 | method = resolve_dotted_attribute( |
| 310 | self.instance, |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 311 | method_name, |
| 312 | self.allow_dotted_names |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 313 | ) |
| 314 | except AttributeError: |
| 315 | pass |
| 316 | |
| 317 | # Note that we aren't checking that the method actually |
| 318 | # be a callable object of some kind |
| 319 | if method is None: |
| 320 | return "" |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 321 | else: |
Neal Norwitz | 732911f | 2003-06-29 04:16:28 +0000 | [diff] [blame] | 322 | import pydoc |
Neal Norwitz | 3f401f0 | 2003-06-29 04:19:37 +0000 | [diff] [blame] | 323 | return pydoc.getdoc(method) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 324 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 325 | def system_multicall(self, call_list): |
| 326 | """system.multicall([{'methodName': 'add', 'params': [2, 2]}, ...]) => \ |
| 327 | [[4], ...] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 328 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 329 | Allows the caller to package multiple XML-RPC calls into a single |
| 330 | request. |
| 331 | |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 332 | See http://www.xmlrpc.com/discuss/msgReader$1208 |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 333 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 334 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 335 | results = [] |
| 336 | for call in call_list: |
| 337 | method_name = call['methodName'] |
| 338 | params = call['params'] |
| 339 | |
| 340 | try: |
| 341 | # XXX A marshalling error in any response will fail the entire |
| 342 | # multicall. If someone cares they should fix this. |
| 343 | results.append([self._dispatch(method_name, params)]) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 344 | except Fault as fault: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 345 | results.append( |
| 346 | {'faultCode' : fault.faultCode, |
| 347 | 'faultString' : fault.faultString} |
| 348 | ) |
| 349 | except: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 350 | exc_type, exc_value, exc_tb = sys.exc_info() |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 351 | results.append( |
| 352 | {'faultCode' : 1, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 353 | 'faultString' : "%s:%s" % (exc_type, exc_value)} |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 354 | ) |
| 355 | return results |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 356 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 357 | def _dispatch(self, method, params): |
| 358 | """Dispatches the XML-RPC method. |
| 359 | |
| 360 | XML-RPC calls are forwarded to a registered function that |
| 361 | matches the called XML-RPC method name. If no such function |
| 362 | exists then the call is forwarded to the registered instance, |
| 363 | if available. |
| 364 | |
| 365 | If the registered instance has a _dispatch method then that |
| 366 | method will be called with the name of the XML-RPC method and |
Georg Brandl | 7eb4b7d | 2005-07-22 21:49:32 +0000 | [diff] [blame] | 367 | its parameters as a tuple |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 368 | e.g. instance._dispatch('add',(2,3)) |
| 369 | |
| 370 | If the registered instance does not have a _dispatch method |
| 371 | then the instance will be searched to find a matching method |
| 372 | and, if found, will be called. |
| 373 | |
| 374 | Methods beginning with an '_' are considered private and will |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 375 | not be called. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 376 | """ |
| 377 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 378 | func = None |
| 379 | try: |
| 380 | # check to see if a matching function has been registered |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 381 | func = self.funcs[method] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 382 | except KeyError: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 383 | if self.instance is not None: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 384 | # check for a _dispatch method |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 385 | if hasattr(self.instance, '_dispatch'): |
| 386 | return self.instance._dispatch(method, params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 387 | else: |
| 388 | # call instance method directly |
| 389 | try: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 390 | func = resolve_dotted_attribute( |
| 391 | self.instance, |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 392 | method, |
| 393 | self.allow_dotted_names |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 394 | ) |
| 395 | except AttributeError: |
| 396 | pass |
| 397 | |
| 398 | if func is not None: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 399 | return func(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 400 | else: |
| 401 | raise Exception('method "%s" is not supported' % method) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 402 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 403 | class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 404 | """Simple XML-RPC request handler class. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 405 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 406 | Handles all HTTP POST requests and attempts to decode them as |
| 407 | XML-RPC requests. |
| 408 | """ |
| 409 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 410 | # Class attribute listing the accessible path components; |
| 411 | # paths not on this list will result in a 404 error. |
| 412 | rpc_paths = ('/', '/RPC2') |
| 413 | |
| 414 | def is_rpc_path_valid(self): |
| 415 | if self.rpc_paths: |
| 416 | return self.path in self.rpc_paths |
| 417 | else: |
| 418 | # If .rpc_paths is empty, just assume all paths are legal |
| 419 | return True |
| 420 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 421 | def do_POST(self): |
| 422 | """Handles the HTTP POST request. |
| 423 | |
| 424 | Attempts to interpret all HTTP POST requests as XML-RPC calls, |
| 425 | which are forwarded to the server's _dispatch method for handling. |
| 426 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 427 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 428 | # Check that the path is legal |
| 429 | if not self.is_rpc_path_valid(): |
| 430 | self.report_404() |
| 431 | return |
| 432 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 433 | try: |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 434 | # Get arguments by reading body of request. |
| 435 | # We read this in chunks to avoid straining |
Andrew M. Kuchling | e63fde7 | 2005-12-04 15:36:57 +0000 | [diff] [blame] | 436 | # socket.read(); around the 10 or 15Mb mark, some platforms |
| 437 | # begin to have problems (bug #792570). |
| 438 | max_chunk_size = 10*1024*1024 |
| 439 | size_remaining = int(self.headers["content-length"]) |
| 440 | L = [] |
| 441 | while size_remaining: |
| 442 | chunk_size = min(size_remaining, max_chunk_size) |
| 443 | L.append(self.rfile.read(chunk_size)) |
| 444 | size_remaining -= len(L[-1]) |
Hye-Shik Chang | 9604286 | 2007-08-19 10:49:11 +0000 | [diff] [blame] | 445 | data = b''.join(L) |
Andrew M. Kuchling | e63fde7 | 2005-12-04 15:36:57 +0000 | [diff] [blame] | 446 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 447 | # In previous versions of SimpleXMLRPCServer, _dispatch |
| 448 | # could be overridden in this class, instead of in |
| 449 | # SimpleXMLRPCDispatcher. To maintain backwards compatibility, |
| 450 | # check to see if a subclass implements _dispatch and dispatch |
| 451 | # using that method if present. |
| 452 | response = self.server._marshaled_dispatch( |
| 453 | data, getattr(self, '_dispatch', None) |
| 454 | ) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 455 | except Exception as e: # This should only happen if the module is buggy |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 456 | # internal error, report as HTTP server error |
| 457 | self.send_response(500) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 458 | |
| 459 | # Send information about the exception if requested |
| 460 | if hasattr(self.server, '_send_traceback_header') and \ |
| 461 | self.server._send_traceback_header: |
| 462 | self.send_header("X-exception", str(e)) |
| 463 | self.send_header("X-traceback", traceback.format_exc()) |
| 464 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 465 | self.end_headers() |
| 466 | else: |
Guido van Rossum | 8a392d7 | 2007-11-21 22:09:45 +0000 | [diff] [blame] | 467 | # Got a valid XML RPC response; convert to bytes first |
| 468 | response = response.encode("utf-8") |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 469 | self.send_response(200) |
| 470 | self.send_header("Content-type", "text/xml") |
| 471 | self.send_header("Content-length", str(len(response))) |
| 472 | self.end_headers() |
| 473 | self.wfile.write(response) |
| 474 | |
| 475 | # shut down the connection |
| 476 | self.wfile.flush() |
| 477 | self.connection.shutdown(1) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 478 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 479 | def report_404 (self): |
| 480 | # Report a 404 error |
| 481 | self.send_response(404) |
| 482 | response = 'No such page' |
| 483 | self.send_header("Content-type", "text/plain") |
| 484 | self.send_header("Content-length", str(len(response))) |
| 485 | self.end_headers() |
| 486 | self.wfile.write(response) |
| 487 | # shut down the connection |
| 488 | self.wfile.flush() |
| 489 | self.connection.shutdown(1) |
| 490 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 491 | def log_request(self, code='-', size='-'): |
| 492 | """Selectively log an accepted request.""" |
| 493 | |
| 494 | if self.server.logRequests: |
| 495 | BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size) |
| 496 | |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 497 | class SimpleXMLRPCServer(SocketServer.TCPServer, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 498 | SimpleXMLRPCDispatcher): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 499 | """Simple XML-RPC server. |
| 500 | |
| 501 | Simple XML-RPC server that allows functions and a single instance |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 502 | to be installed to handle requests. The default implementation |
| 503 | attempts to dispatch XML-RPC calls to the functions or instance |
| 504 | installed in the server. Override the _dispatch method inhereted |
| 505 | from SimpleXMLRPCDispatcher to change this behavior. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 506 | """ |
| 507 | |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 508 | allow_reuse_address = True |
| 509 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 510 | # Warning: this is for debugging purposes only! Never set this to True in |
| 511 | # production code, as will be sending out sensitive information (exception |
| 512 | # and stack trace details) when exceptions are raised inside |
| 513 | # SimpleXMLRPCRequestHandler.do_POST |
| 514 | _send_traceback_header = False |
| 515 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 516 | def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 517 | logRequests=True, allow_none=False, encoding=None, bind_and_activate=True): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 518 | self.logRequests = logRequests |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 519 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 520 | SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 521 | SocketServer.TCPServer.__init__(self, addr, requestHandler, bind_and_activate) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 522 | |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 523 | # [Bug #1222790] If possible, set close-on-exec flag; if a |
| 524 | # method spawns a subprocess, the subprocess shouldn't have |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 525 | # the listening socket open. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 526 | if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'): |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 527 | flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD) |
| 528 | flags |= fcntl.FD_CLOEXEC |
| 529 | fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags) |
| 530 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 531 | class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher): |
| 532 | """Simple handler for XML-RPC data passed through CGI.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 533 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 534 | def __init__(self, allow_none=False, encoding=None): |
| 535 | SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 536 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 537 | def handle_xmlrpc(self, request_text): |
| 538 | """Handle a single XML-RPC request""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 539 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 540 | response = self._marshaled_dispatch(request_text) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 541 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 542 | print('Content-Type: text/xml') |
| 543 | print('Content-Length: %d' % len(response)) |
| 544 | print() |
Martin v. Löwis | 9c5ea50 | 2003-05-01 05:05:09 +0000 | [diff] [blame] | 545 | sys.stdout.write(response) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 546 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 547 | def handle_get(self): |
| 548 | """Handle a single HTTP GET request. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 549 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 550 | Default implementation indicates an error because |
| 551 | XML-RPC uses the POST method. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 552 | """ |
| 553 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 554 | code = 400 |
| 555 | message, explain = \ |
| 556 | BaseHTTPServer.BaseHTTPRequestHandler.responses[code] |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 557 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 558 | response = BaseHTTPServer.DEFAULT_ERROR_MESSAGE % \ |
| 559 | { |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 560 | 'code' : code, |
| 561 | 'message' : message, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 562 | 'explain' : explain |
| 563 | } |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 564 | print('Status: %d %s' % (code, message)) |
| 565 | print('Content-Type: text/html') |
| 566 | print('Content-Length: %d' % len(response)) |
| 567 | print() |
Neal Norwitz | 732911f | 2003-06-29 04:16:28 +0000 | [diff] [blame] | 568 | sys.stdout.write(response) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 569 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 570 | def handle_request(self, request_text = None): |
| 571 | """Handle a single XML-RPC request passed through a CGI post method. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 572 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 573 | If no XML data is given then it is read from stdin. The resulting |
| 574 | XML-RPC response is printed to stdout along with the correct HTTP |
| 575 | headers. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 576 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 577 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 578 | if request_text is None and \ |
| 579 | os.environ.get('REQUEST_METHOD', None) == 'GET': |
| 580 | self.handle_get() |
| 581 | else: |
| 582 | # POST data is normally available through stdin |
| 583 | if request_text is None: |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 584 | request_text = sys.stdin.read() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 585 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 586 | self.handle_xmlrpc(request_text) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 587 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 588 | if __name__ == '__main__': |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 589 | print('Running XML-RPC server on port 8000') |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 590 | server = SimpleXMLRPCServer(("localhost", 8000)) |
| 591 | server.register_function(pow) |
| 592 | server.register_function(lambda x,y: x+y, 'add') |
| 593 | server.serve_forever() |