]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23254: Document how to close the TCPServer listening socket.
authorRobert Collins <rbtcollins@hp.com>
Wed, 29 Jul 2015 00:48:42 +0000 (12:48 +1200)
committerRobert Collins <rbtcollins@hp.com>
Wed, 29 Jul 2015 00:48:42 +0000 (12:48 +1200)
Patch from Martin Panter.

Doc/library/socketserver.rst
Lib/test/test_socketserver.py
Misc/NEWS

index a9053d1419e7ed7a2dbfb50979484190698ea8e5..bb933a65b4a2690bc2885caac872290b7519c3fd 100644 (file)
@@ -39,9 +39,10 @@ Creating a server requires several steps.  First, you must create a request
 handler class by subclassing the :class:`BaseRequestHandler` class and
 overriding its :meth:`handle` method; this method will process incoming
 requests.  Second, you must instantiate one of the server classes, passing it
-the server's address and the request handler class.  Finally, call the
+the server's address and the request handler class.  Then call the
 :meth:`handle_request` or :meth:`serve_forever` method of the server object to
-process one or many requests.
+process one or many requests.  Finally, call :meth:`~BaseServer.server_close`
+to close the socket.
 
 When inheriting from :class:`ThreadingMixIn` for threaded connection behavior,
 you should explicitly declare how you want your threads to behave on an abrupt
@@ -170,6 +171,13 @@ Server Objects
    .. versionadded:: 2.6
 
 
+.. method:: BaseServer.server_close()
+
+   Clean up the server. May be overridden.
+
+   .. versionadded:: 2.6
+
+
 .. attribute:: BaseServer.address_family
 
    The family of protocols to which the server's socket belongs.
@@ -540,6 +548,7 @@ An example for the :class:`ThreadingMixIn` class::
        client(ip, port, "Hello World 3")
 
        server.shutdown()
+       server.server_close()
 
 
 The output of the example should look something like this::
index 8707017f3b25ce824e8bd1e26267248e288bdd71..714ca4afb0a8f998d56e86d9e85651d30a9724da 100644 (file)
@@ -158,6 +158,8 @@ class SocketServerTest(unittest.TestCase):
         if verbose: print "waiting for server"
         server.shutdown()
         t.join()
+        server.server_close()
+        self.assertRaises(socket.error, server.socket.fileno)
         if verbose: print "done"
 
     def stream_examine(self, proto, addr):
index c2ca4e8cf30fab265d673408c4175b30d8546f1e..e86117581cb088a37600c494f64568bbab45dd36 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -34,6 +34,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #23254: Document how to close the TCPServer listening socket.
+  Patch from Martin Panter.
+
 - Issue #17527: Add PATCH to wsgiref.validator. Patch from Luca Sbardella.
 
 - Issue #24613: Calling array.fromstring() with self is no longer allowed