Python gevent.socket.timeout() Examples

The following are 17 code examples of gevent.socket.timeout(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gevent.socket , or try the search function .
Example #1
Source File: peermanager.py    From pydevp2p with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def connect(self, address, remote_pubkey):
        log.debug('connecting', address=address)
        """
        gevent.socket.create_connection(address, timeout=Timeout, source_address=None)
        Connect to address (a 2-tuple (host, port)) and return the socket object.
        Passing the optional timeout parameter will set the timeout
        getdefaulttimeout() is default
        """
        try:
            connection = create_connection(address, timeout=self.connect_timeout)
        except socket.timeout:
            log.info('connection timeout', address=address, timeout=self.connect_timeout)
            self.errors.add(address, 'connection timeout')
            return False
        except socket.error as e:
            log.info('connection error', errno=e.errno, reason=e.strerror)
            self.errors.add(address, 'connection error')
            return False
        self._start_peer(connection, address, remote_pubkey)
        return True 
Example #2
Source File: _ssl3.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def sendall(self, data, flags=0):
        self._checkClosed()
        if self._sslobj:
            if flags != 0:
                raise ValueError(
                    "non-zero flags not allowed in calls to sendall() on %s" %
                    self.__class__)

        try:
            return socket.sendall(self, data, flags)
        except _socket_timeout:
            if self.timeout == 0.0:
                # Raised by the stdlib on non-blocking sockets
                raise SSLWantWriteError("The operation did not complete (write)")
            raise 
Example #3
Source File: test_concurrency.py    From sync-engine with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_no_logging_until_many_transient_error():
    transient = [
        socket.timeout,
        socket.error,
        _mysql_exceptions.OperationalError(
            "(_mysql_exceptions.OperationalError) (1213, 'Deadlock "
            "found when trying to get lock; try restarting transaction')"),
        _mysql_exceptions.OperationalError(
            "(_mysql_exceptions.OperationalError) Lost connection to MySQL "
            "server during query"),
        _mysql_exceptions.OperationalError(
            "(_mysql_exceptions.OperationalError) MySQL server has gone away."),
        _mysql_exceptions.OperationalError(
            "(_mysql_exceptions.OperationalError) Can't connect to MySQL "
            "server on 127.0.0.1"),
        _mysql_exceptions.OperationalError(
            "(_mysql_exceptions.OperationalError) Max connect timeout reached "
            "while reaching hostgroup 71"),
        StatementError(
            message="?", statement="SELECT *", params={},
            orig=_mysql_exceptions.OperationalError(
                "(_mysql_exceptions.OperationalError) MySQL server has gone away.")),
    ]

    for transient_exc in transient:
        logger = MockLogger()
        failing_function = FailingFunction(transient_exc, max_executions=2)
        retry_with_logging(failing_function, logger=logger)

        assert logger.call_count == 0, '{} should not be logged'.format(transient_exc)
        assert failing_function.call_count == 2

        failing_function = FailingFunction(socket.error, max_executions=21)
        retry_with_logging(failing_function, logger=logger)

        assert logger.call_count == 1
        assert failing_function.call_count == 21

        failing_function = FailingFunction(socket.error, max_executions=2) 
Example #4
Source File: _sslgte279.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def sendall(self, data, flags=0):
        self._checkClosed()
        self.__check_flags('sendall', flags)

        try:
            socket.sendall(self, data)
        except _socket_timeout as ex:
            if self.timeout == 0.0:
                # Python 2 simply *hangs* in this case, which is bad, but
                # Python 3 raises SSLWantWriteError. We do the same.
                raise SSLWantWriteError("The operation did not complete (write)")
            # Convert the socket.timeout back to the sslerror
            raise SSLError(*ex.args) 
Example #5
Source File: _ssl3.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def sendall(self, data, flags=0):
        self._checkClosed()
        if self._sslobj:
            if flags != 0:
                raise ValueError(
                    "non-zero flags not allowed in calls to sendall() on %s" %
                    self.__class__)

        try:
            return socket.sendall(self, data, flags)
        except _socket_timeout:
            if self.timeout == 0.0:
                # Raised by the stdlib on non-blocking sockets
                raise SSLWantWriteError("The operation did not complete (write)")
            raise 
Example #6
Source File: _ssl2.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def sendall(self, data, flags=0):
        try:
            socket.sendall(self, data)
        except _socket_timeout as ex:
            if self.timeout == 0.0:
                # Python 2 simply *hangs* in this case, which is bad, but
                # Python 3 raises SSLWantWriteError. We do the same.
                raise SSLError(SSL_ERROR_WANT_WRITE)
            # Convert the socket.timeout back to the sslerror
            raise SSLError(*ex.args) 
Example #7
Source File: _sslgte279.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def sendall(self, data, flags=0):
        self._checkClosed()
        self.__check_flags('sendall', flags)

        try:
            socket.sendall(self, data)
        except _socket_timeout as ex:
            if self.timeout == 0.0:
                # Python 2 simply *hangs* in this case, which is bad, but
                # Python 3 raises SSLWantWriteError. We do the same.
                raise SSLWantWriteError("The operation did not complete (write)")
            # Convert the socket.timeout back to the sslerror
            raise SSLError(*ex.args) 
Example #8
Source File: _ssl3.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def sendall(self, data, flags=0):
        self._checkClosed()
        if self._sslobj:
            if flags != 0:
                raise ValueError(
                    "non-zero flags not allowed in calls to sendall() on %s" %
                    self.__class__)

        try:
            return socket.sendall(self, data, flags)
        except _socket_timeout:
            if self.timeout == 0.0:
                # Raised by the stdlib on non-blocking sockets
                raise SSLWantWriteError("The operation did not complete (write)")
            raise 
Example #9
Source File: _ssl2.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def sendall(self, data, flags=0):
        try:
            socket.sendall(self, data)
        except _socket_timeout as ex:
            if self.timeout == 0.0:
                # Python 2 simply *hangs* in this case, which is bad, but
                # Python 3 raises SSLWantWriteError. We do the same.
                raise SSLError(SSL_ERROR_WANT_WRITE)
            # Convert the socket.timeout back to the sslerror
            raise SSLError(*ex.args) 
Example #10
Source File: _sslgte279.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def sendall(self, data, flags=0):
        self._checkClosed()
        self.__check_flags('sendall', flags)

        try:
            socket.sendall(self, data)
        except _socket_timeout as ex:
            if self.timeout == 0.0:
                # Python 2 simply *hangs* in this case, which is bad, but
                # Python 3 raises SSLWantWriteError. We do the same.
                raise SSLWantWriteError("The operation did not complete (write)")
            # Convert the socket.timeout back to the sslerror
            raise SSLError(*ex.args) 
Example #11
Source File: _ssl3.py    From satori with Apache License 2.0 5 votes vote down vote up
def sendall(self, data, flags=0):
        self._checkClosed()
        if self._sslobj:
            if flags != 0:
                raise ValueError(
                    "non-zero flags not allowed in calls to sendall() on %s" %
                    self.__class__)

        try:
            return socket.sendall(self, data, flags)
        except _socket_timeout:
            if self.timeout == 0.0:
                # Raised by the stdlib on non-blocking sockets
                raise SSLWantWriteError("The operation did not complete (write)")
            raise 
Example #12
Source File: produce_and_consume.py    From Barrage with MIT License 5 votes vote down vote up
def heartbeat(sock, danmaku_queue, is_health):
    """Keep the connection alive.

    :param sock: the socket object.
    :param danmaku_queue: the queue to recieve danmaku.
    :param is_health: the status of connection
    """
    
    while True:
        try:
            send_data = send_socket_data(sock, 16, 16, 1, 2)
        except socket.timeout:
            is_health = False
        produce_danmaku.switch(sock, danmaku_queue, is_health) 
Example #13
Source File: produce_and_consume.py    From Barrage with MIT License 5 votes vote down vote up
def produce_danmaku(sock, danmaku_queue, is_health=True):
    """Produce danmakus.

    :param sock: the socket object.
    :param danmaku_queue: the queue to recieve danmaku.
    :param is_health: the status of connection
    """
    start = time.time()
    while True:
        end = time.time()
        if end - start > HEARTBEAT_KEEP_TIME:
            start = time.time()
            heartbeat.switch(sock, danmaku_queue, is_health)
        try:
            data = sock.recv(10240)
            if not data:
                break
        except socket.timeout:
            if not is_health:
                print "连接超时,准备重连服务器。。。"
                break
        except socket.error:
            break
        status = process_recieve_data(danmaku_queue, data)
        if status:
            consume_danmaku.switch(sock, danmaku_queue, is_health) 
Example #14
Source File: server.py    From ghttproxy with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, timeout=60):
        self.timeout = timeout 
