def create_connection(address, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None):
 (host, port) = address
 err = None
 for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
  (af, socktype, proto, canonname, sa) = res
  sock = None
  try:
   sock = socket.socket(af, socktype, proto)
   sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
   if (timeout is not socket._GLOBAL_DEFAULT_TIMEOUT):
    sock.settimeout(timeout)
   if source_address:
    sock.bind(source_address)
   sock.connect(sa)
   return sock
  except socket.error as _:
   err = _
   if (sock is not None):
    sock.close()
 if (err is not None):
  raise err
 else:
  raise socket.error('getaddrinfo returns an empty list')

def create_connection ( address , timeout = socket . _GLOBAL_DEFAULT_TIMEOUT , source_address = None ) : ( host , port ) = address err = None for res in socket . getaddrinfo ( host , port , 0 , socket . SOCK_STREAM ) : ( af , socktype , proto , canonname , sa ) = res sock = None try : sock = socket . socket ( af , socktype , proto ) sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , 1 ) if ( timeout is not socket . _GLOBAL_DEFAULT_TIMEOUT ) : sock . settimeout ( timeout ) if source_address : sock . bind ( source_address ) sock . connect ( sa ) return sock except socket . error as _ : err = _ if ( sock is not None ) : sock . close ( ) if ( err is not None ) : raise err else : raise socket . error ( 'getaddrinfo returns an empty list' )
connect to *address* and return the socket object .