(address, options)
Given (host, port) and PoolOptions, connect and return a socket object.

    Can raise socket.error.

    This is a modified version of create_connection from CPython >= 2.6.
Given (host, port) and PoolOptions, connect and return a socket object.
Given ( host port ) and PoolOptions connect and return a socket object .
def _create_connection(address, options):
    """Given (host, port) and PoolOptions, connect and return a socket object.

    Can raise socket.error.

    This is a modified version of create_connection from CPython >= 2.6.
    """
    host, port = address

    # Check if dealing with a unix domain socket
    if host.endswith('.sock'):
        if not hasattr(socket, "AF_UNIX"):
            raise ConnectionFailure("UNIX-sockets are not supported "
                                    "on this system")
        sock = socket.socket(socket.AF_UNIX)
        # SOCK_CLOEXEC not supported for Unix sockets.
        _set_non_inheritable_non_atomic(sock.fileno())
        try:
            sock.connect(host)
            return sock
        except socket.error:
            sock.close()
            raise

    # Don't try IPv6 if we don't support it. Also skip it if host
    # is 'localhost' (::1 is fine). Avoids slow connect issues
    # like PYTHON-356.
    family = socket.AF_INET
    if socket.has_ipv6 and host != 'localhost':
        family = socket.AF_UNSPEC

    err = None
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
        af, socktype, proto, dummy, sa = res
        # SOCK_CLOEXEC was new in CPython 3.2, and only available on a limited
        # number of platforms (newer Linux and *BSD). Starting with CPython 3.4
        # all file descriptors are created non-inheritable. See PEP 446.
        try:
            sock = socket.socket(
                af, socktype | getattr(socket, 'SOCK_CLOEXEC', 0), proto)
        except socket.error:
            # Can SOCK_CLOEXEC be defined even if the kernel doesn't support
            # it?
            sock = socket.socket(af, socktype, proto)
        # Fallback when SOCK_CLOEXEC isn't available.
        _set_non_inheritable_non_atomic(sock.fileno())
        try:
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            sock.settimeout(options.connect_timeout)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE,
                            options.socket_keepalive)
            if options.socket_keepalive:
                _set_keepalive_times(sock)
            sock.connect(sa)
            return sock
        except socket.error as e:
            err = e
            sock.close()

    if err is not None:
        raise err
    else:
        # This likely means we tried to connect to an IPv6 only
        # host with an OS/kernel or Python interpreter that doesn't
        # support IPv6. The test case is Jython2.5.1 which doesn't
        # support IPv6 at all.
        raise socket.error('getaddrinfo failed')
def _create_connection ( address , options ) : host , port = address # Check if dealing with a unix domain socket if host . endswith ( '.sock' ) : if not hasattr ( socket , "AF_UNIX" ) : raise ConnectionFailure ( "UNIX-sockets are not supported " "on this system" ) sock = socket . socket ( socket . AF_UNIX ) # SOCK_CLOEXEC not supported for Unix sockets. _set_non_inheritable_non_atomic ( sock . fileno ( ) ) try : sock . connect ( host ) return sock except socket . error : sock . close ( ) raise # Don't try IPv6 if we don't support it. Also skip it if host # is 'localhost' (::1 is fine). Avoids slow connect issues # like PYTHON-356. family = socket . AF_INET if socket . has_ipv6 and host != 'localhost' : family = socket . AF_UNSPEC err = None for res in socket . getaddrinfo ( host , port , family , socket . SOCK_STREAM ) : af , socktype , proto , dummy , sa = res # SOCK_CLOEXEC was new in CPython 3.2, and only available on a limited # number of platforms (newer Linux and *BSD). Starting with CPython 3.4 # all file descriptors are created non-inheritable. See PEP 446. try : sock = socket . socket ( af , socktype | getattr ( socket , 'SOCK_CLOEXEC' , 0 ) , proto ) except socket . error : # Can SOCK_CLOEXEC be defined even if the kernel doesn't support # it? sock = socket . socket ( af , socktype , proto ) # Fallback when SOCK_CLOEXEC isn't available. _set_non_inheritable_non_atomic ( sock . fileno ( ) ) try : sock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , 1 ) sock . settimeout ( options . connect_timeout ) sock . setsockopt ( socket . SOL_SOCKET , socket . SO_KEEPALIVE , options . socket_keepalive ) if options . socket_keepalive : _set_keepalive_times ( sock ) sock . connect ( sa ) return sock except socket . error as e : err = e sock . close ( ) if err is not None : raise err else : # This likely means we tried to connect to an IPv6 only # host with an OS/kernel or Python interpreter that doesn't # support IPv6. The test case is Jython2.5.1 which doesn't # support IPv6 at all. raise socket . error ( 'getaddrinfo failed' )