Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(3)

Side by Side Diff: Doc/library/xmlrpc.server.rst

Issue 26404: socketserver context manager
Patch Set: Created 4 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 :mod:`xmlrpc.server` --- Basic XML-RPC servers 1 :mod:`xmlrpc.server` --- Basic XML-RPC servers
2 ============================================== 2 ==============================================
3 3
4 .. module:: xmlrpc.server 4 .. module:: xmlrpc.server
5 :synopsis: Basic XML-RPC server implementations. 5 :synopsis: Basic XML-RPC server implementations.
6 .. moduleauthor:: Brian Quinlan <brianq@activestate.com> 6 .. moduleauthor:: Brian Quinlan <brianq@activestate.com>
7 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 7 .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8 8
9 **Source code:** :source:`Lib/xmlrpc/server.py` 9 **Source code:** :source:`Lib/xmlrpc/server.py`
10 10
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 Server code:: 140 Server code::
141 141
142 from xmlrpc.server import SimpleXMLRPCServer 142 from xmlrpc.server import SimpleXMLRPCServer
143 from xmlrpc.server import SimpleXMLRPCRequestHandler 143 from xmlrpc.server import SimpleXMLRPCRequestHandler
144 144
145 # Restrict to a particular path. 145 # Restrict to a particular path.
146 class RequestHandler(SimpleXMLRPCRequestHandler): 146 class RequestHandler(SimpleXMLRPCRequestHandler):
147 rpc_paths = ('/RPC2',) 147 rpc_paths = ('/RPC2',)
148 148
149 # Create server 149 # Create server
150 server = SimpleXMLRPCServer(("localhost", 8000), 150 with SimpleXMLRPCServer(("localhost", 8000),
151 requestHandler=RequestHandler) 151 requestHandler=RequestHandler) as server
Martin Panter 2016/02/24 02:55:12 missing colon (... as server:)
palaviv 2016/02/24 10:05:34 Done.
152 server.register_introspection_functions() 152 server.register_introspection_functions()
153 153
154 # Register pow() function; this will use the value of 154 # Register pow() function; this will use the value of
155 # pow.__name__ as the name, which is just 'pow'. 155 # pow.__name__ as the name, which is just 'pow'.
156 server.register_function(pow) 156 server.register_function(pow)
157 157
158 # Register a function under a different name 158 # Register a function under a different name
159 def adder_function(x,y): 159 def adder_function(x,y):
160 return x + y 160 return x + y
161 server.register_function(adder_function, 'add') 161 server.register_function(adder_function, 'add')
162 162
163 # Register an instance; all the methods of the instance are 163 # Register an instance; all the methods of the instance are
164 # published as XML-RPC methods (in this case, just 'mul'). 164 # published as XML-RPC methods (in this case, just 'mul').
165 class MyFuncs: 165 class MyFuncs:
166 def mul(self, x, y): 166 def mul(self, x, y):
167 return x * y 167 return x * y
168 168
169 server.register_instance(MyFuncs()) 169 server.register_instance(MyFuncs())
170 170
171 # Run the server's main loop 171 # Run the server's main loop
172 server.serve_forever() 172 server.serve_forever()
173 173
174 The following client code will call the methods made available by the preceding 174 The following client code will call the methods made available by the preceding
175 server:: 175 server::
176 176
177 import xmlrpc.client 177 import xmlrpc.client
178 178
179 s = xmlrpc.client.ServerProxy('http://localhost:8000') 179 s = xmlrpc.client.ServerProxy('http://localhost:8000')
180 print(s.pow(2,3)) # Returns 2**3 = 8 180 print(s.pow(2,3)) # Returns 2**3 = 8
181 print(s.add(2,3)) # Returns 5 181 print(s.add(2,3)) # Returns 5
182 print(s.mul(5,2)) # Returns 5*2 = 10 182 print(s.mul(5,2)) # Returns 5*2 = 10
(...skipping 16 matching lines...) Expand all
199 199
200 class ExampleService: 200 class ExampleService:
201 def getData(self): 201 def getData(self):
202 return '42' 202 return '42'
203 203
204 class currentTime: 204 class currentTime:
205 @staticmethod 205 @staticmethod
206 def getCurrentTime(): 206 def getCurrentTime():
207 return datetime.datetime.now() 207 return datetime.datetime.now()
208 208
209 server = SimpleXMLRPCServer(("localhost", 8000)) 209 with SimpleXMLRPCServer(("localhost", 8000)) as server:
210 server.register_function(pow) 210 server.register_function(pow)
211 server.register_function(lambda x,y: x+y, 'add') 211 server.register_function(lambda x,y: x+y, 'add')
212 server.register_instance(ExampleService(), allow_dotted_names=True) 212 server.register_instance(ExampleService(), allow_dotted_names=True)
213 server.register_multicall_functions() 213 server.register_multicall_functions()
214 print('Serving XML-RPC on localhost port 8000') 214 print('Serving XML-RPC on localhost port 8000')
215 try: 215 print('It is advisable to run this example server within a secure, close d network.')
Martin Panter 2016/02/24 02:55:12 I think this just dilutes the example. It is only
palaviv 2016/02/24 10:05:34 Done.
216 server.serve_forever() 216 try:
217 except KeyboardInterrupt: 217 server.serve_forever()
218 print("\nKeyboard interrupt received, exiting.") 218 except KeyboardInterrupt:
219 server.server_close() 219 print("\nKeyboard interrupt received, exiting.")
220 sys.exit(0) 220 sys.exit(0)
221 221
222 This ExampleService demo can be invoked from the command line:: 222 This ExampleService demo can be invoked from the command line::
223 223
224 python -m xmlrpc.server 224 python -m xmlrpc.server
225 225
226 226
227 The client that interacts with the above server is included in 227 The client that interacts with the above server is included in
228 `Lib/xmlrpc/client.py`:: 228 `Lib/xmlrpc/client.py`::
229 229
230 server = ServerProxy("http://localhost:8000") 230 server = ServerProxy("http://localhost:8000")
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 .. method:: DocCGIXMLRPCRequestHandler.set_server_name(server_name) 393 .. method:: DocCGIXMLRPCRequestHandler.set_server_name(server_name)
394 394
395 Set the name used in the generated HTML documentation. This name will appear at 395 Set the name used in the generated HTML documentation. This name will appear at
396 the top of the generated documentation inside a "h1" element. 396 the top of the generated documentation inside a "h1" element.
397 397
398 398
399 .. method:: DocCGIXMLRPCRequestHandler.set_server_documentation(server_documenta tion) 399 .. method:: DocCGIXMLRPCRequestHandler.set_server_documentation(server_documenta tion)
400 400
401 Set the description used in the generated HTML documentation. This descriptio n 401 Set the description used in the generated HTML documentation. This descriptio n
402 will appear as a paragraph, below the server name, in the documentation. 402 will appear as a paragraph, below the server name, in the documentation.
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+