]> granicus.if.org Git - python/commitdiff
bpo-31639: Use threads in http.server module. (GH-5018)
authorJulien Palard <julien@palard.fr>
Fri, 23 Mar 2018 16:40:33 +0000 (17:40 +0100)
committerGitHub <noreply@github.com>
Fri, 23 Mar 2018 16:40:33 +0000 (17:40 +0100)
Doc/library/http.server.rst
Lib/http/server.py
Misc/NEWS.d/next/Library/2017-12-27-21-55-19.bpo-31639.l3avDJ.rst [new file with mode: 0644]

index c98843de02cba31cf11b70693b018ec06faccda5..4fe46cba691f9398260a1520ab84f2c5d4a94015 100644 (file)
@@ -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)
 
index 502bce0c7a40daa7b554434cd07f0b4ccb15d88c..a2726ab8975013eb44016eda1ef0cf75de6896bf 100644 (file)
@@ -83,7 +83,7 @@ XXX To do:
 __version__ = "0.6"
 
 __all__ = [
-    "HTTPServer", "BaseHTTPRequestHandler",
+    "HTTPServer", "ThreadedHTTPServer", "BaseHTTPRequestHandler",
     "SimpleHTTPRequestHandler", "CGIHTTPRequestHandler",
 ]
 
@@ -140,6 +140,10 @@ class HTTPServer(socketserver.TCPServer):
         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 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
 
 
 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 (file)
index 0000000..e876f40
--- /dev/null
@@ -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.