blob: c736e2a444cb2858db9f51bc3bea9631e2f27596 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`SimpleXMLRPCServer` --- Basic XML-RPC server
3==================================================
4
5.. module:: SimpleXMLRPCServer
6 :synopsis: Basic XML-RPC server implementation.
7.. moduleauthor:: Brian Quinlan <brianq@activestate.com>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011The :mod:`SimpleXMLRPCServer` module provides a basic server framework for
12XML-RPC servers written in Python. Servers can either be free standing, using
13:class:`SimpleXMLRPCServer`, or embedded in a CGI environment, using
14:class:`CGIXMLRPCRequestHandler`.
15
16
Georg Brandl55ac8f02007-09-01 13:51:09 +000017.. class:: SimpleXMLRPCServer(addr[, requestHandler[, logRequests[, allow_none[, encoding[, bind_and_activate]]]]])
Georg Brandl116aa622007-08-15 14:28:22 +000018
19 Create a new server instance. This class provides methods for registration of
20 functions that can be called by the XML-RPC protocol. The *requestHandler*
21 parameter should be a factory for request handler instances; it defaults to
22 :class:`SimpleXMLRPCRequestHandler`. The *addr* and *requestHandler* parameters
23 are passed to the :class:`SocketServer.TCPServer` constructor. If *logRequests*
24 is true (the default), requests will be logged; setting this parameter to false
25 will turn off logging. The *allow_none* and *encoding* parameters are passed
26 on to :mod:`xmlrpclib` and control the XML-RPC responses that will be returned
27 from the server. The *bind_and_activate* parameter controls whether
28 :meth:`server_bind` and :meth:`server_activate` are called immediately by the
29 constructor; it defaults to true. Setting it to false allows code to manipulate
30 the *allow_reuse_address* class variable before the address is bound.
31
Georg Brandl116aa622007-08-15 14:28:22 +000032
33.. class:: CGIXMLRPCRequestHandler([allow_none[, encoding]])
34
35 Create a new instance to handle XML-RPC requests in a CGI environment. The
36 *allow_none* and *encoding* parameters are passed on to :mod:`xmlrpclib` and
37 control the XML-RPC responses that will be returned from the server.
38
Georg Brandl116aa622007-08-15 14:28:22 +000039
40.. class:: SimpleXMLRPCRequestHandler()
41
42 Create a new request handler instance. This request handler supports ``POST``
43 requests and modifies logging so that the *logRequests* parameter to the
44 :class:`SimpleXMLRPCServer` constructor parameter is honored.
45
46
47.. _simple-xmlrpc-servers:
48
49SimpleXMLRPCServer Objects
50--------------------------
51
52The :class:`SimpleXMLRPCServer` class is based on
53:class:`SocketServer.TCPServer` and provides a means of creating simple, stand
54alone XML-RPC servers.
55
56
57.. method:: SimpleXMLRPCServer.register_function(function[, name])
58
59 Register a function that can respond to XML-RPC requests. If *name* is given,
60 it will be the method name associated with *function*, otherwise
61 ``function.__name__`` will be used. *name* can be either a normal or Unicode
62 string, and may contain characters not legal in Python identifiers, including
63 the period character.
64
65
66.. method:: SimpleXMLRPCServer.register_instance(instance[, allow_dotted_names])
67
68 Register an object which is used to expose method names which have not been
69 registered using :meth:`register_function`. If *instance* contains a
70 :meth:`_dispatch` method, it is called with the requested method name and the
71 parameters from the request. Its API is ``def _dispatch(self, method, params)``
72 (note that *params* does not represent a variable argument list). If it calls
73 an underlying function to perform its task, that function is called as
74 ``func(*params)``, expanding the parameter list. The return value from
75 :meth:`_dispatch` is returned to the client as the result. If *instance* does
76 not have a :meth:`_dispatch` method, it is searched for an attribute matching
77 the name of the requested method.
78
79 If the optional *allow_dotted_names* argument is true and the instance does not
80 have a :meth:`_dispatch` method, then if the requested method name contains
81 periods, each component of the method name is searched for individually, with
82 the effect that a simple hierarchical search is performed. The value found from
83 this search is then called with the parameters from the request, and the return
84 value is passed back to the client.
85
86 .. warning::
87
88 Enabling the *allow_dotted_names* option allows intruders to access your
89 module's global variables and may allow intruders to execute arbitrary code on
90 your machine. Only use this option on a secure, closed network.
91
Georg Brandl116aa622007-08-15 14:28:22 +000092
93.. method:: SimpleXMLRPCServer.register_introspection_functions()
94
95 Registers the XML-RPC introspection functions ``system.listMethods``,
96 ``system.methodHelp`` and ``system.methodSignature``.
97
Georg Brandl116aa622007-08-15 14:28:22 +000098
99.. method:: SimpleXMLRPCServer.register_multicall_functions()
100
101 Registers the XML-RPC multicall function system.multicall.
102
103
Christian Heimes8640e742008-02-23 16:23:06 +0000104.. attribute:: SimpleXMLRPCRequestHandler.rpc_paths
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106 An attribute value that must be a tuple listing valid path portions of the URL
107 for receiving XML-RPC requests. Requests posted to other paths will result in a
108 404 "no such page" HTTP error. If this tuple is empty, all paths will be
109 considered valid. The default value is ``('/', '/RPC2')``.
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000112.. _simplexmlrpcserver-example:
113
114SimpleXMLRPCServer Example
115^^^^^^^^^^^^^^^^^^^^^^^^^^
116Server code::
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118 from SimpleXMLRPCServer import SimpleXMLRPCServer
Christian Heimes8640e742008-02-23 16:23:06 +0000119 from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
120
121 # Restrict to a particular path.
122 class RequestHandler(SimpleXMLRPCRequestHandler):
123 rpc_paths = ('/RPC2',)
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125 # Create server
Christian Heimes8640e742008-02-23 16:23:06 +0000126 server = SimpleXMLRPCServer(("localhost", 8000),
127 requestHandler=RequestHandler)
Georg Brandl116aa622007-08-15 14:28:22 +0000128 server.register_introspection_functions()
129
130 # Register pow() function; this will use the value of
131 # pow.__name__ as the name, which is just 'pow'.
132 server.register_function(pow)
133
134 # Register a function under a different name
135 def adder_function(x,y):
136 return x + y
137 server.register_function(adder_function, 'add')
138
139 # Register an instance; all the methods of the instance are
140 # published as XML-RPC methods (in this case, just 'div').
141 class MyFuncs:
142 def div(self, x, y):
143 return x // y
144
145 server.register_instance(MyFuncs())
146
147 # Run the server's main loop
148 server.serve_forever()
149
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000150The following client code will call the methods made available by the preceding
Georg Brandl116aa622007-08-15 14:28:22 +0000151server::
152
153 import xmlrpclib
154
Thomas Wouters1b7f8912007-09-19 03:06:30 +0000155 s = xmlrpclib.ServerProxy('http://localhost:8000')
Georg Brandl6911e3c2007-09-04 07:15:32 +0000156 print(s.pow(2,3)) # Returns 2**3 = 8
157 print(s.add(2,3)) # Returns 5
Georg Brandlf6945182008-02-01 11:56:49 +0000158 print(s.mul(5,2)) # Returns 5*2 = 10
Georg Brandl116aa622007-08-15 14:28:22 +0000159
160 # Print list of available methods
Georg Brandl6911e3c2007-09-04 07:15:32 +0000161 print(s.system.listMethods())
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163
164CGIXMLRPCRequestHandler
165-----------------------
166
167The :class:`CGIXMLRPCRequestHandler` class can be used to handle XML-RPC
168requests sent to Python CGI scripts.
169
170
171.. method:: CGIXMLRPCRequestHandler.register_function(function[, name])
172
173 Register a function that can respond to XML-RPC requests. If *name* is given,
174 it will be the method name associated with function, otherwise
175 *function.__name__* will be used. *name* can be either a normal or Unicode
176 string, and may contain characters not legal in Python identifiers, including
177 the period character.
178
179
180.. method:: CGIXMLRPCRequestHandler.register_instance(instance)
181
182 Register an object which is used to expose method names which have not been
183 registered using :meth:`register_function`. If instance contains a
184 :meth:`_dispatch` method, it is called with the requested method name and the
185 parameters from the request; the return value is returned to the client as the
186 result. If instance does not have a :meth:`_dispatch` method, it is searched
187 for an attribute matching the name of the requested method; if the requested
188 method name contains periods, each component of the method name is searched for
189 individually, with the effect that a simple hierarchical search is performed.
190 The value found from this search is then called with the parameters from the
191 request, and the return value is passed back to the client.
192
193
194.. method:: CGIXMLRPCRequestHandler.register_introspection_functions()
195
196 Register the XML-RPC introspection functions ``system.listMethods``,
197 ``system.methodHelp`` and ``system.methodSignature``.
198
199
200.. method:: CGIXMLRPCRequestHandler.register_multicall_functions()
201
202 Register the XML-RPC multicall function ``system.multicall``.
203
204
205.. method:: CGIXMLRPCRequestHandler.handle_request([request_text = None])
206
207 Handle a XML-RPC request. If *request_text* is given, it should be the POST
208 data provided by the HTTP server, otherwise the contents of stdin will be used.
209
210Example::
211
212 class MyFuncs:
213 def div(self, x, y) : return x // y
214
215
216 handler = CGIXMLRPCRequestHandler()
217 handler.register_function(pow)
218 handler.register_function(lambda x,y: x+y, 'add')
219 handler.register_introspection_functions()
220 handler.register_instance(MyFuncs())
221 handler.handle_request()
222