Esempio n. 1
0
 def connect(self, address):
     if isinstance(address, tuple) and len(address)==2:
         address = gethostbyname(address[0]), address[1]
     if self.timeout == 0.0:
         return self._sock.connect(address)
     sock = self._sock
     if self.timeout is None:
         while True:
             err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
             if err:
                 raise error(err, strerror(err))
             result = sock.connect_ex(address)
             if not result or result == EISCONN:
                 break
             elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
                 wait_readwrite(sock.fileno())
             else:
                 raise error(result, strerror(result))
     else:
         end = time.time() + self.timeout
         while True:
             err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
             if err:
                 raise error(err, strerror(err))
             result = sock.connect_ex(address)
             if not result or result == EISCONN:
                 break
             elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
                 timeleft = end - time.time()
                 if timeleft <= 0:
                     raise timeout('timed out')
                 wait_readwrite(sock.fileno(), timeout=timeleft)
             else:
                 raise error(result, strerror(result))
Esempio n. 2
0
 def connect(self, address):
     if self.timeout == 0.0:
         return self._sock.connect(address)
     sock = self._sock
     if isinstance(address, tuple):
         r = getaddrinfo(address[0], address[1], sock.family, sock.type, sock.proto)
         address = r[0][-1]
     if self.timeout is not None:
         timer = Timeout.start_new(self.timeout, timeout('timed out'))
     else:
         timer = None
     try:
         while True:
             err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
             if err:
                 raise error(err, strerror(err))
             result = sock.connect_ex(address)
             if not result or result == EISCONN:
                 break
             elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
                 self._wait(self._write_event)
             else:
                 raise error(result, strerror(result))
     finally:
         if timer is not None:
             timer.cancel()
Esempio n. 3
0
 def connect(self, address):
     if self.timeout == 0.0:
         return self._sock.connect(address)
     sock = self._sock
     if isinstance(address, tuple):
         r = getaddrinfo(address[0], address[1], sock.family, sock.type,
                         sock.proto)
         address = r[0][-1]
     if self.timeout is not None:
         timer = Timeout.start_new(self.timeout, timeout('timed out'))
     else:
         timer = None
     try:
         while True:
             err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
             if err:
                 raise error(err, strerror(err))
             result = sock.connect_ex(address)
             if not result or result == EISCONN:
                 break
             elif (result in (EWOULDBLOCK, EINPROGRESS,
                              EALREADY)) or (result == EINVAL
                                             and is_windows):
                 self._wait(self._write_event)
             else:
                 raise error(result, strerror(result))
     finally:
         if timer is not None:
             timer.cancel()
Esempio n. 4
0
 def connect(self, address):
     if isinstance(address, tuple) and len(address) == 2:
         address = gethostbyname(address[0]), address[1]
     if self.timeout == 0.0:
         return self._sock.connect(address)
     sock = self._sock
     if self.timeout is None:
         while True:
             err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
             if err:
                 raise error(err, strerror(err))
             result = sock.connect_ex(address)
             if not result or result == EISCONN:
                 break
             elif (result in (EWOULDBLOCK, EINPROGRESS,
                              EALREADY)) or (result == EINVAL
                                             and is_windows):
                 wait_readwrite(sock.fileno(), event=self._rw_event)
             else:
                 raise error(result, strerror(result))
     else:
         end = time.time() + self.timeout
         while True:
             err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
             if err:
                 raise error(err, strerror(err))
             result = sock.connect_ex(address)
             if not result or result == EISCONN:
                 break
             elif (result in (EWOULDBLOCK, EINPROGRESS,
                              EALREADY)) or (result == EINVAL
                                             and is_windows):
                 timeleft = end - time.time()
                 if timeleft <= 0:
                     raise timeout('timed out')
                 wait_readwrite(sock.fileno(),
                                timeout=timeleft,
                                event=self._rw_event)
             else:
                 raise error(result, strerror(result))
Esempio n. 5
0
    def connect(self, address):
        """
        Connect to *address*.

        .. versionchanged:: 20.6.0
            If the host part of the address includes an IPv6 scope ID,
            it will be used instead of ignored, if the platform supplies
            :func:`socket.inet_pton`.
        """
        if self.timeout == 0.0:
            return self._sock.connect(address)
        address = _resolve_addr(self._sock, address)
        with Timeout._start_new_or_dummy(self.timeout,
                                         __socket__.timeout("timed out")):
            while 1:
                err = self.getsockopt(__socket__.SOL_SOCKET,
                                      __socket__.SO_ERROR)
                if err:
                    raise _SocketError(err, strerror(err))
                result = self._sock.connect_ex(address)

                if not result or result == EISCONN:
                    break
                if (result in (EWOULDBLOCK, EINPROGRESS,
                               EALREADY)) or (result == EINVAL and is_windows):
                    self._wait(self._write_event)
                else:
                    if (isinstance(address, tuple) and address[0] == 'fe80::1'
                            and result == EHOSTUNREACH):
                        # On Python 3.7 on mac, we see EHOSTUNREACH
                        # returned for this link-local address, but it really is
                        # supposed to be ECONNREFUSED according to the standard library
                        # tests (test_socket.NetworkConnectionNoServer.test_create_connection)
                        # (On previous versions, that code passed the '127.0.0.1' IPv4 address, so
                        # ipv6 link locals were never a factor; 3.7 passes 'localhost'.)
                        # It is something of a mystery how the stdlib socket code doesn't
                        # produce EHOSTUNREACH---I (JAM) can't see how socketmodule.c would avoid
                        # that. The normal connect just calls connect_ex much like we do.
                        result = ECONNREFUSED
                    raise _SocketError(result, strerror(result))