Python socket.SocketType() Examples

The following are 48 code examples for showing how to use socket.SocketType(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

You may also check out all available functions/classes of the module socket , or try the search function .

Example 1
Project: pyuavcan   Author: UAVCAN   File: _socketcan.py    License: MIT License 6 votes vote down vote up
def _make_socket(iface_name: str, can_fd: bool) -> socket.SocketType:
    s = socket.socket(socket.PF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
    try:
        s.bind((iface_name,))
        s.setsockopt(socket.SOL_SOCKET, _SO_TIMESTAMP, 1)  # timestamping
        if can_fd:
            s.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_FD_FRAMES, 1)

        s.setblocking(False)

        if 0 != s.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR):
            raise OSError('Could not configure the socket: getsockopt(SOL_SOCKET, SO_ERROR) != 0')
    except BaseException:
        with contextlib.suppress(Exception):
            s.close()
        raise

    return s 
Example 2
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 6 votes vote down vote up
def test_init_handles_ipv6_addr(self):
        addr = (
            sentinel.host,
            sentinel.port,
            sentinel.flowinfo,
            sentinel.scopeid,
        )
        protocol = Mock(spec=network.LineProtocol)
        protocol_kwargs = {}
        sock = Mock(spec=socket.SocketType)

        network.Connection.__init__(
            self.mock, protocol, protocol_kwargs, sock, addr, sentinel.timeout
        )
        assert sentinel.host == self.mock.host
        assert sentinel.port == self.mock.port 
Example 3
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 6 votes vote down vote up
def select_recv(conn, buff_size, timeout=None):
    """add timeout for socket.recv()
    :type conn: socket.SocketType
    :type buff_size: int
    :type timeout: float
    :rtype: Union[bytes, None]
    """
    rlist, _, _ = select.select([conn], [], [], timeout)
    if not rlist:
        # timeout
        raise RuntimeError("recv timeout")

    buff = conn.recv(buff_size)
    if not buff:
        raise RuntimeError("received zero bytes, socket was closed")

    return buff 
Example 4
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 6 votes vote down vote up
def _rd_shutdown(self, conn, once=False):
        """action when connection should be read-shutdown
        :type conn: socket.SocketType
        """
        if conn in self.conn_rd:
            self.conn_rd.remove(conn)

        try:
            conn.shutdown(socket.SHUT_RD)
        except:
            pass

        if not once and conn in self.map:  # use the `once` param to avoid infinite loop
            # if a socket is rd_shutdowned, then it's
            #   pair should be wr_shutdown.
            self._wr_shutdown(self.map[conn], True)

        if self.map.get(conn) not in self.conn_rd:
            # if both two connection pair was rd-shutdowned,
            #   this pair sockets are regarded to be completed
            #   so we gonna close them
            self._terminate(conn) 
Example 5
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 6 votes vote down vote up
def select_recv(conn, buff_size, timeout=None):
    """add timeout for socket.recv()
    :type conn: socket.SocketType
    :type buff_size: int
    :type timeout: float
    :rtype: Union[bytes, None]
    """
    rlist, _, _ = select.select([conn], [], [], timeout)
    if not rlist:
        # timeout
        raise RuntimeError("recv timeout")

    buff = conn.recv(buff_size)
    if not buff:
        raise RuntimeError("received zero bytes, socket was closed")

    return buff 
Example 6
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 6 votes vote down vote up
def add_conn_pair(self, conn1, conn2, callback=None):
        """
        transfer anything between two sockets

        :type conn1: socket.SocketType
        :type conn2: socket.SocketType
        :param callback: callback in connection finish
        :type callback: Callable
        """
        # mark as readable
        self.conn_rd.add(conn1)
        self.conn_rd.add(conn2)

        # record sockets pairs
        self.map[conn1] = conn2
        self.map[conn2] = conn1

        # record callback
        if callback is not None:
            self.callbacks[conn1] = callback 
