Esempio n. 1
0
    def handle(self, conn, _address):  # pylint: disable=method-hidden
        """
        Interact with one remote user.

        .. versionchanged:: 1.1b2 Each connection gets its own
            ``locals`` dictionary. Previously they were shared in a
            potentially unsafe manner.
        """
        conn.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True)  # pylint:disable=no-member
        raw_file = conn.makefile(mode="r")
        getcurrent().stdin = _StdIn(conn, raw_file)
        getcurrent().stdout = _StdErr(conn, raw_file)

        # Swizzle the inputs
        getcurrent().switch_in()
        try:
            console = InteractiveConsole(self._create_interactive_locals())
            if PY36:
                # Beginning in 3.6, the console likes to print "now exiting <class>"
                # but probably our socket is already closed, so this just causes problems.
                console.interact(banner=self.banner, exitmsg='')  # pylint:disable=unexpected-keyword-arg
            else:
                console.interact(banner=self.banner)
        except SystemExit:
            # raised by quit(); obviously this cannot propagate.
            exc_clear()  # Python 2
        finally:
            raw_file.close()
            conn.close()
Esempio n. 2
0
 def recv_into(self, *args):
     while 1:
         try:
             return self._sock.recv_into(*args)
         except _SocketError as ex:
             if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0:
                 raise
             exc_clear()  # Python 2
         self._wait(self._read_event)
Esempio n. 3
0
 def recv(self, *args):
     while 1:
         try:
             return self._sock.recv(*args)
         except _SocketError as ex:
             if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0:
                 raise
             # QQQ without clearing exc_info test__refcount.test_clean_exit fails
             exc_clear()  # Python 2
         self._wait(self._read_event)
Esempio n. 4
0
    def sendto(self, *args):
        try:
            return self._sock.sendto(*args)
        except _SocketError as ex:
            if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0:
                raise
            exc_clear()
            self._wait(self._write_event)

            try:
                return self._sock.sendto(*args)
            except _SocketError as ex2:
                if ex2.args[0] == EWOULDBLOCK:
                    exc_clear()
                    return 0
                raise
Esempio n. 5
0
 def send(self, data, flags=0, timeout=timeout_default):
     if timeout is timeout_default:
         timeout = self.timeout
     try:
         return self._sock.send(data, flags)
     except _SocketError as ex:
         if ex.args[0] not in GSENDAGAIN or timeout == 0.0:
             raise
         exc_clear()
         self._wait(self._write_event)
         try:
             return self._sock.send(data, flags)
         except _SocketError as ex2:
             if ex2.args[0] == EWOULDBLOCK:
                 exc_clear()
                 return 0
             raise
def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None):
    """
    create_connection(address, timeout=None, source_address=None) -> socket

    Connect to *address* and return the :class:`gevent.socket.socket`
    object.

    Convenience function. Connect to *address* (a 2-tuple ``(host,
    port)``) and return the socket object. Passing the optional
    *timeout* parameter will set the timeout on the socket instance
    before attempting to connect. If no *timeout* is supplied, the
    global default timeout setting returned by
    :func:`getdefaulttimeout` is used. If *source_address* is set it
    must be a tuple of (host, port) for the socket to bind as a source
    address before making the connection. A host of '' or port 0 tells
    the OS to use the default.
    """

    host, port = address
    # getaddrinfo is documented as returning a list, but our interface
    # is pluggable, so be sure it does.
    addrs = list(getaddrinfo(host, port, 0, SOCK_STREAM))
    if not addrs:
        raise error("getaddrinfo returns an empty list")

    for res in addrs:
        af, socktype, proto, _, sa = res
        sock = None
        try:
            sock = socket(af, socktype, proto)
            if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
                sock.settimeout(timeout)
            if source_address:
                sock.bind(source_address)
            sock.connect(sa)
        except error:
            if sock is not None:
                sock.close()
            sock = None
            if res is addrs[-1]:
                raise
            # without exc_clear(), if connect() fails once, the socket
            # is referenced by the frame in exc_info and the next
            # bind() fails (see test__socket.TestCreateConnection)
            # that does not happen with regular sockets though,
            # because _socket.socket.connect() is a built-in. this is
            # similar to "getnameinfo loses a reference" failure in
            # test_socket.py
            exc_clear()
        except BaseException:
            # Things like GreenletExit,  Timeout and KeyboardInterrupt.
            # These get raised immediately, being sure to
            # close the socket
            if sock is not None:
                sock.close()
            sock = None
            raise
        else:
            try:
                return sock
            finally:
                sock = None