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