Example 7
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 6 votes vote down vote up
def _rd_shutdown(self, conn, once=False):
        """action when connection should be read-shutdown
        :type conn: socket.SocketType
        """
        if conn in self.conn_rd:
            self.conn_rd.remove(conn)

        try:
            conn.shutdown(socket.SHUT_RD)
        except:
            pass

        if not once and conn in self.map:  # use the `once` param to avoid infinite loop
            # if a socket is rd_shutdowned, then it's
            #   pair should be wr_shutdown.
            self._wr_shutdown(self.map[conn], True)

        if self.map.get(conn) not in self.conn_rd:
            # if both two connection pair was rd-shutdowned,
            #   this pair sockets are regarded to be completed
            #   so we gonna close them
            self._terminate(conn) 
Example 8
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_init_ensure_nonblocking_io(self):
        sock = Mock(spec=socket.SocketType)

        network.Connection.__init__(
            self.mock,
            Mock(),
            {},
            sock,
            (sentinel.host, sentinel.port),
            sentinel.timeout,
        )
        sock.setblocking.assert_called_once_with(False) 
Example 9
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_init_stores_values_in_attributes(self):
        addr = (sentinel.host, sentinel.port)
        protocol = Mock(spec=network.LineProtocol)
        protocol_kwargs = {}
        sock = Mock(spec=socket.SocketType)

        network.Connection.__init__(
            self.mock, protocol, protocol_kwargs, sock, addr, sentinel.timeout
        )
        assert sock == self.mock._sock
        assert protocol == self.mock.protocol
        assert protocol_kwargs == self.mock.protocol_kwargs
        assert sentinel.timeout == self.mock.timeout
        assert sentinel.host == self.mock.host
        assert sentinel.port == self.mock.port 
Example 10
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_stop_disables_recv_send_and_timeout(self):
        self.mock.stopping = False
        self.mock.actor_ref = Mock()
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.stop(self.mock, sentinel.reason)
        self.mock.disable_timeout.assert_called_once_with()
        self.mock.disable_recv.assert_called_once_with()
        self.mock.disable_send.assert_called_once_with() 
Example 11
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_stop_closes_socket(self):
        self.mock.stopping = False
        self.mock.actor_ref = Mock()
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.stop(self.mock, sentinel.reason)
        self.mock._sock.close.assert_called_once_with() 
Example 12
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_stop_stops_actor(self):
        self.mock.stopping = False
        self.mock.actor_ref = Mock()
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.stop(self.mock, sentinel.reason)
        self.mock.actor_ref.stop.assert_called_once_with(block=False) 
Example 13
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_stop_handles_actor_already_being_stopped(self):
        self.mock.stopping = False
        self.mock.actor_ref = Mock()
        self.mock.actor_ref.stop.side_effect = pykka.ActorDeadError()
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.stop(self.mock, sentinel.reason)
        self.mock.actor_ref.stop.assert_called_once_with(block=False) 
Example 14
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_stop_sets_stopping_to_true(self):
        self.mock.stopping = False
        self.mock.actor_ref = Mock()
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.stop(self.mock, sentinel.reason)
        assert self.mock.stopping is True 
Example 15
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_stop_does_not_proceed_when_already_stopping(self):
        self.mock.stopping = True
        self.mock.actor_ref = Mock()
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.stop(self.mock, sentinel.reason)
        assert 0 == self.mock.actor_ref.stop.call_count
        assert 0 == self.mock._sock.close.call_count 
Example 16
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_stop_logs_reason(self):
        self.mock.stopping = False
        self.mock.actor_ref = Mock()
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.stop(self.mock, sentinel.reason)
        network.logger.log.assert_called_once_with(
            logging.DEBUG, sentinel.reason
        ) 
Example 17
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_stop_logs_that_it_is_calling_itself(self):
        self.mock.stopping = True
        self.mock.actor_ref = Mock()
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.stop(self.mock, sentinel.reason)
        network.logger.log(any_int, any_unicode) 
Example 18
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_enable_recv_registers_with_glib(self):
        self.mock.recv_id = None
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.fileno.return_value = sentinel.fileno
        GLib.io_add_watch.return_value = sentinel.tag

        network.Connection.enable_recv(self.mock)
        GLib.io_add_watch.assert_called_once_with(
            sentinel.fileno,
            GLib.IO_IN | GLib.IO_ERR | GLib.IO_HUP,
            self.mock.recv_callback,
        )
        assert sentinel.tag == self.mock.recv_id 
