Skip to content

Commit 08a5720

Browse files
committed
Add suggestions
1 parent 4df61de commit 08a5720

File tree

2 files changed

+55
-54
lines changed

2 files changed

+55
-54
lines changed

Doc/library/http.server.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ handler. Code to create and run the server looks like this::
6161

6262
The *certfile* argument is the path to the SSL certificate chain file,
6363
and the *keyfile* is the path to file containing the private key.
64-
64+
6565
A *password* can be specified for files protected and wrapped with PKCS#8,
6666
but beware that this could possibly expose hardcoded passwords in clear.
6767

Lib/http/server.py

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@
8383
__version__ = "0.6"
8484

8585
__all__ = [
86-
"HTTPServer", "ThreadingHTTPServer", "BaseHTTPRequestHandler",
87-
"SimpleHTTPRequestHandler", "CGIHTTPRequestHandler", "HTTPSServer",
88-
"ThreadingHTTPSServer",
86+
"HTTPServer", "ThreadingHTTPServer",
87+
"HTTPSServer", "ThreadingHTTPSServer",
88+
"BaseHTTPRequestHandler", "SimpleHTTPRequestHandler",
89+
"CGIHTTPRequestHandler",
8990
]
9091

9192
import copy
@@ -150,6 +151,56 @@ class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
150151
daemon_threads = True
151152

152153

154+
class HTTPSServer(HTTPServer):
155+
def __init__(self, server_address, RequestHandlerClass,
156+
bind_and_activate=True, *, certfile, keyfile=None,
157+
password=None, alpn_protocols=None):
158+
try:
159+
import ssl
160+
except ImportError:
161+
raise RuntimeError("SSL module is missing; HTTPS support is unavailable")
162+
163+
self.ssl = ssl
164+
self.certfile = certfile
165+
self.keyfile = keyfile
166+
self.password = password
167+
# Support by default HTTP/1.1
168+
self.alpn_protocols = ["http/1.1"] if alpn_protocols is None else alpn_protocols
169+
170+
super().__init__(server_address, RequestHandlerClass, bind_and_activate)
171+
172+
def server_activate(self):
173+
"""Wrap the socket in SSLSocket."""
174+
super().server_activate()
175+
176+
context = self._create_context()
177+
self.socket = context.wrap_socket(self.socket, server_side=True)
178+
179+
def _create_context(self):
180+
"""Create a secure SSL context."""
181+
context = self.ssl.create_default_context(self.ssl.Purpose.CLIENT_AUTH)
182+
context.load_cert_chain(certfile=self.certfile,
183+
keyfile=self.keyfile,
184+
password=self.password)
185+
context.set_alpn_protocols(self.alpn_protocols)
186+
187+
return context
188+
189+
190+
class ThreadingHTTPSServer(socketserver.ThreadingMixIn, HTTPSServer):
191+
daemon_threads = True
192+
193+
194+
def _get_best_family(*address):
195+
infos = socket.getaddrinfo(
196+
*address,
197+
type=socket.SOCK_STREAM,
198+
flags=socket.AI_PASSIVE,
199+
)
200+
family, type, proto, canonname, sockaddr = next(iter(infos))
201+
return family, sockaddr
202+
203+
153204
class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
154205

155206
"""HTTP request handler base class.
@@ -1252,56 +1303,6 @@ def run_cgi(self):
12521303
self.log_message("CGI script exited OK")
12531304

12541305

1255-
class HTTPSServer(HTTPServer):
1256-
def __init__(self, server_address, RequestHandlerClass,
1257-
bind_and_activate=True, *, certfile, keyfile=None,
1258-
password=None, alpn_protocols=None):
1259-
try:
1260-
import ssl
1261-
except ImportError:
1262-
raise RuntimeError("SSL module is missing; HTTPS support is unavailable")
1263-
1264-
self.ssl = ssl
1265-
self.certfile = certfile
1266-
self.keyfile = keyfile
1267-
self.password = password
1268-
# Support by default HTTP/1.1
1269-
self.alpn_protocols = ["http/1.1"] if alpn_protocols is None else alpn_protocols
1270-
1271-
super().__init__(server_address, RequestHandlerClass, bind_and_activate)
1272-
1273-
def server_activate(self):
1274-
"""Wrap the socket in SSLSocket."""
1275-
super().server_activate()
1276-
1277-
context = self._create_context()
1278-
self.socket = context.wrap_socket(self.socket, server_side=True)
1279-
1280-
def _create_context(self):
1281-
"""Create a secure SSL context."""
1282-
context = self.ssl.create_default_context(self.ssl.Purpose.CLIENT_AUTH)
1283-
context.load_cert_chain(certfile=self.certfile,
1284-
keyfile=self.keyfile,
1285-
password=self.password)
1286-
context.set_alpn_protocols(self.alpn_protocols)
1287-
1288-
return context
1289-
1290-
1291-
class ThreadingHTTPSServer(socketserver.ThreadingMixIn, HTTPSServer):
1292-
daemon_threads = True
1293-
1294-
1295-
def _get_best_family(*address):
1296-
infos = socket.getaddrinfo(
1297-
*address,
1298-
type=socket.SOCK_STREAM,
1299-
flags=socket.AI_PASSIVE,
1300-
)
1301-
family, type, proto, canonname, sockaddr = next(iter(infos))
1302-
return family, sockaddr
1303-
1304-
13051306
def test(HandlerClass=BaseHTTPRequestHandler,
13061307
ServerClass=ThreadingHTTPServer,
13071308
protocol="HTTP/1.0", port=8000, bind=None,

0 commit comments

Comments
 (0)