Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f7e3b5e

Browse files
committed
Issue #23254: Document how to close the TCPServer listening socket.
Patch from Martin Panter.
2 parents ace8848 + 1ee9283 commit f7e3b5e

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

Doc/library/socketserver.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ Creating a server requires several steps. First, you must create a request
3333
handler class by subclassing the :class:`BaseRequestHandler` class and
3434
overriding its :meth:`handle` method; this method will process incoming
3535
requests. Second, you must instantiate one of the server classes, passing it
36-
the server's address and the request handler class. Finally, call the
36+
the server's address and the request handler class. Then call the
3737
:meth:`handle_request` or :meth:`serve_forever` method of the server object to
38-
process one or many requests.
38+
process one or many requests. Finally, call :meth:`~BaseServer.server_close`
39+
to close the socket.
3940

4041
When inheriting from :class:`ThreadingMixIn` for threaded connection behavior,
4142
you should explicitly declare how you want your threads to behave on an abrupt
@@ -177,6 +178,13 @@ Server Objects
177178
Tell the :meth:`serve_forever` loop to stop and wait until it does.
178179

179180

181+
.. method:: BaseServer.server_close()
182+
183+
Clean up the server. May be overridden.
184+
185+
.. versionadded:: 2.6
186+
187+
180188
.. attribute:: BaseServer.address_family
181189

182190
The family of protocols to which the server's socket belongs.
@@ -547,6 +555,7 @@ An example for the :class:`ThreadingMixIn` class::
547555
client(ip, port, "Hello World 3")
548556

549557
server.shutdown()
558+
server.server_close()
550559

551560

552561
The output of the example should look something like this::

Lib/test/test_socketserver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def run_server(self, svrcls, hdlrbase, testfunc):
144144
server.shutdown()
145145
t.join()
146146
server.server_close()
147+
self.assertEqual(-1, server.socket.fileno())
147148
if verbose: print("done")
148149

149150
def stream_examine(self, proto, addr):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #23254: Document how to close the TCPServer listening socket.
17+
Patch from Martin Panter.
18+
1619
- Issue #19450: Update Windows builds to use SQLite 3.8.11.0
1720

1821
- Issue #17527: Add PATCH to wsgiref.validator. Patch from Luca Sbardella.

0 commit comments

Comments
 (0)