Example 19
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_enable_recv_already_registered(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock.recv_id = sentinel.tag

        network.Connection.enable_recv(self.mock)
        assert 0 == GLib.io_add_watch.call_count 
Example 20
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_enable_recv_does_not_change_tag(self):
        self.mock.recv_id = sentinel.tag
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.enable_recv(self.mock)
        assert sentinel.tag == self.mock.recv_id 
Example 21
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_enable_recv_on_closed_socket(self):
        self.mock.recv_id = None
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.fileno.side_effect = socket.error(errno.EBADF, "")

        network.Connection.enable_recv(self.mock)
        self.mock.stop.assert_called_once_with(any_unicode)
        assert self.mock.recv_id is None 
Example 22
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_enable_send_already_registered(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock.send_id = sentinel.tag

        network.Connection.enable_send(self.mock)
        assert 0 == GLib.io_add_watch.call_count 
Example 23
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_enable_send_does_not_change_tag(self):
        self.mock.send_id = sentinel.tag
        self.mock._sock = Mock(spec=socket.SocketType)

        network.Connection.enable_send(self.mock)
        assert sentinel.tag == self.mock.send_id 
Example 24
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_enable_send_on_closed_socket(self):
        self.mock.send_id = None
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.fileno.side_effect = socket.error(errno.EBADF, "")

        network.Connection.enable_send(self.mock)
        assert self.mock.send_id is None 
Example 25
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_recv_callback_respects_io_err(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock.actor_ref = Mock()

        assert network.Connection.recv_callback(
            self.mock, sentinel.fd, (GLib.IO_IN | GLib.IO_ERR)
        )
        self.mock.stop.assert_called_once_with(any_unicode) 
Example 26
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_recv_callback_respects_io_hup(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock.actor_ref = Mock()

        assert network.Connection.recv_callback(
            self.mock, sentinel.fd, (GLib.IO_IN | GLib.IO_HUP)
        )
        self.mock.stop.assert_called_once_with(any_unicode) 
Example 27
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_recv_callback_sends_data_to_actor(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.recv.return_value = b"data"
        self.mock.actor_ref = Mock()

        assert network.Connection.recv_callback(
            self.mock, sentinel.fd, GLib.IO_IN
        )
        self.mock.actor_ref.tell.assert_called_once_with({"received": b"data"}) 
Example 28
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_recv_callback_handles_dead_actors(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.recv.return_value = b"data"
        self.mock.actor_ref = Mock()
        self.mock.actor_ref.tell.side_effect = pykka.ActorDeadError()

        assert network.Connection.recv_callback(
            self.mock, sentinel.fd, GLib.IO_IN
        )
        self.mock.stop.assert_called_once_with(any_unicode) 
Example 29
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_recv_callback_gets_no_data(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.recv.return_value = b""
        self.mock.actor_ref = Mock()

        assert network.Connection.recv_callback(
            self.mock, sentinel.fd, GLib.IO_IN
        )
        assert self.mock.mock_calls == [
            call._sock.recv(any_int),
            call.disable_recv(),
            call.actor_ref.tell({"close": True}),
        ] 
Example 30
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_recv_callback_recoverable_error(self):
        self.mock._sock = Mock(spec=socket.SocketType)

        for error in (errno.EWOULDBLOCK, errno.EINTR):
            self.mock._sock.recv.side_effect = socket.error(error, "")
            assert network.Connection.recv_callback(
                self.mock, sentinel.fd, GLib.IO_IN
            )
            assert 0 == self.mock.stop.call_count 
Example 31
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_recv_callback_unrecoverable_error(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.recv.side_effect = socket.error

        assert network.Connection.recv_callback(
            self.mock, sentinel.fd, GLib.IO_IN
        )
        self.mock.stop.assert_called_once_with(any_unicode) 
Example 32
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_send_callback_respects_io_hup(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.send.return_value = 1
        self.mock.send_lock = Mock()
        self.mock.actor_ref = Mock()
        self.mock.send_buffer = b""

        assert network.Connection.send_callback(
            self.mock, sentinel.fd, (GLib.IO_IN | GLib.IO_HUP)
        )
        self.mock.stop.assert_called_once_with(any_unicode) 
Example 33
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_send_callback_respects_io_hup_and_io_err(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.send.return_value = 1
        self.mock.send_lock = Mock()
        self.mock.actor_ref = Mock()
        self.mock.send_buffer = b""

        assert network.Connection.send_callback(
            self.mock, sentinel.fd, ((GLib.IO_IN | GLib.IO_HUP) | GLib.IO_ERR)
        )
        self.mock.stop.assert_called_once_with(any_unicode) 
Example 34
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_send_callback_acquires_and_releases_lock(self):
        self.mock.send_lock = Mock()
        self.mock.send_lock.acquire.return_value = True
        self.mock.send_buffer = b""
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.send.return_value = 0

        assert network.Connection.send_callback(
            self.mock, sentinel.fd, GLib.IO_IN
        )
        self.mock.send_lock.acquire.assert_called_once_with(False)
        self.mock.send_lock.release.assert_called_once_with() 
Example 35
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_send_callback_fails_to_acquire_lock(self):
        self.mock.send_lock = Mock()
        self.mock.send_lock.acquire.return_value = False
        self.mock.send_buffer = b""
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.send.return_value = 0

        assert network.Connection.send_callback(
            self.mock, sentinel.fd, GLib.IO_IN
        )
        self.mock.send_lock.acquire.assert_called_once_with(False)
        assert 0 == self.mock._sock.send.call_count 
Example 36
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_send_recoverable_error(self):
        self.mock._sock = Mock(spec=socket.SocketType)

        for error in (errno.EWOULDBLOCK, errno.EINTR):
            self.mock._sock.send.side_effect = socket.error(error, "")

            network.Connection.send(self.mock, b"data")
            assert 0 == self.mock.stop.call_count 
Example 37
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_send_calls_socket_send_partial_send(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.send.return_value = 2

        assert b"ta" == network.Connection.send(self.mock, b"data")
        self.mock._sock.send.assert_called_once_with(b"data") 
Example 38
Project: mopidy-mpd   Author: mopidy   File: test_connection.py    License: Apache License 2.0 5 votes vote down vote up
def test_send_unrecoverable_error(self):
        self.mock._sock = Mock(spec=socket.SocketType)
        self.mock._sock.send.side_effect = socket.error

        assert b"" == network.Connection.send(self.mock, b"data")
        self.mock.stop.assert_called_once_with(any_unicode) 
Example 39
Project: Fluid-Designer   Author: Microvellum   File: test_socket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_SocketType_is_socketobject(self):
        import _socket
        self.assertTrue(socket.SocketType is _socket.socket)
        s = socket.socket()
        self.assertIsInstance(s, socket.SocketType)
        s.close() 
Example 40
Project: ironpython3   Author: IronLanguages   File: test_socket.py    License: Apache License 2.0 5 votes vote down vote up
def test_SocketType_is_socketobject(self):
        import _socket
        self.assertTrue(socket.SocketType is _socket.socket)
        s = socket.socket()
        self.assertIsInstance(s, socket.SocketType)
        s.close() 
Example 41
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 5 votes vote down vote up
def add_conn_pair(self, conn1, conn2,tmp=None, callback=None):
        """
        transfer anything between two sockets

        :type conn1: socket.SocketType
        :type conn2: socket.SocketType
        :param callback: callback in connection finish
        :type callback: Callable
        """
        # mark as readable
        self.conn_rd.add(conn1)
        self.conn_rd.add(conn2)

        # record sockets pairs
        self.map[conn1] = conn2
        self.map[conn2] = conn1



        # record callback
        if callback is not None:
            self.callbacks[conn1] = callback

        if tmp is not None:
            conn2.send(tmp)
            logging.info("tmp send:{}".format(len(tmp))) 
Example 42
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 5 votes vote down vote up
def _wr_shutdown(self, conn, once=False):
        """action when connection should be write-shutdown
        :type conn: socket.SocketType
        """
        try:
            conn.shutdown(socket.SHUT_WR)
        except:
            pass

        if not once and conn in self.map:  # use the `once` param to avoid infinite loop
            #   pair should be rd_shutdown.
            # if a socket is wr_shutdowned, then it's
            self._rd_shutdown(self.map[conn], True) 
Example 43
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 5 votes vote down vote up
def _terminate(self, conn):
        """terminate a sockets pair (two socket)
        :type conn: socket.SocketType
        :param conn: any one of the sockets pair
        """
        try_close(conn)  # close the first socket

        server_pool.ServerPool.bridgeRemove += 1

        # ------ close and clean the mapped socket, if exist ------
        if conn in self.map:
            _mapped_conn = self.map[conn]
            try_close(_mapped_conn)
            if _mapped_conn in self.map:
                del self.map[_mapped_conn]

            del self.map[conn]  # clean the first socket
        else:
            _mapped_conn = None  # just a fallback

        # ------ callback --------
        # because we are not sure which socket are assigned to callback,
        #   so we should try both
        if conn in self.callbacks:
            try:
                self.callbacks[conn]()
            except Exception as e:
                log.error("traceback error: {}".format(e))
                log.debug(traceback.format_exc())
            del self.callbacks[conn]
        elif _mapped_conn and _mapped_conn in self.callbacks:
            try:
                self.callbacks[_mapped_conn]()
            except Exception as e:
                log.error("traceback error: {}".format(e))
                log.debug(traceback.format_exc())
            del self.callbacks[_mapped_conn] 
Example 44
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 5 votes vote down vote up
def _wr_shutdown(self, conn, once=False):
        """action when connection should be write-shutdown
        :type conn: socket.SocketType
        """
        try:
            conn.shutdown(socket.SHUT_WR)
        except:
            pass

        if not once and conn in self.map:  # use the `once` param to avoid infinite loop
            #   pair should be rd_shutdown.
            # if a socket is wr_shutdowned, then it's
            self._rd_shutdown(self.map[conn], True) 
Example 45
Project: passbytcp   Author: mxdg   File: common_func.py    License: MIT License 5 votes vote down vote up
def _terminate(self, conn):
        """terminate a sockets pair (two socket)
        :type conn: socket.SocketType
        :param conn: any one of the sockets pair
        """
        try_close(conn)  # close the first socket

        # ------ close and clean the mapped socket, if exist ------
        if conn in self.map:
            _mapped_conn = self.map[conn]
            try_close(_mapped_conn)
            if _mapped_conn in self.map:
                del self.map[_mapped_conn]

            del self.map[conn]  # clean the first socket
        else:
            _mapped_conn = None  # just a fallback

        # ------ callback --------
        # because we are not sure which socket are assigned to callback,
        #   so we should try both
        if conn in self.callbacks:
            try:
                self.callbacks[conn]()
            except Exception as e:
                log.error("traceback error: {}".format(e))
                log.debug(traceback.format_exc())
            del self.callbacks[conn]
        elif _mapped_conn and _mapped_conn in self.callbacks:
            try:
                self.callbacks[_mapped_conn]()
            except Exception as e:
                log.error("traceback error: {}".format(e))
                log.debug(traceback.format_exc())
            del self.callbacks[_mapped_conn] 
Example 46
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_socket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_SocketType_is_socketobject(self):
        import _socket
        self.assertTrue(socket.SocketType is _socket.socket)
        s = socket.socket()
        self.assertIsInstance(s, socket.SocketType)
        s.close() 
Example 47
Project: CTFCrackTools-V2   Author: Acmesec   File: test_socket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def testSocketTypeAvailable(self):
        self.assertIs(socket.socket, socket.SocketType) 
Example 48
Project: CTFCrackTools   Author: Acmesec   File: test_socket.py    License: GNU General Public License v3.0 5 votes vote down vote up
def testSocketTypeAvailable(self):
        self.assertIs(socket.socket, socket.SocketType)