Modify socket in server_bind()

This commit is contained in:
Unrud 2018-09-06 09:12:51 +02:00
parent 9c802e0f57
commit e0eeae02dd

View File

@ -50,20 +50,8 @@ class ParallelHTTPServer(ParallelizationMixIn,
client_timeout = None client_timeout = None
max_connections = None max_connections = None
def __init__(self, address, handler, bind_and_activate=True): def __init__(self, *args, **kwargs):
"""Create server.""" super().__init__(*args, **kwargs)
ipv6 = ":" in address[0]
if ipv6:
self.address_family = socket.AF_INET6
# Do not bind and activate, as we might change socket options
super().__init__(address, handler, False)
if ipv6:
# Only allow IPv6 connections to the IPv6 socket
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
if self.max_connections: if self.max_connections:
self.connections_guard = multiprocessing.BoundedSemaphore( self.connections_guard = multiprocessing.BoundedSemaphore(
self.max_connections) self.max_connections)
@ -71,13 +59,15 @@ class ParallelHTTPServer(ParallelizationMixIn,
# use dummy context manager # use dummy context manager
self.connections_guard = contextlib.ExitStack() self.connections_guard = contextlib.ExitStack()
if bind_and_activate: def server_bind(self):
try: ipv6 = ":" in self.server_address[0]
self.server_bind() if ipv6 and self.address_family == socket.AF_INET:
self.server_activate() self.address_family = socket.AF_INET6
except BaseException: self.socket = socket.socket(self.address_family, self.socket_type)
self.server_close() if ipv6:
raise # Only allow IPv6 connections to the IPv6 socket
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
super().server_bind()
def get_request(self): def get_request(self):
# Set timeout for client # Set timeout for client
@ -110,12 +100,9 @@ class ParallelHTTPSServer(ParallelHTTPServer):
ciphers = None ciphers = None
certificate_authority = None certificate_authority = None
def __init__(self, address, handler, bind_and_activate=True): def server_bind(self):
super().server_bind()
"""Create server by wrapping HTTP socket in an SSL socket.""" """Create server by wrapping HTTP socket in an SSL socket."""
# Do not bind and activate, as we change the socket
super().__init__(address, handler, False)
self.socket = ssl.wrap_socket( self.socket = ssl.wrap_socket(
self.socket, self.key, self.certificate, server_side=True, self.socket, self.key, self.certificate, server_side=True,
cert_reqs=ssl.CERT_REQUIRED if self.certificate_authority else cert_reqs=ssl.CERT_REQUIRED if self.certificate_authority else
@ -124,14 +111,6 @@ class ParallelHTTPSServer(ParallelHTTPServer):
ssl_version=self.protocol, ciphers=self.ciphers, ssl_version=self.protocol, ciphers=self.ciphers,
do_handshake_on_connect=False) do_handshake_on_connect=False)
if bind_and_activate:
try:
self.server_bind()
self.server_activate()
except BaseException:
self.server_close()
raise
def finish_request(self, request, client_address): def finish_request(self, request, client_address):
with self.connections_guard: with self.connections_guard:
try: try: