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 |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 107 | import os |
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 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 162 | def __init__(self): |
| 163 | self.funcs = {} |
| 164 | self.instance = None |
| 165 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 166 | def register_instance(self, instance, allow_dotted_names=False): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 167 | """Registers an instance to respond to XML-RPC requests. |
| 168 | |
| 169 | Only one instance can be installed at a time. |
| 170 | |
| 171 | If the registered instance has a _dispatch method then that |
| 172 | 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] | 173 | its parameters as a tuple |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 174 | e.g. instance._dispatch('add',(2,3)) |
| 175 | |
| 176 | If the registered instance does not have a _dispatch method |
| 177 | then the instance will be searched to find a matching method |
| 178 | and, if found, will be called. Methods beginning with an '_' |
| 179 | are considered private and will not be called by |
| 180 | SimpleXMLRPCServer. |
| 181 | |
| 182 | If a registered function matches a XML-RPC request, then it |
| 183 | will be called instead of the registered instance. |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 184 | |
| 185 | If the optional allow_dotted_names argument is true and the |
| 186 | instance does not have a _dispatch method, method names |
| 187 | containing dots are supported and resolved, as long as none of |
| 188 | the name segments start with an '_'. |
| 189 | |
| 190 | *** SECURITY WARNING: *** |
| 191 | |
| 192 | Enabling the allow_dotted_names options allows intruders |
| 193 | to access your module's global variables and may allow |
| 194 | intruders to execute arbitrary code on your machine. Only |
| 195 | use this option on a secure, closed network. |
| 196 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 197 | """ |
| 198 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 199 | self.instance = instance |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 200 | self.allow_dotted_names = allow_dotted_names |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 201 | |
| 202 | def register_function(self, function, name = None): |
| 203 | """Registers a function to respond to XML-RPC requests. |
| 204 | |
| 205 | The optional name argument can be used to set a Unicode name |
| 206 | for the function. |
| 207 | """ |
| 208 | |
| 209 | if name is None: |
| 210 | name = function.__name__ |
| 211 | self.funcs[name] = function |
| 212 | |
| 213 | def register_introspection_functions(self): |
| 214 | """Registers the XML-RPC introspection methods in the system |
| 215 | namespace. |
| 216 | |
| 217 | see http://xmlrpc.usefulinc.com/doc/reserved.html |
| 218 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 219 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 220 | self.funcs.update({'system.listMethods' : self.system_listMethods, |
| 221 | 'system.methodSignature' : self.system_methodSignature, |
| 222 | 'system.methodHelp' : self.system_methodHelp}) |
| 223 | |
| 224 | def register_multicall_functions(self): |
| 225 | """Registers the XML-RPC multicall method in the system |
| 226 | namespace. |
| 227 | |
| 228 | see http://www.xmlrpc.com/discuss/msgReader$1208""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 229 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 230 | self.funcs.update({'system.multicall' : self.system_multicall}) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 231 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 232 | def _marshaled_dispatch(self, data, dispatch_method = None): |
| 233 | """Dispatches an XML-RPC method from marshalled (XML) data. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 234 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 235 | XML-RPC methods are dispatched from the marshalled (XML) data |
| 236 | using the _dispatch method and the result is returned as |
| 237 | marshalled data. For backwards compatibility, a dispatch |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 238 | function can be provided as an argument (see comment in |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 239 | SimpleXMLRPCRequestHandler.do_POST) but overriding the |
| 240 | existing method through subclassing is the prefered means |
| 241 | of changing method dispatch behavior. |
| 242 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 243 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 244 | params, method = xmlrpclib.loads(data) |
| 245 | |
| 246 | # generate response |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 247 | try: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 248 | if dispatch_method is not None: |
| 249 | response = dispatch_method(method, params) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 250 | else: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 251 | response = self._dispatch(method, params) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 252 | # wrap response in a singleton tuple |
| 253 | response = (response,) |
| 254 | response = xmlrpclib.dumps(response, methodresponse=1) |
| 255 | except Fault, fault: |
| 256 | response = xmlrpclib.dumps(fault) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 257 | except: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 258 | # report exception back to server |
| 259 | response = xmlrpclib.dumps( |
| 260 | xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value)) |
| 261 | ) |
| 262 | |
| 263 | return response |
| 264 | |
| 265 | def system_listMethods(self): |
| 266 | """system.listMethods() => ['add', 'subtract', 'multiple'] |
| 267 | |
| 268 | Returns a list of the methods supported by the server.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 269 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 270 | methods = self.funcs.keys() |
| 271 | if self.instance is not None: |
| 272 | # Instance can implement _listMethod to return a list of |
| 273 | # methods |
| 274 | if hasattr(self.instance, '_listMethods'): |
| 275 | methods = remove_duplicates( |
| 276 | methods + self.instance._listMethods() |
| 277 | ) |
| 278 | # if the instance has a _dispatch method then we |
| 279 | # don't have enough information to provide a list |
| 280 | # of methods |
| 281 | elif not hasattr(self.instance, '_dispatch'): |
| 282 | methods = remove_duplicates( |
| 283 | methods + list_public_methods(self.instance) |
| 284 | ) |
| 285 | methods.sort() |
| 286 | return methods |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 287 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 288 | def system_methodSignature(self, method_name): |
| 289 | """system.methodSignature('add') => [double, int, int] |
| 290 | |
Brett Cannon | b9b5f16 | 2004-10-03 23:21:44 +0000 | [diff] [blame] | 291 | 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] | 292 | above example, the add method takes two integers as arguments |
| 293 | and returns a double result. |
| 294 | |
| 295 | This server does NOT support system.methodSignature.""" |
| 296 | |
| 297 | # See http://xmlrpc.usefulinc.com/doc/sysmethodsig.html |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 298 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 299 | return 'signatures not supported' |
| 300 | |
| 301 | def system_methodHelp(self, method_name): |
| 302 | """system.methodHelp('add') => "Adds two integers together" |
| 303 | |
| 304 | Returns a string containing documentation for the specified method.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 305 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 306 | method = None |
| 307 | if self.funcs.has_key(method_name): |
| 308 | method = self.funcs[method_name] |
| 309 | elif self.instance is not None: |
| 310 | # Instance can implement _methodHelp to return help for a method |
| 311 | if hasattr(self.instance, '_methodHelp'): |
| 312 | return self.instance._methodHelp(method_name) |
| 313 | # if the instance has a _dispatch method then we |
| 314 | # don't have enough information to provide help |
| 315 | elif not hasattr(self.instance, '_dispatch'): |
| 316 | try: |
| 317 | method = resolve_dotted_attribute( |
| 318 | self.instance, |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 319 | method_name, |
| 320 | self.allow_dotted_names |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 321 | ) |
| 322 | except AttributeError: |
| 323 | pass |
| 324 | |
| 325 | # Note that we aren't checking that the method actually |
| 326 | # be a callable object of some kind |
| 327 | if method is None: |
| 328 | return "" |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 329 | else: |
Neal Norwitz | 732911f | 2003-06-29 04:16:28 +0000 | [diff] [blame] | 330 | import pydoc |
Neal Norwitz | 3f401f0 | 2003-06-29 04:19:37 +0000 | [diff] [blame] | 331 | return pydoc.getdoc(method) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 332 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 333 | def system_multicall(self, call_list): |
| 334 | """system.multicall([{'methodName': 'add', 'params': [2, 2]}, ...]) => \ |
| 335 | [[4], ...] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 336 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 337 | Allows the caller to package multiple XML-RPC calls into a single |
| 338 | request. |
| 339 | |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 340 | See http://www.xmlrpc.com/discuss/msgReader$1208 |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 341 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 342 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 343 | results = [] |
| 344 | for call in call_list: |
| 345 | method_name = call['methodName'] |
| 346 | params = call['params'] |
| 347 | |
| 348 | try: |
| 349 | # XXX A marshalling error in any response will fail the entire |
| 350 | # multicall. If someone cares they should fix this. |
| 351 | results.append([self._dispatch(method_name, params)]) |
| 352 | except Fault, fault: |
| 353 | results.append( |
| 354 | {'faultCode' : fault.faultCode, |
| 355 | 'faultString' : fault.faultString} |
| 356 | ) |
| 357 | except: |
| 358 | results.append( |
| 359 | {'faultCode' : 1, |
| 360 | 'faultString' : "%s:%s" % (sys.exc_type, sys.exc_value)} |
| 361 | ) |
| 362 | return results |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 363 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 364 | def _dispatch(self, method, params): |
| 365 | """Dispatches the XML-RPC method. |
| 366 | |
| 367 | XML-RPC calls are forwarded to a registered function that |
| 368 | matches the called XML-RPC method name. If no such function |
| 369 | exists then the call is forwarded to the registered instance, |
| 370 | if available. |
| 371 | |
| 372 | If the registered instance has a _dispatch method then that |
| 373 | 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] | 374 | its parameters as a tuple |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 375 | e.g. instance._dispatch('add',(2,3)) |
| 376 | |
| 377 | If the registered instance does not have a _dispatch method |
| 378 | then the instance will be searched to find a matching method |
| 379 | and, if found, will be called. |
| 380 | |
| 381 | Methods beginning with an '_' are considered private and will |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 382 | not be called. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 383 | """ |
| 384 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 385 | func = None |
| 386 | try: |
| 387 | # check to see if a matching function has been registered |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 388 | func = self.funcs[method] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 389 | except KeyError: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 390 | if self.instance is not None: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 391 | # check for a _dispatch method |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 392 | if hasattr(self.instance, '_dispatch'): |
| 393 | return self.instance._dispatch(method, params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 394 | else: |
| 395 | # call instance method directly |
| 396 | try: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 397 | func = resolve_dotted_attribute( |
| 398 | self.instance, |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 399 | method, |
| 400 | self.allow_dotted_names |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 401 | ) |
| 402 | except AttributeError: |
| 403 | pass |
| 404 | |
| 405 | if func is not None: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 406 | return func(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 407 | else: |
| 408 | raise Exception('method "%s" is not supported' % method) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 409 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 410 | class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 411 | """Simple XML-RPC request handler class. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 412 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 413 | Handles all HTTP POST requests and attempts to decode them as |
| 414 | XML-RPC requests. |
| 415 | """ |
| 416 | |
| 417 | def do_POST(self): |
| 418 | """Handles the HTTP POST request. |
| 419 | |
| 420 | Attempts to interpret all HTTP POST requests as XML-RPC calls, |
| 421 | which are forwarded to the server's _dispatch method for handling. |
| 422 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 423 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 424 | try: |
| 425 | # get arguments |
| 426 | data = self.rfile.read(int(self.headers["content-length"])) |
| 427 | # In previous versions of SimpleXMLRPCServer, _dispatch |
| 428 | # could be overridden in this class, instead of in |
| 429 | # SimpleXMLRPCDispatcher. To maintain backwards compatibility, |
| 430 | # check to see if a subclass implements _dispatch and dispatch |
| 431 | # using that method if present. |
| 432 | response = self.server._marshaled_dispatch( |
| 433 | data, getattr(self, '_dispatch', None) |
| 434 | ) |
| 435 | except: # This should only happen if the module is buggy |
| 436 | # internal error, report as HTTP server error |
| 437 | self.send_response(500) |
| 438 | self.end_headers() |
| 439 | else: |
| 440 | # got a valid XML RPC response |
| 441 | self.send_response(200) |
| 442 | self.send_header("Content-type", "text/xml") |
| 443 | self.send_header("Content-length", str(len(response))) |
| 444 | self.end_headers() |
| 445 | self.wfile.write(response) |
| 446 | |
| 447 | # shut down the connection |
| 448 | self.wfile.flush() |
| 449 | self.connection.shutdown(1) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 450 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 451 | def log_request(self, code='-', size='-'): |
| 452 | """Selectively log an accepted request.""" |
| 453 | |
| 454 | if self.server.logRequests: |
| 455 | BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size) |
| 456 | |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 457 | class SimpleXMLRPCServer(SocketServer.TCPServer, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 458 | SimpleXMLRPCDispatcher): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 459 | """Simple XML-RPC server. |
| 460 | |
| 461 | 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] | 462 | to be installed to handle requests. The default implementation |
| 463 | attempts to dispatch XML-RPC calls to the functions or instance |
| 464 | installed in the server. Override the _dispatch method inhereted |
| 465 | from SimpleXMLRPCDispatcher to change this behavior. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 466 | """ |
| 467 | |
| 468 | def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler, |
| 469 | logRequests=1): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 470 | self.logRequests = logRequests |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 471 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 472 | SimpleXMLRPCDispatcher.__init__(self) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 473 | SocketServer.TCPServer.__init__(self, addr, requestHandler) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 474 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 475 | class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher): |
| 476 | """Simple handler for XML-RPC data passed through CGI.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 477 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 478 | def __init__(self): |
| 479 | SimpleXMLRPCDispatcher.__init__(self) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 480 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 481 | def handle_xmlrpc(self, request_text): |
| 482 | """Handle a single XML-RPC request""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 483 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 484 | response = self._marshaled_dispatch(request_text) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 485 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 486 | print 'Content-Type: text/xml' |
| 487 | print 'Content-Length: %d' % len(response) |
| 488 | print |
Martin v. Löwis | 9c5ea50 | 2003-05-01 05:05:09 +0000 | [diff] [blame] | 489 | sys.stdout.write(response) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 490 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 491 | def handle_get(self): |
| 492 | """Handle a single HTTP GET request. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 493 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 494 | Default implementation indicates an error because |
| 495 | XML-RPC uses the POST method. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 496 | """ |
| 497 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 498 | code = 400 |
| 499 | message, explain = \ |
| 500 | BaseHTTPServer.BaseHTTPRequestHandler.responses[code] |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 501 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 502 | response = BaseHTTPServer.DEFAULT_ERROR_MESSAGE % \ |
| 503 | { |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 504 | 'code' : code, |
| 505 | 'message' : message, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 506 | 'explain' : explain |
| 507 | } |
| 508 | print 'Status: %d %s' % (code, message) |
| 509 | print 'Content-Type: text/html' |
| 510 | print 'Content-Length: %d' % len(response) |
| 511 | print |
Neal Norwitz | 732911f | 2003-06-29 04:16:28 +0000 | [diff] [blame] | 512 | sys.stdout.write(response) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 513 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 514 | def handle_request(self, request_text = None): |
| 515 | """Handle a single XML-RPC request passed through a CGI post method. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 516 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 517 | If no XML data is given then it is read from stdin. The resulting |
| 518 | XML-RPC response is printed to stdout along with the correct HTTP |
| 519 | headers. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 520 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 521 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 522 | if request_text is None and \ |
| 523 | os.environ.get('REQUEST_METHOD', None) == 'GET': |
| 524 | self.handle_get() |
| 525 | else: |
| 526 | # POST data is normally available through stdin |
| 527 | if request_text is None: |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 528 | request_text = sys.stdin.read() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 529 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 530 | self.handle_xmlrpc(request_text) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 531 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 532 | if __name__ == '__main__': |
| 533 | server = SimpleXMLRPCServer(("localhost", 8000)) |
| 534 | server.register_function(pow) |
| 535 | server.register_function(lambda x,y: x+y, 'add') |
| 536 | server.serve_forever() |