@@ -79,13 +79,19 @@ The :class:`SimpleXMLRPCServer` class is based on
79
79
alone XML-RPC servers.
80
80
81
81
82
- .. method :: SimpleXMLRPCServer.register_function(function, name=None)
82
+ .. method :: SimpleXMLRPCServer.register_function(function=None , name=None)
83
83
84
84
Register a function that can respond to XML-RPC requests. If *name * is given,
85
85
it will be the method name associated with *function *, otherwise
86
- ``function.__name__ `` will be used. *name * can be either a normal or Unicode
87
- string, and may contain characters not legal in Python identifiers, including
88
- the period character.
86
+ ``function.__name__ `` will be used. *name * is a string, and may contain
87
+ characters not legal in Python identifiers, including the period character.
88
+
89
+ This method can also be used as a decorator. When used as a decorator,
90
+ *name * can only be given as a keyword argument to register *function * under
91
+ *name *. If no *name * is given, ``function.__name__ `` will be used.
92
+
93
+ .. versionchanged :: 3.7
94
+ :meth: `register_function ` can be used as a decorator.
89
95
90
96
91
97
.. method :: SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False)
@@ -148,7 +154,7 @@ Server code::
148
154
rpc_paths = ('/RPC2',)
149
155
150
156
# Create server
151
- with SimpleXMLRPCServer((" localhost" , 8000),
157
+ with SimpleXMLRPCServer((' localhost' , 8000),
152
158
requestHandler=RequestHandler) as server:
153
159
server.register_introspection_functions()
154
160
@@ -157,7 +163,7 @@ Server code::
157
163
server.register_function(pow)
158
164
159
165
# Register a function under a different name
160
- def adder_function(x,y):
166
+ def adder_function(x, y):
161
167
return x + y
162
168
server.register_function(adder_function, 'add')
163
169
@@ -185,6 +191,37 @@ server::
185
191
# Print list of available methods
186
192
print(s.system.listMethods())
187
193
194
+ :meth: `register_function ` can also be used as a decorator. The previous server
195
+ example can register functions in a decorator way::
196
+
197
+ from xmlrpc.server import SimpleXMLRPCServer
198
+ from xmlrpc.server import SimpleXMLRPCRequestHandler
199
+
200
+ class RequestHandler(SimpleXMLRPCRequestHandler):
201
+ rpc_paths = ('/RPC2',)
202
+
203
+ with SimpleXMLRPCServer(('localhost', 8000),
204
+ requestHandler=RequestHandler) as server:
205
+ server.register_introspection_functions()
206
+
207
+ # Register pow() function; this will use the value of
208
+ # pow.__name__ as the name, which is just 'pow'.
209
+ server.register_function(pow)
210
+
211
+ # Register a function under a different name, using
212
+ # register_function as a decorator. *name* can only be given
213
+ # as a keyword argument.
214
+ @server.register_function(name='add')
215
+ def adder_function(x, y):
216
+ return x + y
217
+
218
+ # Register a function under function.__name__.
219
+ @server.register_function
220
+ def mul(x, y):
221
+ return x * y
222
+
223
+ server.serve_forever()
224
+
188
225
The following example included in the :file: `Lib/xmlrpc/server.py ` module shows
189
226
a server allowing dotted names and registering a multicall function.
190
227
@@ -252,17 +289,23 @@ This client which interacts with the demo XMLRPC server can be invoked as::
252
289
CGIXMLRPCRequestHandler
253
290
-----------------------
254
291
255
- The :class: `CGIXMLRPCRequestHandler ` class can be used to handle XML-RPC
292
+ The :class: `CGIXMLRPCRequestHandler ` class can be used to handle XML-RPC
256
293
requests sent to Python CGI scripts.
257
294
258
295
259
- .. method :: CGIXMLRPCRequestHandler.register_function(function, name=None)
296
+ .. method :: CGIXMLRPCRequestHandler.register_function(function=None, name=None)
297
+
298
+ Register a function that can respond to XML-RPC requests. If *name * is given,
299
+ it will be the method name associated with *function *, otherwise
300
+ ``function.__name__ `` will be used. *name * is a string, and may contain
301
+ characters not legal in Python identifiers, including the period character.
302
+
303
+ This method can also be used as a decorator. When used as a decorator,
304
+ *name * can only be given as a keyword argument to register *function * under
305
+ *name *. If no *name * is given, ``function.__name__ `` will be used.
260
306
261
- Register a function that can respond to XML-RPC requests. If *name * is given,
262
- it will be the method name associated with function, otherwise
263
- *function.__name__ * will be used. *name * can be either a normal or Unicode
264
- string, and may contain characters not legal in Python identifiers, including
265
- the period character.
307
+ .. versionchanged :: 3.7
308
+ :meth: `register_function ` can be used as a decorator.
266
309
267
310
268
311
.. method :: CGIXMLRPCRequestHandler.register_instance(instance)
0 commit comments