Python twisted.internet.error.TimeoutError() Examples

The following are 30 code examples of twisted.internet.error.TimeoutError(). 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 twisted.internet.error , or try the search function .
Example #1
Source File: gcm.py    From autopush with Mozilla Public License 2.0 6 votes vote down vote up
def _process_error(self, failure):
        err = failure.value
        if isinstance(err, gcmclient.GCMAuthenticationError):
            self.log.error("GCM Authentication Error: %s" % err)
            raise RouterException("Server error", status_code=500,
                                  errno=901)
        if isinstance(err, TimeoutError):
            self.log.warn("GCM Timeout: %s" % err)
            self.metrics.increment("notification.bridge.error",
                                   tags=make_tags(
                                       self._base_tags,
                                       reason="timeout"))
            raise RouterException("Server error", status_code=502,
                                  errno=903,
                                  log_exception=False)
        if isinstance(err, ConnectError):
            self.log.warn("GCM Unavailable: %s" % err)
            self.metrics.increment("notification.bridge.error",
                                   tags=make_tags(
                                       self._base_tags,
                                       reason="connection_unavailable"))
            raise RouterException("Server error", status_code=502,
                                  errno=902,
                                  log_exception=False)
        return failure 
Example #2
Source File: test_errorback.py    From poi_spider with Apache License 2.0 6 votes vote down vote up
def errback_httpbin(self, failure):
        # log all failures
        self.logger.error(repr(failure))

        # in case you want to do something special for some errors,
        # you may need the failure's type:

        if failure.check(HttpError):
            print("HttpError出错了")
            # these exceptions come from HttpError spider middleware
            # you can get the non-200 response
            response = failure.value.response
            self.logger.error('HttpError on %s', response.url)

        elif failure.check(DNSLookupError):
            # this is the original request
            request = failure.request
            self.logger.error('DNSLookupError on %s', request.url)

        elif failure.check(TimeoutError, TCPTimedOutError):
            request = failure.request
            self.logger.error('TimeoutError on %s', request.url) 
Example #3
Source File: pop3client.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def connectionLost(self, reason):
        if self.timeout > 0:
            self.setTimeout(None)

        if self._timedOut:
            reason = error.TimeoutError()
        elif self._greetingError:
            reason = ServerErrorResponse(self._greetingError)

        d = []
        if self._waiting is not None:
            d.append(self._waiting)
            self._waiting = None
        if self._blockedQueue is not None:
            d.extend([deferred for (deferred, f, a) in self._blockedQueue])
            self._blockedQueue = None
        for w in d:
            w.errback(reason) 
Example #4
Source File: test_imap.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def test_serverTimeout(self):
        """
        The *client* has a timeout mechanism which will close connections that
        are inactive for a period.
        """
        c = Clock()
        self.server.timeoutTest = True
        self.client.timeout = 5 #seconds
        self.client.callLater = c.callLater
        self.selectedArgs = None

        def login():
            d = self.client.login('testuser', 'password-test')
            c.advance(5)
            d.addErrback(timedOut)
            return d

        def timedOut(failure):
            self._cbStopClient(None)
            failure.trap(error.TimeoutError)

        d = self.connected.addCallback(strip(login))
        d.addErrback(self._ebGeneral)
        return defer.gatherResults([d, self.loopback()]) 
Example #5
Source File: test_threads.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def testCallBeforeStartupUnexecuted(self):
        progname = self.mktemp()
        progfile = file(progname, 'w')
        progfile.write(_callBeforeStartupProgram % {'reactor': reactor.__module__})
        progfile.close()

        def programFinished((out, err, reason)):
            if reason.check(error.ProcessTerminated):
                self.fail("Process did not exit cleanly (out: %s err: %s)" % (out, err))

            if err:
                log.msg("Unexpected output on standard error: %s" % (err,))
            self.failIf(out, "Expected no output, instead received:\n%s" % (out,))

        def programTimeout(err):
            err.trap(error.TimeoutError)
            proto.signalProcess('KILL')
            return err

        env = os.environ.copy()
        env['PYTHONPATH'] = os.pathsep.join(sys.path)
        d = defer.Deferred().addCallbacks(programFinished, programTimeout)
        proto = ThreadStartupProcessProtocol(d)
        reactor.spawnProcess(proto, sys.executable, ('python', progname), env)
        return d 
