diff -r 62438d1b11c7 Doc/library/socketserver.rst --- a/Doc/library/socketserver.rst Fri May 02 23:26:03 2014 +0200 +++ b/Doc/library/socketserver.rst Sat May 03 23:40:05 2014 +0200 @@ -181,6 +181,8 @@ The family of protocols to which the server's socket belongs. Common examples are :const:`socket.AF_INET` and :const:`socket.AF_UNIX`. + Note that :const:`socket.AF_INET` will be automatically updated to + :const:`socket.AF_INET6` if you specify to listen to an IPv6 address. .. attribute:: BaseServer.RequestHandlerClass diff -r 62438d1b11c7 Lib/socketserver.py --- a/Lib/socketserver.py Fri May 02 23:26:03 2014 +0200 +++ b/Lib/socketserver.py Sat May 03 23:40:05 2014 +0200 @@ -132,6 +132,7 @@ import selectors import os import errno +from ipaddress import IPv6Address, AddressValueError try: import threading except ImportError: @@ -439,6 +440,7 @@ def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): """Constructor. May be extended, do not override.""" BaseServer.__init__(self, server_address, RequestHandlerClass) + self.address_family_ipv6_check(server_address); self.socket = socket.socket(self.address_family, self.socket_type) if bind_and_activate: @@ -498,6 +500,18 @@ pass #some platforms may raise ENOTCONN here self.close_request(request) + def address_family_ipv6_check(self, server_address): + """Called by constructor to determine if the socket address family + should be upgraded from IPv4 to IPv6 based on the server IP address. + """ + try: + if (self.address_family == socket.AF_INET and + len(server_address) == 2 and + IPv6Address(server_address[0]).version == 6): + self.address_family = socket.AF_INET6 + except (AddressValueError): + pass + def close_request(self, request): """Called to clean up an individual request.""" request.close()