Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 1 | """XML-RPC Servers. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 11 | The Doc* classes can be used to create XML-RPC servers that |
| 12 | serve pydoc-style documentation in response to HTTP |
| 13 | GET requests. This documentation is dynamically generated |
| 14 | based on the functions and methods registered with the |
| 15 | server. |
| 16 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 17 | A list of possible usage patterns follows: |
| 18 | |
| 19 | 1. Install functions: |
| 20 | |
| 21 | server = SimpleXMLRPCServer(("localhost", 8000)) |
| 22 | server.register_function(pow) |
| 23 | server.register_function(lambda x,y: x+y, 'add') |
| 24 | server.serve_forever() |
| 25 | |
| 26 | 2. Install an instance: |
| 27 | |
| 28 | class MyFuncs: |
| 29 | def __init__(self): |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 30 | # make all of the sys functions available through sys.func_name |
| 31 | import sys |
| 32 | self.sys = sys |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 33 | def _listMethods(self): |
| 34 | # implement this method so that system.listMethods |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 35 | # knows to advertise the sys methods |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 36 | return list_public_methods(self) + \ |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 37 | ['sys.' + method for method in list_public_methods(self.sys)] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 38 | def pow(self, x, y): return pow(x, y) |
| 39 | def add(self, x, y) : return x + y |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 40 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 41 | server = SimpleXMLRPCServer(("localhost", 8000)) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 42 | server.register_introspection_functions() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 43 | server.register_instance(MyFuncs()) |
| 44 | server.serve_forever() |
| 45 | |
| 46 | 3. Install an instance with custom dispatch method: |
| 47 | |
| 48 | class Math: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 49 | def _listMethods(self): |
| 50 | # this method must be present for system.listMethods |
| 51 | # to work |
| 52 | return ['add', 'pow'] |
| 53 | def _methodHelp(self, method): |
| 54 | # this method must be present for system.methodHelp |
| 55 | # to work |
| 56 | if method == 'add': |
| 57 | return "add(2,3) => 5" |
| 58 | elif method == 'pow': |
| 59 | return "pow(x, y[, z]) => number" |
| 60 | else: |
| 61 | # By convention, return empty |
| 62 | # string if no help is available |
| 63 | return "" |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 64 | def _dispatch(self, method, params): |
| 65 | if method == 'pow': |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 66 | return pow(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 67 | elif method == 'add': |
| 68 | return params[0] + params[1] |
| 69 | else: |
| 70 | raise 'bad method' |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 71 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 72 | server = SimpleXMLRPCServer(("localhost", 8000)) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 73 | server.register_introspection_functions() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 74 | server.register_instance(Math()) |
| 75 | server.serve_forever() |
| 76 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 77 | 4. Subclass SimpleXMLRPCServer: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 78 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 79 | class MathServer(SimpleXMLRPCServer): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 80 | def _dispatch(self, method, params): |
| 81 | try: |
| 82 | # We are forcing the 'export_' prefix on methods that are |
| 83 | # callable through XML-RPC to prevent potential security |
| 84 | # problems |
| 85 | func = getattr(self, 'export_' + method) |
| 86 | except AttributeError: |
| 87 | raise Exception('method "%s" is not supported' % method) |
| 88 | else: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 89 | return func(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 90 | |
| 91 | def export_add(self, x, y): |
| 92 | return x + y |
| 93 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 94 | server = MathServer(("localhost", 8000)) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 95 | server.serve_forever() |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 96 | |
| 97 | 5. CGI script: |
| 98 | |
| 99 | server = CGIXMLRPCRequestHandler() |
| 100 | server.register_function(pow) |
| 101 | server.handle_request() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 102 | """ |
| 103 | |
| 104 | # Written by Brian Quinlan (brian@sweetapp.com). |
| 105 | # Based on code written by Fredrik Lundh. |
| 106 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 107 | from xmlrpc.client import Fault, dumps, loads, gzip_encode, gzip_decode |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 108 | from http.server import BaseHTTPRequestHandler |
| 109 | import http.server |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 110 | import socketserver |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 111 | import sys |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 112 | import os |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 113 | import re |
| 114 | import pydoc |
| 115 | import inspect |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 116 | import traceback |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 117 | try: |
| 118 | import fcntl |
| 119 | except ImportError: |
| 120 | fcntl = None |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 122 | def resolve_dotted_attribute(obj, attr, allow_dotted_names=True): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 123 | """resolve_dotted_attribute(a, 'b.c.d') => a.b.c.d |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 124 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 125 | Resolves a dotted attribute name to an object. Raises |
| 126 | an AttributeError if any attribute in the chain starts with a '_'. |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 127 | |
| 128 | If the optional allow_dotted_names argument is false, dots are not |
| 129 | supported and this function operates similar to getattr(obj, attr). |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 130 | """ |
| 131 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 132 | if allow_dotted_names: |
| 133 | attrs = attr.split('.') |
| 134 | else: |
| 135 | attrs = [attr] |
| 136 | |
| 137 | for i in attrs: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 138 | if i.startswith('_'): |
| 139 | raise AttributeError( |
| 140 | 'attempt to access private attribute "%s"' % i |
| 141 | ) |
| 142 | else: |
| 143 | obj = getattr(obj,i) |
| 144 | return obj |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 145 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 146 | def list_public_methods(obj): |
| 147 | """Returns a list of attribute strings, found in the specified |
| 148 | object, which represent callable attributes""" |
| 149 | |
| 150 | return [member for member in dir(obj) |
| 151 | if not member.startswith('_') and |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 152 | hasattr(getattr(obj, member), '__call__')] |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 153 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 154 | class SimpleXMLRPCDispatcher: |
| 155 | """Mix-in class that dispatches XML-RPC requests. |
| 156 | |
| 157 | This class is used to register XML-RPC method handlers |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 158 | and then to dispatch them. This class doesn't need to be |
| 159 | instanced directly when used by SimpleXMLRPCServer but it |
| 160 | can be instanced when used by the MultiPathXMLRPCServer |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 161 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 162 | |
Matthias Klose | a3d29e8 | 2009-04-07 13:13:10 +0000 | [diff] [blame] | 163 | def __init__(self, allow_none=False, encoding=None): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 164 | self.funcs = {} |
| 165 | self.instance = None |
Andrew M. Kuchling | 10a16de | 2005-12-04 16:34:40 +0000 | [diff] [blame] | 166 | self.allow_none = allow_none |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 167 | self.encoding = encoding or 'utf-8' |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 169 | def register_instance(self, instance, allow_dotted_names=False): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 170 | """Registers an instance to respond to XML-RPC requests. |
| 171 | |
| 172 | Only one instance can be installed at a time. |
| 173 | |
| 174 | If the registered instance has a _dispatch method then that |
| 175 | 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] | 176 | its parameters as a tuple |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 177 | e.g. instance._dispatch('add',(2,3)) |
| 178 | |
| 179 | If the registered instance does not have a _dispatch method |
| 180 | then the instance will be searched to find a matching method |
| 181 | and, if found, will be called. Methods beginning with an '_' |
| 182 | are considered private and will not be called by |
| 183 | SimpleXMLRPCServer. |
| 184 | |
| 185 | If a registered function matches a XML-RPC request, then it |
| 186 | will be called instead of the registered instance. |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 187 | |
| 188 | If the optional allow_dotted_names argument is true and the |
| 189 | instance does not have a _dispatch method, method names |
| 190 | containing dots are supported and resolved, as long as none of |
| 191 | the name segments start with an '_'. |
| 192 | |
| 193 | *** SECURITY WARNING: *** |
| 194 | |
| 195 | Enabling the allow_dotted_names options allows intruders |
| 196 | to access your module's global variables and may allow |
| 197 | intruders to execute arbitrary code on your machine. Only |
| 198 | use this option on a secure, closed network. |
| 199 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 200 | """ |
| 201 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 202 | self.instance = instance |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 203 | self.allow_dotted_names = allow_dotted_names |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 204 | |
Georg Brandl | fe99105 | 2009-09-16 15:54:04 +0000 | [diff] [blame] | 205 | def register_function(self, function, name=None): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 206 | """Registers a function to respond to XML-RPC requests. |
| 207 | |
| 208 | The optional name argument can be used to set a Unicode name |
| 209 | for the function. |
| 210 | """ |
| 211 | |
| 212 | if name is None: |
| 213 | name = function.__name__ |
| 214 | self.funcs[name] = function |
| 215 | |
| 216 | def register_introspection_functions(self): |
| 217 | """Registers the XML-RPC introspection methods in the system |
| 218 | namespace. |
| 219 | |
| 220 | see http://xmlrpc.usefulinc.com/doc/reserved.html |
| 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 | self.funcs.update({'system.listMethods' : self.system_listMethods, |
| 224 | 'system.methodSignature' : self.system_methodSignature, |
| 225 | 'system.methodHelp' : self.system_methodHelp}) |
| 226 | |
| 227 | def register_multicall_functions(self): |
| 228 | """Registers the XML-RPC multicall method in the system |
| 229 | namespace. |
| 230 | |
| 231 | see http://www.xmlrpc.com/discuss/msgReader$1208""" |
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 | self.funcs.update({'system.multicall' : self.system_multicall}) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 234 | |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 235 | def _marshaled_dispatch(self, data, dispatch_method = None, path = None): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 236 | """Dispatches an XML-RPC method from marshalled (XML) data. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 237 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 238 | XML-RPC methods are dispatched from the marshalled (XML) data |
| 239 | using the _dispatch method and the result is returned as |
| 240 | marshalled data. For backwards compatibility, a dispatch |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 241 | function can be provided as an argument (see comment in |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 242 | SimpleXMLRPCRequestHandler.do_POST) but overriding the |
| 243 | existing method through subclassing is the prefered means |
| 244 | of changing method dispatch behavior. |
| 245 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 246 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 247 | try: |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 248 | params, method = loads(data) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 249 | |
| 250 | # generate response |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 251 | if dispatch_method is not None: |
| 252 | response = dispatch_method(method, params) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 253 | else: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 254 | response = self._dispatch(method, params) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 255 | # wrap response in a singleton tuple |
| 256 | response = (response,) |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 257 | response = dumps(response, methodresponse=1, |
| 258 | allow_none=self.allow_none, encoding=self.encoding) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 259 | except Fault as fault: |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 260 | response = dumps(fault, allow_none=self.allow_none, |
| 261 | encoding=self.encoding) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 262 | except: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 263 | # report exception back to server |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 264 | exc_type, exc_value, exc_tb = sys.exc_info() |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 265 | response = dumps( |
| 266 | Fault(1, "%s:%s" % (exc_type, exc_value)), |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 267 | encoding=self.encoding, allow_none=self.allow_none, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 268 | ) |
| 269 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 270 | return response.encode(self.encoding) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 271 | |
| 272 | def system_listMethods(self): |
| 273 | """system.listMethods() => ['add', 'subtract', 'multiple'] |
| 274 | |
| 275 | Returns a list of the methods supported by the server.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 276 | |
Hye-Shik Chang | 9604286 | 2007-08-19 10:49:11 +0000 | [diff] [blame] | 277 | methods = set(self.funcs.keys()) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 278 | if self.instance is not None: |
| 279 | # Instance can implement _listMethod to return a list of |
| 280 | # methods |
| 281 | if hasattr(self.instance, '_listMethods'): |
Hye-Shik Chang | 9604286 | 2007-08-19 10:49:11 +0000 | [diff] [blame] | 282 | methods |= set(self.instance._listMethods()) |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 283 | # if the instance has a _dispatch method then we |
| 284 | # don't have enough information to provide a list |
| 285 | # of methods |
| 286 | elif not hasattr(self.instance, '_dispatch'): |
Hye-Shik Chang | 9604286 | 2007-08-19 10:49:11 +0000 | [diff] [blame] | 287 | methods |= set(list_public_methods(self.instance)) |
| 288 | return sorted(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 |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 309 | if method_name in self.funcs: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 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)]) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 354 | except Fault as fault: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 355 | results.append( |
| 356 | {'faultCode' : fault.faultCode, |
| 357 | 'faultString' : fault.faultString} |
| 358 | ) |
| 359 | except: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 360 | exc_type, exc_value, exc_tb = sys.exc_info() |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 361 | results.append( |
| 362 | {'faultCode' : 1, |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 363 | 'faultString' : "%s:%s" % (exc_type, exc_value)} |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 364 | ) |
| 365 | return results |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 366 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 367 | def _dispatch(self, method, params): |
| 368 | """Dispatches the XML-RPC method. |
| 369 | |
| 370 | XML-RPC calls are forwarded to a registered function that |
| 371 | matches the called XML-RPC method name. If no such function |
| 372 | exists then the call is forwarded to the registered instance, |
| 373 | if available. |
| 374 | |
| 375 | If the registered instance has a _dispatch method then that |
| 376 | 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] | 377 | its parameters as a tuple |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 378 | e.g. instance._dispatch('add',(2,3)) |
| 379 | |
| 380 | If the registered instance does not have a _dispatch method |
| 381 | then the instance will be searched to find a matching method |
| 382 | and, if found, will be called. |
| 383 | |
| 384 | Methods beginning with an '_' are considered private and will |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 385 | not be called. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 386 | """ |
| 387 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 388 | func = None |
| 389 | try: |
| 390 | # check to see if a matching function has been registered |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 391 | func = self.funcs[method] |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 392 | except KeyError: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 393 | if self.instance is not None: |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 394 | # check for a _dispatch method |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 395 | if hasattr(self.instance, '_dispatch'): |
| 396 | return self.instance._dispatch(method, params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 397 | else: |
| 398 | # call instance method directly |
| 399 | try: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 400 | func = resolve_dotted_attribute( |
| 401 | self.instance, |
Guido van Rossum | d064142 | 2005-02-03 15:01:24 +0000 | [diff] [blame] | 402 | method, |
| 403 | self.allow_dotted_names |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 404 | ) |
| 405 | except AttributeError: |
| 406 | pass |
| 407 | |
| 408 | if func is not None: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 409 | return func(*params) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 410 | else: |
| 411 | raise Exception('method "%s" is not supported' % method) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 412 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 413 | class SimpleXMLRPCRequestHandler(BaseHTTPRequestHandler): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 414 | """Simple XML-RPC request handler class. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 415 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 416 | Handles all HTTP POST requests and attempts to decode them as |
| 417 | XML-RPC requests. |
| 418 | """ |
| 419 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 420 | # Class attribute listing the accessible path components; |
| 421 | # paths not on this list will result in a 404 error. |
| 422 | rpc_paths = ('/', '/RPC2') |
| 423 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 424 | #if not None, encode responses larger than this, if possible |
| 425 | encode_threshold = 1400 #a common MTU |
| 426 | |
| 427 | #Override form StreamRequestHandler: full buffering of output |
| 428 | #and no Nagle. |
| 429 | wbufsize = -1 |
| 430 | disable_nagle_algorithm = True |
| 431 | |
| 432 | # a re to match a gzip Accept-Encoding |
| 433 | aepattern = re.compile(r""" |
| 434 | \s* ([^\s;]+) \s* #content-coding |
| 435 | (;\s* q \s*=\s* ([0-9\.]+))? #q |
| 436 | """, re.VERBOSE | re.IGNORECASE) |
| 437 | |
| 438 | def accept_encodings(self): |
| 439 | r = {} |
| 440 | ae = self.headers.get("Accept-Encoding", "") |
| 441 | for e in ae.split(","): |
| 442 | match = self.aepattern.match(e) |
| 443 | if match: |
| 444 | v = match.group(3) |
| 445 | v = float(v) if v else 1.0 |
| 446 | r[match.group(1)] = v |
| 447 | return r |
| 448 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 449 | def is_rpc_path_valid(self): |
| 450 | if self.rpc_paths: |
| 451 | return self.path in self.rpc_paths |
| 452 | else: |
| 453 | # If .rpc_paths is empty, just assume all paths are legal |
| 454 | return True |
| 455 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 456 | def do_POST(self): |
| 457 | """Handles the HTTP POST request. |
| 458 | |
| 459 | Attempts to interpret all HTTP POST requests as XML-RPC calls, |
| 460 | which are forwarded to the server's _dispatch method for handling. |
| 461 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 462 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 463 | # Check that the path is legal |
| 464 | if not self.is_rpc_path_valid(): |
| 465 | self.report_404() |
| 466 | return |
| 467 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 468 | try: |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 469 | # Get arguments by reading body of request. |
| 470 | # We read this in chunks to avoid straining |
Andrew M. Kuchling | e63fde7 | 2005-12-04 15:36:57 +0000 | [diff] [blame] | 471 | # socket.read(); around the 10 or 15Mb mark, some platforms |
| 472 | # begin to have problems (bug #792570). |
| 473 | max_chunk_size = 10*1024*1024 |
| 474 | size_remaining = int(self.headers["content-length"]) |
| 475 | L = [] |
| 476 | while size_remaining: |
| 477 | chunk_size = min(size_remaining, max_chunk_size) |
| 478 | L.append(self.rfile.read(chunk_size)) |
| 479 | size_remaining -= len(L[-1]) |
Hye-Shik Chang | 9604286 | 2007-08-19 10:49:11 +0000 | [diff] [blame] | 480 | data = b''.join(L) |
Andrew M. Kuchling | e63fde7 | 2005-12-04 15:36:57 +0000 | [diff] [blame] | 481 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 482 | data = self.decode_request_content(data) |
| 483 | if data is None: |
| 484 | return #response has been sent |
| 485 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 486 | # In previous versions of SimpleXMLRPCServer, _dispatch |
| 487 | # could be overridden in this class, instead of in |
| 488 | # SimpleXMLRPCDispatcher. To maintain backwards compatibility, |
| 489 | # check to see if a subclass implements _dispatch and dispatch |
| 490 | # using that method if present. |
| 491 | response = self.server._marshaled_dispatch( |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 492 | data, getattr(self, '_dispatch', None), self.path |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 493 | ) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 494 | except Exception as e: # This should only happen if the module is buggy |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 495 | # internal error, report as HTTP server error |
| 496 | self.send_response(500) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 497 | |
| 498 | # Send information about the exception if requested |
| 499 | if hasattr(self.server, '_send_traceback_header') and \ |
| 500 | self.server._send_traceback_header: |
| 501 | self.send_header("X-exception", str(e)) |
Victor Stinner | 5bfe146 | 2010-04-16 13:28:05 +0000 | [diff] [blame] | 502 | trace = traceback.format_exc() |
| 503 | trace = str(trace.encode('ASCII', 'backslashreplace'), 'ASCII') |
| 504 | self.send_header("X-traceback", trace) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 505 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 506 | self.send_header("Content-length", "0") |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 507 | self.end_headers() |
| 508 | else: |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 509 | self.send_response(200) |
| 510 | self.send_header("Content-type", "text/xml") |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 511 | if self.encode_threshold is not None: |
| 512 | if len(response) > self.encode_threshold: |
| 513 | q = self.accept_encodings().get("gzip", 0) |
| 514 | if q: |
Kristján Valur Jónsson | aefde24 | 2009-07-19 22:29:24 +0000 | [diff] [blame] | 515 | try: |
| 516 | response = gzip_encode(response) |
| 517 | self.send_header("Content-Encoding", "gzip") |
| 518 | except NotImplementedError: |
| 519 | pass |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 520 | self.send_header("Content-length", str(len(response))) |
| 521 | self.end_headers() |
| 522 | self.wfile.write(response) |
| 523 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 524 | def decode_request_content(self, data): |
| 525 | #support gzip encoding of request |
| 526 | encoding = self.headers.get("content-encoding", "identity").lower() |
| 527 | if encoding == "identity": |
| 528 | return data |
| 529 | if encoding == "gzip": |
| 530 | try: |
| 531 | return gzip_decode(data) |
Kristján Valur Jónsson | aefde24 | 2009-07-19 22:29:24 +0000 | [diff] [blame] | 532 | except NotImplementedError: |
| 533 | self.send_response(501, "encoding %r not supported" % encoding) |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 534 | except ValueError: |
| 535 | self.send_response(400, "error decoding gzip content") |
| 536 | else: |
| 537 | self.send_response(501, "encoding %r not supported" % encoding) |
| 538 | self.send_header("Content-length", "0") |
| 539 | self.end_headers() |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 540 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 541 | def report_404 (self): |
| 542 | # Report a 404 error |
| 543 | self.send_response(404) |
Christian Heimes | 0aa93cd | 2007-12-08 18:38:20 +0000 | [diff] [blame] | 544 | response = b'No such page' |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 545 | self.send_header("Content-type", "text/plain") |
| 546 | self.send_header("Content-length", str(len(response))) |
| 547 | self.end_headers() |
| 548 | self.wfile.write(response) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 549 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 550 | def log_request(self, code='-', size='-'): |
| 551 | """Selectively log an accepted request.""" |
| 552 | |
| 553 | if self.server.logRequests: |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 554 | BaseHTTPRequestHandler.log_request(self, code, size) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 555 | |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 556 | class SimpleXMLRPCServer(socketserver.TCPServer, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 557 | SimpleXMLRPCDispatcher): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 558 | """Simple XML-RPC server. |
| 559 | |
| 560 | 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] | 561 | to be installed to handle requests. The default implementation |
| 562 | attempts to dispatch XML-RPC calls to the functions or instance |
| 563 | installed in the server. Override the _dispatch method inhereted |
| 564 | from SimpleXMLRPCDispatcher to change this behavior. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 565 | """ |
| 566 | |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 567 | allow_reuse_address = True |
| 568 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 569 | # Warning: this is for debugging purposes only! Never set this to True in |
| 570 | # production code, as will be sending out sensitive information (exception |
| 571 | # and stack trace details) when exceptions are raised inside |
| 572 | # SimpleXMLRPCRequestHandler.do_POST |
| 573 | _send_traceback_header = False |
| 574 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 575 | def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler, |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 576 | logRequests=True, allow_none=False, encoding=None, bind_and_activate=True): |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 577 | self.logRequests = logRequests |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 578 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 579 | SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding) |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 580 | socketserver.TCPServer.__init__(self, addr, requestHandler, bind_and_activate) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 581 | |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 582 | # [Bug #1222790] If possible, set close-on-exec flag; if a |
| 583 | # method spawns a subprocess, the subprocess shouldn't have |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 584 | # the listening socket open. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 585 | if fcntl is not None and hasattr(fcntl, 'FD_CLOEXEC'): |
Andrew M. Kuchling | 3a97605 | 2005-12-04 15:07:41 +0000 | [diff] [blame] | 586 | flags = fcntl.fcntl(self.fileno(), fcntl.F_GETFD) |
| 587 | flags |= fcntl.FD_CLOEXEC |
| 588 | fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags) |
| 589 | |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 590 | class MultiPathXMLRPCServer(SimpleXMLRPCServer): |
| 591 | """Multipath XML-RPC Server |
| 592 | This specialization of SimpleXMLRPCServer allows the user to create |
| 593 | multiple Dispatcher instances and assign them to different |
| 594 | HTTP request paths. This makes it possible to run two or more |
| 595 | 'virtual XML-RPC servers' at the same port. |
| 596 | Make sure that the requestHandler accepts the paths in question. |
| 597 | """ |
| 598 | def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler, |
| 599 | logRequests=True, allow_none=False, encoding=None, bind_and_activate=True): |
| 600 | |
| 601 | SimpleXMLRPCServer.__init__(self, addr, requestHandler, logRequests, allow_none, |
| 602 | encoding, bind_and_activate) |
| 603 | self.dispatchers = {} |
| 604 | self.allow_none = allow_none |
| 605 | self.encoding = encoding |
| 606 | |
| 607 | def add_dispatcher(self, path, dispatcher): |
| 608 | self.dispatchers[path] = dispatcher |
| 609 | return dispatcher |
| 610 | |
| 611 | def get_dispatcher(self, path): |
| 612 | return self.dispatchers[path] |
| 613 | |
| 614 | def _marshaled_dispatch(self, data, dispatch_method = None, path = None): |
| 615 | try: |
| 616 | response = self.dispatchers[path]._marshaled_dispatch( |
| 617 | data, dispatch_method, path) |
| 618 | except: |
| 619 | # report low level exception back to server |
| 620 | # (each dispatcher should have handled their own |
| 621 | # exceptions) |
| 622 | exc_type, exc_value = sys.exc_info()[:2] |
| 623 | response = xmlrpclib.dumps( |
| 624 | xmlrpclib.Fault(1, "%s:%s" % (exc_type, exc_value)), |
| 625 | encoding=self.encoding, allow_none=self.allow_none) |
| 626 | return response |
| 627 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 628 | class CGIXMLRPCRequestHandler(SimpleXMLRPCDispatcher): |
| 629 | """Simple handler for XML-RPC data passed through CGI.""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 630 | |
Andrew M. Kuchling | 427aedb | 2005-12-04 17:13:12 +0000 | [diff] [blame] | 631 | def __init__(self, allow_none=False, encoding=None): |
| 632 | SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 633 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 634 | def handle_xmlrpc(self, request_text): |
| 635 | """Handle a single XML-RPC request""" |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 636 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 637 | response = self._marshaled_dispatch(request_text) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 638 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 639 | print('Content-Type: text/xml') |
| 640 | print('Content-Length: %d' % len(response)) |
| 641 | print() |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 642 | sys.stdout.flush() |
| 643 | sys.stdout.buffer.write(response) |
| 644 | sys.stdout.buffer.flush() |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 645 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 646 | def handle_get(self): |
| 647 | """Handle a single HTTP GET request. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 648 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 649 | Default implementation indicates an error because |
| 650 | XML-RPC uses the POST method. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 651 | """ |
| 652 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 653 | code = 400 |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 654 | message, explain = BaseHTTPRequestHandler.responses[code] |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 655 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 656 | response = http.server.DEFAULT_ERROR_MESSAGE % \ |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 657 | { |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 658 | 'code' : code, |
| 659 | 'message' : message, |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 660 | 'explain' : explain |
| 661 | } |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 662 | response = response.encode('utf-8') |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 663 | print('Status: %d %s' % (code, message)) |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 664 | print('Content-Type: %s' % http.server.DEFAULT_ERROR_CONTENT_TYPE) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 665 | print('Content-Length: %d' % len(response)) |
| 666 | print() |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 667 | sys.stdout.flush() |
| 668 | sys.stdout.buffer.write(response) |
| 669 | sys.stdout.buffer.flush() |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 670 | |
Georg Brandl | fe99105 | 2009-09-16 15:54:04 +0000 | [diff] [blame] | 671 | def handle_request(self, request_text=None): |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 672 | """Handle a single XML-RPC request passed through a CGI post method. |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 673 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 674 | If no XML data is given then it is read from stdin. The resulting |
| 675 | XML-RPC response is printed to stdout along with the correct HTTP |
| 676 | headers. |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 677 | """ |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 678 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 679 | if request_text is None and \ |
| 680 | os.environ.get('REQUEST_METHOD', None) == 'GET': |
| 681 | self.handle_get() |
| 682 | else: |
| 683 | # POST data is normally available through stdin |
Georg Brandl | 99412e5 | 2009-04-01 04:27:47 +0000 | [diff] [blame] | 684 | try: |
| 685 | length = int(os.environ.get('CONTENT_LENGTH', None)) |
Georg Brandl | c748506 | 2009-04-01 15:53:15 +0000 | [diff] [blame] | 686 | except (ValueError, TypeError): |
Georg Brandl | 99412e5 | 2009-04-01 04:27:47 +0000 | [diff] [blame] | 687 | length = -1 |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 688 | if request_text is None: |
Georg Brandl | 99412e5 | 2009-04-01 04:27:47 +0000 | [diff] [blame] | 689 | request_text = sys.stdin.read(length) |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 690 | |
Martin v. Löwis | d69663d | 2003-01-15 11:37:23 +0000 | [diff] [blame] | 691 | self.handle_xmlrpc(request_text) |
Tim Peters | 2c60f7a | 2003-01-29 03:49:43 +0000 | [diff] [blame] | 692 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 693 | |
| 694 | # ----------------------------------------------------------------------------- |
| 695 | # Self documenting XML-RPC Server. |
| 696 | |
| 697 | class ServerHTMLDoc(pydoc.HTMLDoc): |
| 698 | """Class used to generate pydoc HTML document for a server""" |
| 699 | |
| 700 | def markup(self, text, escape=None, funcs={}, classes={}, methods={}): |
| 701 | """Mark up some plain text, given a context of symbols to look for. |
| 702 | Each context dictionary maps object names to anchor names.""" |
| 703 | escape = escape or self.escape |
| 704 | results = [] |
| 705 | here = 0 |
| 706 | |
| 707 | # XXX Note that this regular expression does not allow for the |
| 708 | # hyperlinking of arbitrary strings being used as method |
| 709 | # names. Only methods with names consisting of word characters |
| 710 | # and '.'s are hyperlinked. |
| 711 | pattern = re.compile(r'\b((http|ftp)://\S+[\w/]|' |
| 712 | r'RFC[- ]?(\d+)|' |
| 713 | r'PEP[- ]?(\d+)|' |
| 714 | r'(self\.)?((?:\w|\.)+))\b') |
| 715 | while 1: |
| 716 | match = pattern.search(text, here) |
| 717 | if not match: break |
| 718 | start, end = match.span() |
| 719 | results.append(escape(text[here:start])) |
| 720 | |
| 721 | all, scheme, rfc, pep, selfdot, name = match.groups() |
| 722 | if scheme: |
| 723 | url = escape(all).replace('"', '"') |
| 724 | results.append('<a href="%s">%s</a>' % (url, url)) |
| 725 | elif rfc: |
| 726 | url = 'http://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc) |
| 727 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 728 | elif pep: |
| 729 | url = 'http://www.python.org/dev/peps/pep-%04d/' % int(pep) |
| 730 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 731 | elif text[end:end+1] == '(': |
| 732 | results.append(self.namelink(name, methods, funcs, classes)) |
| 733 | elif selfdot: |
| 734 | results.append('self.<strong>%s</strong>' % name) |
| 735 | else: |
| 736 | results.append(self.namelink(name, classes)) |
| 737 | here = end |
| 738 | results.append(escape(text[here:])) |
| 739 | return ''.join(results) |
| 740 | |
| 741 | def docroutine(self, object, name, mod=None, |
| 742 | funcs={}, classes={}, methods={}, cl=None): |
| 743 | """Produce HTML documentation for a function or method object.""" |
| 744 | |
| 745 | anchor = (cl and cl.__name__ or '') + '-' + name |
| 746 | note = '' |
| 747 | |
| 748 | title = '<a name="%s"><strong>%s</strong></a>' % ( |
| 749 | self.escape(anchor), self.escape(name)) |
| 750 | |
| 751 | if inspect.ismethod(object): |
| 752 | args, varargs, varkw, defaults = inspect.getargspec(object) |
| 753 | # exclude the argument bound to the instance, it will be |
| 754 | # confusing to the non-Python user |
| 755 | argspec = inspect.formatargspec ( |
| 756 | args[1:], |
| 757 | varargs, |
| 758 | varkw, |
| 759 | defaults, |
| 760 | formatvalue=self.formatvalue |
| 761 | ) |
| 762 | elif inspect.isfunction(object): |
| 763 | args, varargs, varkw, defaults = inspect.getargspec(object) |
| 764 | argspec = inspect.formatargspec( |
| 765 | args, varargs, varkw, defaults, formatvalue=self.formatvalue) |
| 766 | else: |
| 767 | argspec = '(...)' |
| 768 | |
| 769 | if isinstance(object, tuple): |
| 770 | argspec = object[0] or argspec |
| 771 | docstring = object[1] or "" |
| 772 | else: |
| 773 | docstring = pydoc.getdoc(object) |
| 774 | |
| 775 | decl = title + argspec + (note and self.grey( |
| 776 | '<font face="helvetica, arial">%s</font>' % note)) |
| 777 | |
| 778 | doc = self.markup( |
| 779 | docstring, self.preformat, funcs, classes, methods) |
| 780 | doc = doc and '<dd><tt>%s</tt></dd>' % doc |
| 781 | return '<dl><dt>%s</dt>%s</dl>\n' % (decl, doc) |
| 782 | |
| 783 | def docserver(self, server_name, package_documentation, methods): |
| 784 | """Produce HTML documentation for an XML-RPC server.""" |
| 785 | |
| 786 | fdict = {} |
| 787 | for key, value in methods.items(): |
| 788 | fdict[key] = '#-' + key |
| 789 | fdict[value] = fdict[key] |
| 790 | |
| 791 | server_name = self.escape(server_name) |
| 792 | head = '<big><big><strong>%s</strong></big></big>' % server_name |
| 793 | result = self.heading(head, '#ffffff', '#7799ee') |
| 794 | |
| 795 | doc = self.markup(package_documentation, self.preformat, fdict) |
| 796 | doc = doc and '<tt>%s</tt>' % doc |
| 797 | result = result + '<p>%s</p>\n' % doc |
| 798 | |
| 799 | contents = [] |
| 800 | method_items = sorted(methods.items()) |
| 801 | for key, value in method_items: |
| 802 | contents.append(self.docroutine(value, key, funcs=fdict)) |
| 803 | result = result + self.bigsection( |
| 804 | 'Methods', '#ffffff', '#eeaa77', ''.join(contents)) |
| 805 | |
| 806 | return result |
| 807 | |
| 808 | class XMLRPCDocGenerator: |
| 809 | """Generates documentation for an XML-RPC server. |
| 810 | |
| 811 | This class is designed as mix-in and should not |
| 812 | be constructed directly. |
| 813 | """ |
| 814 | |
| 815 | def __init__(self): |
| 816 | # setup variables used for HTML documentation |
| 817 | self.server_name = 'XML-RPC Server Documentation' |
| 818 | self.server_documentation = \ |
| 819 | "This server exports the following methods through the XML-RPC "\ |
| 820 | "protocol." |
| 821 | self.server_title = 'XML-RPC Server Documentation' |
| 822 | |
| 823 | def set_server_title(self, server_title): |
| 824 | """Set the HTML title of the generated server documentation""" |
| 825 | |
| 826 | self.server_title = server_title |
| 827 | |
| 828 | def set_server_name(self, server_name): |
| 829 | """Set the name of the generated HTML server documentation""" |
| 830 | |
| 831 | self.server_name = server_name |
| 832 | |
| 833 | def set_server_documentation(self, server_documentation): |
| 834 | """Set the documentation string for the entire server.""" |
| 835 | |
| 836 | self.server_documentation = server_documentation |
| 837 | |
| 838 | def generate_html_documentation(self): |
| 839 | """generate_html_documentation() => html documentation for the server |
| 840 | |
| 841 | Generates HTML documentation for the server using introspection for |
| 842 | installed functions and instances that do not implement the |
| 843 | _dispatch method. Alternatively, instances can choose to implement |
| 844 | the _get_method_argstring(method_name) method to provide the |
| 845 | argument string used in the documentation and the |
| 846 | _methodHelp(method_name) method to provide the help text used |
| 847 | in the documentation.""" |
| 848 | |
| 849 | methods = {} |
| 850 | |
| 851 | for method_name in self.system_listMethods(): |
| 852 | if method_name in self.funcs: |
| 853 | method = self.funcs[method_name] |
| 854 | elif self.instance is not None: |
| 855 | method_info = [None, None] # argspec, documentation |
| 856 | if hasattr(self.instance, '_get_method_argstring'): |
| 857 | method_info[0] = self.instance._get_method_argstring(method_name) |
| 858 | if hasattr(self.instance, '_methodHelp'): |
| 859 | method_info[1] = self.instance._methodHelp(method_name) |
| 860 | |
| 861 | method_info = tuple(method_info) |
| 862 | if method_info != (None, None): |
| 863 | method = method_info |
| 864 | elif not hasattr(self.instance, '_dispatch'): |
| 865 | try: |
| 866 | method = resolve_dotted_attribute( |
| 867 | self.instance, |
| 868 | method_name |
| 869 | ) |
| 870 | except AttributeError: |
| 871 | method = method_info |
| 872 | else: |
| 873 | method = method_info |
| 874 | else: |
| 875 | assert 0, "Could not find method in self.functions and no "\ |
| 876 | "instance installed" |
| 877 | |
| 878 | methods[method_name] = method |
| 879 | |
| 880 | documenter = ServerHTMLDoc() |
| 881 | documentation = documenter.docserver( |
| 882 | self.server_name, |
| 883 | self.server_documentation, |
| 884 | methods |
| 885 | ) |
| 886 | |
| 887 | return documenter.page(self.server_title, documentation) |
| 888 | |
| 889 | class DocXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): |
| 890 | """XML-RPC and documentation request handler class. |
| 891 | |
| 892 | Handles all HTTP POST requests and attempts to decode them as |
| 893 | XML-RPC requests. |
| 894 | |
| 895 | Handles all HTTP GET requests and interprets them as requests |
| 896 | for documentation. |
| 897 | """ |
| 898 | |
| 899 | def do_GET(self): |
| 900 | """Handles the HTTP GET request. |
| 901 | |
| 902 | Interpret all HTTP GET requests as requests for server |
| 903 | documentation. |
| 904 | """ |
| 905 | # Check that the path is legal |
| 906 | if not self.is_rpc_path_valid(): |
| 907 | self.report_404() |
| 908 | return |
| 909 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 910 | response = self.server.generate_html_documentation().encode('utf-8') |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 911 | self.send_response(200) |
| 912 | self.send_header("Content-type", "text/html") |
| 913 | self.send_header("Content-length", str(len(response))) |
| 914 | self.end_headers() |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 915 | self.wfile.write(response) |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 916 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 917 | class DocXMLRPCServer( SimpleXMLRPCServer, |
| 918 | XMLRPCDocGenerator): |
| 919 | """XML-RPC and HTML documentation server. |
| 920 | |
| 921 | Adds the ability to serve server documentation to the capabilities |
| 922 | of SimpleXMLRPCServer. |
| 923 | """ |
| 924 | |
| 925 | def __init__(self, addr, requestHandler=DocXMLRPCRequestHandler, |
Georg Brandl | fe99105 | 2009-09-16 15:54:04 +0000 | [diff] [blame] | 926 | logRequests=True, allow_none=False, encoding=None, |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 927 | bind_and_activate=True): |
| 928 | SimpleXMLRPCServer.__init__(self, addr, requestHandler, logRequests, |
| 929 | allow_none, encoding, bind_and_activate) |
| 930 | XMLRPCDocGenerator.__init__(self) |
| 931 | |
| 932 | class DocCGIXMLRPCRequestHandler( CGIXMLRPCRequestHandler, |
| 933 | XMLRPCDocGenerator): |
| 934 | """Handler for XML-RPC data and documentation requests passed through |
| 935 | CGI""" |
| 936 | |
| 937 | def handle_get(self): |
| 938 | """Handles the HTTP GET request. |
| 939 | |
| 940 | Interpret all HTTP GET requests as requests for server |
| 941 | documentation. |
| 942 | """ |
| 943 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 944 | response = self.generate_html_documentation().encode('utf-8') |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 945 | |
| 946 | print('Content-Type: text/html') |
| 947 | print('Content-Length: %d' % len(response)) |
| 948 | print() |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 949 | sys.stdout.flush() |
| 950 | sys.stdout.buffer.write(response) |
| 951 | sys.stdout.buffer.flush() |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 952 | |
| 953 | def __init__(self): |
| 954 | CGIXMLRPCRequestHandler.__init__(self) |
| 955 | XMLRPCDocGenerator.__init__(self) |
| 956 | |
| 957 | |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 958 | if __name__ == '__main__': |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 959 | print('Running XML-RPC server on port 8000') |
Fredrik Lundh | b329b71 | 2001-09-17 17:35:21 +0000 | [diff] [blame] | 960 | server = SimpleXMLRPCServer(("localhost", 8000)) |
| 961 | server.register_function(pow) |
| 962 | server.register_function(lambda x,y: x+y, 'add') |
| 963 | server.serve_forever() |