Example #6
Source File: pop3client.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def connectionLost(self, reason):
        if self.timeout > 0:
            self.setTimeout(None)

        if self._timedOut:
            reason = error.TimeoutError()
        elif self._greetingError:
            reason = ServerErrorResponse(self._greetingError)

        d = []
        if self._waiting is not None:
            d.append(self._waiting)
            self._waiting = None
        if self._blockedQueue is not None:
            d.extend([deferred for (deferred, f, a) in self._blockedQueue])
            self._blockedQueue = None
        for w in d:
            w.errback(reason) 
Example #7
Source File: test_imap.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def testServerTimeout(self):
        self.server.timeoutTest = True
        self.client.timeout = 5 #seconds
        self.selectedArgs = None

        def login():
            d = self.client.login('testuser', 'password-test')
            d.addErrback(timedOut)
            return d

        def timedOut(failure):
            self._cbStopClient(None)
            failure.trap(error.TimeoutError)

        d = self.connected.addCallback(strip(login))
        d.addErrback(self._ebGeneral)
        self.loopback() 
Example #8
Source File: test_imap.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_serverTimeout(self):
        """
        The *client* has a timeout mechanism which will close connections that
        are inactive for a period.
        """
        c = Clock()
        self.server.timeoutTest = True
        self.client.timeout = 5 #seconds
        self.client.callLater = c.callLater
        self.selectedArgs = None

        def login():
            d = self.client.login(b'testuser', b'password-test')
            c.advance(5)
            d.addErrback(timedOut)
            return d

        def timedOut(failure):
            self._cbStopClient(None)
            failure.trap(error.TimeoutError)

        d = self.connected.addCallback(strip(login))
        d.addErrback(self._ebGeneral)
        return defer.gatherResults([d, self.loopback()]) 
Example #9
Source File: test_threads.py    From BitTorrent with GNU General Public License v3.0 6 votes vote down vote up
def testCallBeforeStartupUnexecuted(self):
        progname = self.mktemp()
        progfile = file(progname, 'w')
        progfile.write(_callBeforeStartupProgram % {'reactor': reactor.__module__})
        progfile.close()

        def programFinished((out, err, reason)):
            if reason.check(error.ProcessTerminated):
                self.fail("Process did not exit cleanly (out: %s err: %s)" % (out, err))

            if err:
                log.msg("Unexpected output on standard error: %s" % (err,))
            self.failIf(out, "Expected no output, instead received:\n%s" % (out,))

        def programTimeout(err):
            err.trap(error.TimeoutError)
            proto.signalProcess('KILL')
            return err

        env = os.environ.copy()
        env['PYTHONPATH'] = os.pathsep.join(sys.path)
        d = defer.Deferred().addCallbacks(programFinished, programTimeout)
        proto = ThreadStartupProcessProtocol(d)
        reactor.spawnProcess(proto, sys.executable, ('python', progname), env)
        return d 
Example #10
Source File: aiqiyi_spider.py    From video_url_crawler_demo with GNU General Public License v3.0 6 votes vote down vote up
def errback_httpbin(self, failure):
		# log all failures
		self.logger.error(repr(failure))

		# in case you want to do something special for some errors,
		# you may need the failure's type:
		if failure.check(HttpError):
			# these exceptions come from HttpError spider middleware
			# you can get the non-200 response
			response = failure.value.response
			self.logger.error('HttpError on %s', response.url)

		elif failure.check(DNSLookupError):
			# this is the original request
			request = failure.request
			self.logger.error('DNSLookupError on %s', request.url)

		elif failure.check(TimeoutError, TCPTimedOutError):
			request = failure.request
			self.logger.error('TimeoutError on %s', request.url) 
