@@ -191,6 +191,11 @@ grouped under the reserved :attr:`system` attribute:
191191 no such string is available, an empty string is returned. The documentation
192192 string may contain HTML markup.
193193
194+ .. versionchanged :: 3.5
195+
196+ Instances of :class: `ServerProxy ` support the :term: `context manager ` protocol
197+ for closing the underlying transport.
198+
194199
195200A working example follows. The server code::
196201
@@ -208,9 +213,9 @@ The client code for the preceding server::
208213
209214 import xmlrpc.client
210215
211- proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
212- print("3 is even: %s" % str(proxy.is_even(3)))
213- print("100 is even: %s" % str(proxy.is_even(100)))
216+ with xmlrpc.client.ServerProxy("http://localhost:8000/") as proxy:
217+ print("3 is even: %s" % str(proxy.is_even(3)))
218+ print("100 is even: %s" % str(proxy.is_even(100)))
214219
215220.. _datetime-objects :
216221
@@ -518,14 +523,14 @@ Example of Client Usage
518523 from xmlrpc.client import ServerProxy, Error
519524
520525 # server = ServerProxy("http://localhost:8000") # local server
521- server = ServerProxy("http://betty.userland.com")
526+ with ServerProxy("http://betty.userland.com") as proxy:
522527
523- print(server )
528+ print(proxy )
524529
525- try:
526- print(server .examples.getStateName(41))
527- except Error as v:
528- print("ERROR", v)
530+ try:
531+ print(proxy .examples.getStateName(41))
532+ except Error as v:
533+ print("ERROR", v)
529534
530535To access an XML-RPC server through a proxy, you need to define a custom
531536transport. The following example shows how:
0 commit comments