Skip to content

Commit 8bcfa02

Browse files
authored
bpo-31639: Use threads in http.server module. (GH-5018)
1 parent a0a42d2 commit 8bcfa02

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

Doc/library/http.server.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ handler. Code to create and run the server looks like this::
3333
:attr:`server_port`. The server is accessible by the handler, typically
3434
through the handler's :attr:`server` instance variable.
3535

36+
.. class:: ThreadedHTTPServer(server_address, RequestHandlerClass)
3637

37-
The :class:`HTTPServer` must be given a *RequestHandlerClass* on instantiation,
38-
of which this module provides three different variants:
38+
This class is identical to HTTPServer but uses threads to handle
39+
requests by using the :class:`~socketserver.ThreadingMixin`. This
40+
is usefull to handle web browsers pre-opening sockets, on which
41+
:class:`HTTPServer` would wait indefinitly.
42+
43+
The :class:`HTTPServer` and :class:`ThreadedHTTPServer` must be given
44+
a *RequestHandlerClass* on instantiation, of which this module
45+
provides three different variants:
3946

4047
.. class:: BaseHTTPRequestHandler(request, client_address, server)
4148

Lib/http/server.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
__version__ = "0.6"
8484

8585
__all__ = [
86-
"HTTPServer", "BaseHTTPRequestHandler",
86+
"HTTPServer", "ThreadedHTTPServer", "BaseHTTPRequestHandler",
8787
"SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
8888
]
8989

@@ -140,6 +140,10 @@ def server_bind(self):
140140
self.server_port = port
141141

142142

143+
class ThreadedHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
144+
daemon_threads = True
145+
146+
143147
class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
144148

145149
"""HTTP request handler base class.
@@ -1213,7 +1217,8 @@ def run_cgi(self):
12131217

12141218

12151219
def test(HandlerClass=BaseHTTPRequestHandler,
1216-
ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""):
1220+
ServerClass=ThreadedHTTPServer,
1221+
protocol="HTTP/1.0", port=8000, bind=""):
12171222
"""Test the HTTP request handler class.
12181223
12191224
This runs an HTTP server on port 8000 (or the port argument).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
http.server now exposes a ThreadedHTTPServer class and uses it when the
2+
module is invoked to cope with web browsers pre-opening sockets.

0 commit comments

Comments
 (0)