Example #11
Source File: test_pop3client.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testTimeout(self):
        def login():
            d = self.client.login('test', 'twisted')
            d.addCallback(loggedIn)
            d.addErrback(timedOut)
            return d

        def loggedIn(result):
            self.fail("Successfully logged in!?  Impossible!")


        def timedOut(failure):
            failure.trap(error.TimeoutError)
            self._cbStopClient(None)

        def quit():
            return self.client.quit()

        self.client.timeout = 0.01

        # Tell the server to not return a response to client.  This
        # will trigger a timeout.
        pop3testserver.TIMEOUT_RESPONSE = True

        methods = [login, quit]
        map(self.connected.addCallback, map(strip, methods))
        self.connected.addCallback(self._cbStopClient)
        self.connected.addErrback(self._ebGeneral)
        return self.loopback() 
Example #12
Source File: base.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def connect(self):
        """Start connection to remote server."""
        if self.state != "disconnected":
            raise RuntimeError, "can't connect in this state"

        self.state = "connecting"
        if not self.factoryStarted:
            self.factory.doStart()
            self.factoryStarted = 1
        self.transport = transport = self._makeTransport()
        if self.timeout is not None:
            self.timeoutID = self.reactor.callLater(self.timeout, transport.failIfNotConnected, error.TimeoutError())
        self.factory.startedConnecting(self) 
Example #13
Source File: socks5.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def handleCmdConnectFailure(self, failure):
        log.error("CMD CONNECT: %s" % failure.getErrorMessage())

        # Map common twisted errors to SOCKS error codes
        if failure.type == error.NoRouteError:
            self.sendReply(SOCKSv5Reply.NetworkUnreachable)
        elif failure.type == error.ConnectionRefusedError:
            self.sendReply(SOCKSv5Reply.ConnectionRefused)
        elif failure.type == error.TCPTimedOutError or failure.type == error.TimeoutError:
            self.sendReply(SOCKSv5Reply.TTLExpired)
        elif failure.type == error.UnsupportedAddressFamily:
            self.sendReply(SOCKSv5Reply.AddressTypeNotSupported)
        elif failure.type == error.ConnectError:
            # Twisted doesn't have a exception defined for EHOSTUNREACH,
            # so the failure is a ConnectError.  Try to catch this case
            # and send a better reply, but fall back to a GeneralFailure.
            reply = SOCKSv5Reply.GeneralFailure
            try:
                import errno
                if hasattr(errno, "EHOSTUNREACH"):
                    if failure.value.osError == errno.EHOSTUNREACH:
                        reply = SOCKSv5Reply.HostUnreachable
                if hasattr(errno, "WSAEHOSTUNREACH"):
                    if failure.value.osError == errno.WSAEHOSTUNREACH:
                        reply = SOCKSv5Reply.HostUnreachable
            except Exception:
                pass
            self.sendReply(reply)
        else:
            self.sendReply(SOCKSv5Reply.GeneralFailure)

        failure.trap(error.NoRouteError, error.ConnectionRefusedError,
                     error.TCPTimedOutError, error.TimeoutError,
                     error.UnsupportedAddressFamily, error.ConnectError) 
Example #14
Source File: client.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def handle_disconnected_connect(self):
        self.state = "connecting"
        if not self.factoryStarted:
            self.factory.doStart()
            self.factoryStarted = True

        if self.timeout is not None:
            self.timeoutID = self.reactor.callLater(self.timeout, self.connectionFailed, failure.Failure(error.TimeoutError()))

        self.sub = _SubConnector(self)
        self.sub.startConnecting()

        self.factory.startedConnecting(self) 
