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