1 """Simple XML-RPC Server.
3 This module can be used to create simple XML-RPC servers
4 by creating a server and either installing functions, a
5 class instance, or by extending the SimpleXMLRPCRequestHandler
8 A list of possible usage patterns follows:
12 server = SimpleXMLRPCServer(("localhost", 8000))
13 server.register_function(pow)
14 server.register_function(lambda x,y: x+y, 'add')
15 server.serve_forever()
17 2. Install an instance:
21 # make all of the string functions available through
25 def pow(self, x, y): return pow(x, y)
26 def add(self, x, y) : return x + y
27 server = SimpleXMLRPCServer(("localhost", 8000))
28 server.register_instance(MyFuncs())
29 server.serve_forever()
31 3. Install an instance with custom dispatch method:
34 def _dispatch(self, method, params):
36 return apply(pow, params)
38 return params[0] + params[1]
41 server = SimpleXMLRPCServer(("localhost", 8000))
42 server.register_instance(Math())
43 server.serve_forever()
45 4. Subclass SimpleXMLRPCRequestHandler:
47 class MathHandler(SimpleXMLRPCRequestHandler):
48 def _dispatch(self, method, params):
50 # We are forcing the 'export_' prefix on methods that are
51 # callable through XML-RPC to prevent potential security
53 func = getattr(self, 'export_' + method)
54 except AttributeError:
55 raise Exception('method "%s" is not supported' % method)
57 return apply(func, params)
59 def log_message(self, format, *args):
60 pass # maybe do something fancy like write the messages to a file
62 def export_add(self, x, y):
65 server = SimpleXMLRPCServer(("localhost", 8000), MathHandler)
66 server.serve_forever()
78 """Simple XML-RPC request handler class.
80 Handles all HTTP POST requests and attempts to decode them as
83 XML-RPC requests are dispatched to the _dispatch method, which
84 may be overriden by subclasses. The default implementation attempts
85 to dispatch XML-RPC calls to the functions or instance installed
90 """Handles the HTTP POST request.
92 Attempts to interpret all HTTP POST requests as XML-RPC calls,
93 which are forwarded to the _dispatch method for handling.
98 data = self.rfile.read(int(self.
headers[
"content-length"]))
103 response = self.
_dispatch(method, params)
105 response = (response,)
123 self.wfile.write(response)
127 self.connection.shutdown(1)
129 def _dispatch(self, method, params):
130 """Dispatches the XML-RPC method.
132 XML-RPC calls are forwarded to a registered function that
133 matches the called XML-RPC method name. If no such function
134 exists then the call is forwarded to the registered instance,
137 If the registered instance has a _dispatch method then that
138 method will be called with the name of the XML-RPC method and
139 it's parameters as a tuple
140 e.g. instance._dispatch('add',(2,3))
142 If the registered instance does not have a _dispatch method
143 then the instance will be searched to find a matching method
144 and, if found, will be called.
146 Methods beginning with an '_' are considered private and will
147 not be called by SimpleXMLRPCServer.
153 func = self.server.funcs[method]
155 if self.server.instance
is not None:
157 if hasattr(self.server.instance,
'_dispatch'):
158 return self.server.instance._dispatch(method, params)
162 func = _resolve_dotted_attribute(
163 self.server.instance,
166 except AttributeError:
170 return apply(func, params)
172 raise Exception(
'method "%s" is not supported' % method)
175 """Selectively log an accepted request."""
177 if self.server.logRequests:
181 def _resolve_dotted_attribute(obj, attr):
182 """Resolves a dotted attribute name to an object. Raises
183 an AttributeError if any attribute in the chain starts with a '_'.
185 for i
in attr.split(
'.'):
186 if i.startswith(
'_'):
187 raise AttributeError(
188 'attempt to access private attribute "%s"' % i
196 """Simple XML-RPC server.
198 Simple XML-RPC server that allows functions and a single instance
199 to be installed to handle requests.
202 def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler,
210 """Registers an instance to respond to XML-RPC requests.
212 Only one instance can be installed at a time.
214 If the registered instance has a _dispatch method then that
215 method will be called with the name of the XML-RPC method and
216 it's parameters as a tuple
217 e.g. instance._dispatch('add',(2,3))
219 If the registered instance does not have a _dispatch method
220 then the instance will be searched to find a matching method
221 and, if found, will be called.
223 Methods beginning with an '_' are considered private and will
224 not be called by SimpleXMLRPCServer.
226 If a registered function matches a XML-RPC request, then it
227 will be called instead of the registered instance.
233 """Registers a function to respond to XML-RPC requests.
235 The optional name argument can be used to set a Unicode name
238 If an instance is also registered then it will only be called
239 if a matching function is not found.
243 name = function.__name__
244 self.
funcs[name] = function
246 if __name__ ==
'__main__':
248 server.register_function(pow)
249 server.register_function(
lambda x,y: x+y,
'add')
250 server.serve_forever()