Example #15
Source File: client.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def _reissue(self, reason, addressesLeft, addressesUsed, query, timeout):
        reason.trap(dns.DNSQueryTimeoutError)

        # If there are no servers left to be tried, adjust the timeout
        # to the next longest timeout period and move all the
        # "used" addresses back to the list of addresses to try.
        if not addressesLeft:
            addressesLeft = addressesUsed
            addressesLeft.reverse()
            addressesUsed = []
            timeout = timeout[1:]

        # If all timeout values have been used, or the protocol has no
        # transport, this query has failed.  Tell the protocol we're
        # giving up on it and return a terminal timeout failure to our
        # caller.
        if not timeout or self.protocol.transport is None:
            self.protocol.removeResend(reason.value.id)
            return failure.Failure(defer.TimeoutError(query))

        # Get an address to try.  Take it out of the list of addresses
        # to try and put it ino the list of already tried addresses.
        address = addressesLeft.pop()
        addressesUsed.append(address)

        # Issue a query to a server.  Use the current timeout.  Add this
        # function as a timeout errback in case another retry is required.
        d = self.protocol.query(address, query, timeout[0], reason.value.id)
        d.addErrback(self._reissue, addressesLeft, addressesUsed, query, timeout)
        return d 
Example #16
Source File: client.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def _timeoutZone(self, d, controller, connector, seconds):
        connector.disconnect()
        controller.timeoutCall = None
        controller.deferred = None
        d.errback(error.TimeoutError("Zone lookup timed out after %d seconds" % (seconds,))) 
Example #17
Source File: client.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def queryUDP(self, queries, timeout = None):
        """
        Make a number of DNS queries via UDP.

        @type queries: A C{list} of C{dns.Query} instances
        @param queries: The queries to make.

        @type timeout: Sequence of C{int}
        @param timeout: Number of seconds after which to reissue the query.
        When the last timeout expires, the query is considered failed.

        @rtype: C{Deferred}
        @raise C{twisted.internet.defer.TimeoutError}: When the query times
        out.
        """
        if timeout is None:
            timeout = self.timeout

        addresses = self.servers + list(self.dynServers)
        if not addresses:
            return defer.fail(IOError("No domain name servers available"))

        # Make sure we go through servers in the list in the order they were
        # specified.
        addresses.reverse()

        used = addresses.pop()
        d = self._query(used, queries, timeout[0])
        d.addErrback(self._reissue, addresses, [used], queries, timeout)
        return d 
Example #18
Source File: client.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def _timeoutZone(self, d, controller, connector, seconds):
        connector.disconnect()
        controller.timeoutCall = None
        controller.deferred = None
        d.errback(error.TimeoutError("Zone lookup timed out after %d seconds" % (seconds,))) 
Example #19
Source File: test_pop3client.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def testTimeout(self):
        def login():
            d = self.client.login('test', 'twisted')
            d.addCallback(loggedIn)
            d.addErrback(timedOut)
            return d

        def loggedIn(result):
            self.fail("Successfully logged in!?  Impossible!")


        def timedOut(failure):
            failure.trap(error.TimeoutError)
            self._cbStopClient(None)

        def quit():
            return self.client.quit()

        self.client.timeout = 0.01

        # Tell the server to not return a response to client.  This
        # will trigger a timeout.
        pop3testserver.TIMEOUT_RESPONSE = True

        methods = [login, quit]
        map(self.connected.addCallback, map(strip, methods))
        self.connected.addCallback(self._cbStopClient)
        self.connected.addErrback(self._ebGeneral)
        return self.loopback() 
Example #20
Source File: base.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def connect(self):
        """Start connection to remote server."""
        if self.state != "disconnected":
            raise RuntimeError, "can't connect in this state"

        self.state = "connecting"
        if not self.factoryStarted:
            self.factory.doStart()
            self.factoryStarted = 1
        self.transport = transport = self._makeTransport()
        if self.timeout is not None:
            self.timeoutID = self.reactor.callLater(self.timeout, transport.failIfNotConnected, error.TimeoutError())
        self.factory.startedConnecting(self) 