Example #15
Source File: base.py    From TcpRoute with GNU General Public License v2.0 5 votes vote down vote up
def __forwardData(self,s,d,o,data_timeout=5*60):
        # TODO: 这里没有处理单方向关闭连接的情况。
        try:
            while True:
                try:
                    data=s.recv(65536)
                    #logging.debug(u'len(data)=%s'%len(data))
                    if not data:
                        break
                    o['forward_data_time'] = int(time.time()*1000)
                except _socket.timeout as e:
                    if o['forward_data_time'] + data_timeout > int(time.time()*1000):
                        # 解决下慢速下载长时间无上传造成读超时断开连接的问题
                        continue
                    raise
                d.sendall(data)
        except _socket.timeout as e:
            logging.debug(u'连接长时间无数据,关闭。')
        except _socket.error as e :
            if e.errno == 9:
                # 另一个协程关闭了链接。
                pass
            elif e.errno == 10053:
                # 远端关闭了连接
                logging.debug(u'远端关闭了连接。')
                pass
            elif e.errno == 10054:
                # 远端重置了连接
                #TODO: 部分网站直连可以成功,但是中途会重置链接。但是 multipath 只在建立连接部分做测试,并没有处理建立连接后被重置的情况。。。
                logging.debug(u'远端重置了连接。')
                pass
            else:
                logging.exception(u'DirectProxy.__forwardData')
        finally:
            # 这里 和 socks5 Handle 会重复关闭
            logging.debug(u'DirectProxy.__forwardData  finally')
            gevent.sleep(5)
            s.close()
            d.close() 
Example #16
Source File: _ssl2.py    From satori with Apache License 2.0 5 votes vote down vote up
def sendall(self, data, flags=0):
        try:
            socket.sendall(self, data)
        except _socket_timeout as ex:
            if self.timeout == 0.0:
                # Python 2 simply *hangs* in this case, which is bad, but
                # Python 3 raises SSLWantWriteError. We do the same.
                raise SSLError(SSL_ERROR_WANT_WRITE)
            # Convert the socket.timeout back to the sslerror
            raise SSLError(*ex.args) 
Example #17
Source File: _sslgte279.py    From satori with Apache License 2.0 5 votes vote down vote up
def sendall(self, data, flags=0):
        self._checkClosed()
        self.__check_flags('sendall', flags)

        try:
            socket.sendall(self, data)
        except _socket_timeout as ex:
            if self.timeout == 0.0:
                # Python 2 simply *hangs* in this case, which is bad, but
                # Python 3 raises SSLWantWriteError. We do the same.
                raise SSLWantWriteError("The operation did not complete (write)")
            # Convert the socket.timeout back to the sslerror
            raise SSLError(*ex.args)