[Python-checkins] bpo-31639: Use threads in http.server module. (GH-5018)

Miss Islington (bot) webhook-mailer at python.org
Fri Mar 23 16:31:29 EDT 2018


https://github.com/python/cpython/commit/f8d2c3cf5f62f0c259b2bf35c631353d22cf1d08
commit: f8d2c3cf5f62f0c259b2bf35c631353d22cf1d08
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-03-23T13:31:20-07:00
summary:

bpo-31639: Use threads in http.server module. (GH-5018)

(cherry picked from commit 8bcfa02e4b1b65634e526e197588bc600674c80b)

Co-authored-by: Julien Palard <julien at palard.fr>

files:
A Misc/NEWS.d/next/Library/2017-12-27-21-55-19.bpo-31639.l3avDJ.rst
M Doc/library/http.server.rst
M Lib/http/server.py

diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst
index c98843de02cb..4fe46cba691f 100644
--- a/Doc/library/http.server.rst
+++ b/Doc/library/http.server.rst
@@ -33,9 +33,16 @@ handler.  Code to create and run the server looks like this::
    :attr:`server_port`. The server is accessible by the handler, typically
    through the handler's :attr:`server` instance variable.
 
+.. class:: ThreadedHTTPServer(server_address, RequestHandlerClass)
 
-The :class:`HTTPServer` must be given a *RequestHandlerClass* on instantiation,
-of which this module provides three different variants:
+   This class is identical to HTTPServer but uses threads to handle
+   requests by using the :class:`~socketserver.ThreadingMixin`. This
+   is usefull to handle web browsers pre-opening sockets, on which
+   :class:`HTTPServer` would wait indefinitly.
+
+The :class:`HTTPServer` and :class:`ThreadedHTTPServer` must be given
+a *RequestHandlerClass* on instantiation, of which this module
+provides three different variants:
 
 .. class:: BaseHTTPRequestHandler(request, client_address, server)
 
diff --git a/Lib/http/server.py b/Lib/http/server.py
index 502bce0c7a40..a2726ab89750 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -83,7 +83,7 @@
 __version__ = "0.6"
 
 __all__ = [
-    "HTTPServer", "BaseHTTPRequestHandler",
+    "HTTPServer", "ThreadedHTTPServer", "BaseHTTPRequestHandler",
     "SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
 ]
 
@@ -140,6 +140,10 @@ def server_bind(self):
         self.server_port = port
 
 
+class ThreadedHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
+    daemon_threads = True
+
+
 class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
 
     """HTTP request handler base class.
@@ -1213,7 +1217,8 @@ def run_cgi(self):
 
 
 def test(HandlerClass=BaseHTTPRequestHandler,
-         ServerClass=HTTPServer, protocol="HTTP/1.0", port=8000, bind=""):
+         ServerClass=ThreadedHTTPServer,
+         protocol="HTTP/1.0", port=8000, bind=""):
     """Test the HTTP request handler class.
 
     This runs an HTTP server on port 8000 (or the port argument).
diff --git a/Misc/NEWS.d/next/Library/2017-12-27-21-55-19.bpo-31639.l3avDJ.rst b/Misc/NEWS.d/next/Library/2017-12-27-21-55-19.bpo-31639.l3avDJ.rst
new file mode 100644
index 000000000000..e876f40813de
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-12-27-21-55-19.bpo-31639.l3avDJ.rst
@@ -0,0 +1,2 @@
+http.server now exposes a ThreadedHTTPServer class and uses it when the
+module is invoked to cope with web browsers pre-opening sockets.



More information about the Python-checkins mailing list