Example #21
Source File: http11.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _cb_timeout(self, result, request, url, timeout):
        if self._timeout_cl.active():
            self._timeout_cl.cancel()
            return result
        # needed for HTTPS requests, otherwise _ResponseReader doesn't
        # receive connectionLost()
        if self._txresponse:
            self._txresponse._transport.stopProducing()

        raise TimeoutError("Getting %s took longer than %s seconds." % (url, timeout)) 
Example #22
Source File: base.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def connect(self):
        """Start connection to remote server."""
        if self.state != "disconnected":
            raise RuntimeError("can't connect in this state")

        self.state = "connecting"
        if not self.factoryStarted:
            self.factory.doStart()
            self.factoryStarted = 1
        self.transport = transport = self._makeTransport()
        if self.timeout is not None:
            self.timeoutID = self.reactor.callLater(self.timeout, transport.failIfNotConnected, error.TimeoutError())
        self.factory.startedConnecting(self) 
Example #23
Source File: pop3client.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def connectionLost(self, reason):
        """
        Clean up when the connection has been lost.

        When the loss of connection was initiated by the client due to a
        timeout, the L{_timedOut} flag will be set.  When it was initiated by
        the client due to an error in the server greeting, L{_greetingError}
        will be set to the server response minus the status indicator.

        @type reason: L{Failure <twisted.python.failure.Failure>}
        @param reason: The reason the connection was terminated.
        """
        if self.timeout > 0:
            self.setTimeout(None)

        if self._timedOut:
            reason = error.TimeoutError()
        elif self._greetingError:
            reason = ServerErrorResponse(self._greetingError)

        d = []
        if self._waiting is not None:
            d.append(self._waiting)
            self._waiting = None
        if self._blockedQueue is not None:
            d.extend([deferred for (deferred, f, a) in self._blockedQueue])
            self._blockedQueue = None
        for w in d:
            w.errback(reason) 
Example #24
Source File: test_pop3client.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def testTimeout(self):
        def login():
            d = self.client.login('test', 'twisted')
            d.addCallback(loggedIn)
            d.addErrback(timedOut)
            return d

        def loggedIn(result):
            self.fail("Successfully logged in!?  Impossible!")


        def timedOut(failure):
            failure.trap(error.TimeoutError)
            self._cbStopClient(None)

        def quit():
            return self.client.quit()

        self.client.timeout = 0.01

        # Tell the server to not return a response to client.  This
        # will trigger a timeout.
        pop3testserver.TIMEOUT_RESPONSE = True

        methods = [login, quit]
        map(self.connected.addCallback, map(strip, methods))
        self.connected.addCallback(self._cbStopClient)
        self.connected.addErrback(self._ebGeneral)
        return self.loopback() 
Example #25
Source File: client.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _reissue(self, reason, addressesLeft, addressesUsed, query, timeout):
        reason.trap(dns.DNSQueryTimeoutError)

        # If there are no servers left to be tried, adjust the timeout
        # to the next longest timeout period and move all the
        # "used" addresses back to the list of addresses to try.
        if not addressesLeft:
            addressesLeft = addressesUsed
            addressesLeft.reverse()
            addressesUsed = []
            timeout = timeout[1:]

        # If all timeout values have been used this query has failed.  Tell the
        # protocol we're giving up on it and return a terminal timeout failure
        # to our caller.
        if not timeout:
            return failure.Failure(defer.TimeoutError(query))

        # Get an address to try.  Take it out of the list of addresses
        # to try and put it ino the list of already tried addresses.
        address = addressesLeft.pop()
        addressesUsed.append(address)

        # Issue a query to a server.  Use the current timeout.  Add this
        # function as a timeout errback in case another retry is required.
        d = self._query(address, query, timeout[0], reason.value.id)
        d.addErrback(self._reissue, addressesLeft, addressesUsed, query, timeout)
        return d 
Example #26
Source File: client.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def _timeoutZone(self, d, controller, connector, seconds):
        connector.disconnect()
        controller.timeoutCall = None
        controller.deferred = None
        d.errback(error.TimeoutError("Zone lookup timed out after %d seconds" % (seconds,))) 
Example #27
Source File: test_threads.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def testCallBeforeStartupUnexecuted(self):
        progname = self.mktemp()
        with open(progname, 'w') as progfile:
            progfile.write(_callBeforeStartupProgram % {'reactor': reactor.__module__})

        def programFinished(result):
            (out, err, reason) = result
            if reason.check(error.ProcessTerminated):
                self.fail("Process did not exit cleanly (out: %s err: %s)" % (out, err))

            if err:
                log.msg("Unexpected output on standard error: %s" % (err,))
            self.assertFalse(
                out,
                "Expected no output, instead received:\n%s" % (out,))

        def programTimeout(err):
            err.trap(error.TimeoutError)
            proto.signalProcess('KILL')
            return err

        env = os.environ.copy()
        env['PYTHONPATH'] = os.pathsep.join(sys.path)
        d = defer.Deferred().addCallbacks(programFinished, programTimeout)
        proto = ThreadStartupProcessProtocol(d)
        reactor.spawnProcess(proto, sys.executable, ('python', progname), env)
        return d 
Example #28
Source File: fcm_v1.py    From autopush with Mozilla Public License 2.0 5 votes vote down vote up
def _process_error(self, failure):
        err = failure.value
        if isinstance(err, FCMAuthenticationError):
            self.log.error("FCM Authentication Error: {}".format(err))
            raise RouterException("Server error", status_code=500, errno=901)
        if isinstance(err, TimeoutError):
            self.log.warn("FCM Timeout: %s" % err)
            self.metrics.increment("notification.bridge.error",
                                   tags=make_tags(
                                       self._base_tags,
                                       reason="timeout"))
            raise RouterException("Server error", status_code=502,
                                  errno=903,
                                  log_exception=False)
        if isinstance(err, ConnectError):
            self.log.warn("FCM Unavailable: %s" % err)
            self.metrics.increment("notification.bridge.error",
                                   tags=make_tags(
                                       self._base_tags,
                                       reason="connection_unavailable"))
            raise RouterException("Server error", status_code=502,
                                  errno=902,
                                  log_exception=False)
        if isinstance(err, FCMNotFoundError):
            self.log.debug("FCM Recipient not found: %s" % err)
            self.metrics.increment("notification.bridge.error",
                                   tags=make_tags(
                                       self._base_tags,
                                       reason="recpient_gone"
                                   ))
            raise RouterException("FCM Recipient no longer available",
                                  status_code=404,
                                  errno=106,
                                  log_exception=False)
        if isinstance(err, RouterException):
            self.log.warn("FCM Error: {}".format(err))
            self.metrics.increment("notification.bridge.error",
                                   tags=make_tags(
                                       self._base_tags,
                                       reason="server_error"))
        return failure 
Example #29
Source File: middlewares.py    From AntSpider with MIT License 5 votes vote down vote up
def process_spider_exception(self, response, exception, spider):
        print(f'#return exception reason:{type(exception)}')
        #if isinstance(exception,TimeoutError):
        #    spider.logger.info("Request TimeoutError.")
            #return request
        #spider.logger.info('exception: %s' % spider.name)
        # Called when a spider or process_spider_input() method
        # (from other spider middleware) raises an exception.

        # Should return either None or an iterable of Response, dict
        # or Item objects.
        pass 
Example #30
Source File: http11.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def _cb_timeout(self, result, request, url, timeout):
        if self._timeout_cl.active():
            self._timeout_cl.cancel()
            return result
        # needed for HTTPS requests, otherwise _ResponseReader doesn't
        # receive connectionLost()
        if self._txresponse:
            self._txresponse._transport.stopProducing()

        raise TimeoutError("Getting %s took longer than %s seconds." % (url, timeout))