Python twisted.internet.error.ConnectionDone() Examples
The following are 30
code examples of twisted.internet.error.ConnectionDone().
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: __init__.py From flocker with Apache License 2.0 | 6 votes |
def make_capture_protocol(): """ Return a ``Deferred``, and a ``Protocol`` which will capture bytes and fire the ``Deferred`` when its connection is lost. :returns: A 2-tuple of ``Deferred`` and ``Protocol`` instance. :rtype: tuple """ d = Deferred() captured_data = [] class Recorder(Protocol): def dataReceived(self, data): captured_data.append(data) def connectionLost(self, reason): if reason.check(ConnectionDone): d.callback(b''.join(captured_data)) else: d.errback(reason) return d, Recorder()
Example #2
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_parserDataReceivedException(self): """ If the parser L{HTTP11ClientProtocol} delivers bytes to in C{dataReceived} raises an exception, the exception is wrapped in a L{Failure} and passed to the parser's C{connectionLost} and then the L{HTTP11ClientProtocol}'s transport is disconnected. """ requestDeferred = self.protocol.request(Request(b'GET', b'/', _boringHeaders, None)) self.protocol.dataReceived(b'unparseable garbage goes here\r\n') d = assertResponseFailed(self, requestDeferred, [ParseError]) def cbFailed(exc): self.assertTrue(self.transport.disconnecting) self.assertEqual( exc.reasons[0].value.data, b'unparseable garbage goes here') # Now do what StringTransport doesn't do but a real transport would # have, call connectionLost on the HTTP11ClientProtocol. Nothing # is asserted about this, but it's important for it to not raise an # exception. self.protocol.connectionLost(Failure(ConnectionDone(u"it is done"))) d.addCallback(cbFailed) return d
Example #3
Source File: test_http2.py From learn_python3_spider with MIT License | 6 votes |
def test_connectionLostAfterForceClose(self): """ If a timed out transport doesn't close after 15 seconds, the L{HTTPChannel} will forcibly close it. """ reactor, conn, transport = self.prepareAbortTest() # Force the follow-on forced closure. reactor.advance(15) self.assertTrue(transport.disconnecting) self.assertTrue(transport.disconnected) # Now call connectionLost on the protocol. This is done by some # transports, including TCP and TLS. We don't have anything we can # assert on here: this just must not explode. conn.connectionLost(error.ConnectionDone)
Example #4
Source File: test_newclient.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_failedWriteTo(self): """ If the L{Deferred} returned by L{Request.writeTo} fires with a L{Failure}, L{HTTP11ClientProtocol.request} disconnects its transport and returns a L{Deferred} which fires with a L{Failure} of L{RequestGenerationFailed} wrapping the underlying failure. """ class BrokenRequest: persistent = False def writeTo(self, transport): return fail(ArbitraryException()) d = self.protocol.request(BrokenRequest()) def cbFailed(ignored): self.assertTrue(self.transport.disconnecting) # Simulate what would happen if the protocol had a real transport # and make sure no exception is raised. self.protocol.connectionLost( Failure(ConnectionDone(u"you asked for it"))) d = assertRequestGenerationFailed(self, d, [ArbitraryException]) d.addCallback(cbFailed) return d
Example #5
Source File: base.py From autopush with Mozilla Public License 2.0 | 6 votes |
def _response_err(self, fail): """errBack for all exceptions that should be logged This traps all exceptions to prevent any further callbacks from running. """ from twisted.internet.error import ConnectionDone fmt = str(fail.value) or 'Exception' if isinstance(fail.value, ConnectionDone): return self.log.failure(format=fmt, failure=fail, status_code=500, errno=999, client_info=self._client_info) self._write_response(500, 999, message="An unexpected server error" " occurred.")
Example #6
Source File: test_http.py From learn_python3_spider with MIT License | 6 votes |
def test_connectionLostAfterForceClose(self): """ If a timed out transport doesn't close after 15 seconds, the L{HTTPChannel} will forcibly close it. """ clock = Clock() transport = StringTransport() factory = http.HTTPFactory() protocol = factory.buildProtocol(None) protocol = parametrizeTimeoutMixin(protocol, clock) protocol.makeConnection(transport) protocol.dataReceived(b'POST / HTTP/1.0\r\nContent-Length: 2\r\n\r\n') self.assertFalse(transport.disconnecting) self.assertFalse(transport.disconnected) # Force the initial timeout and the follow-on forced closure. clock.advance(60) clock.advance(15) self.assertTrue(transport.disconnecting) self.assertTrue(transport.disconnected) # Now call connectionLost on the protocol. This is done by some # transports, including TCP and TLS. We don't have anything we can # assert on here: this just must not explode. protocol.connectionLost(ConnectionDone)
Example #7
Source File: posixbase.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _disconnectSelectable(self, selectable, why, isRead, faildict={ error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()) }): """ Utility function for disconnecting a selectable. Supports half-close notification, isRead should be boolean indicating whether error resulted from doRead(). """ self.removeReader(selectable) f = faildict.get(why.__class__) if f: if (isRead and why.__class__ == error.ConnectionDone and IHalfCloseableDescriptor.providedBy(selectable)): selectable.readConnectionLost(f) else: self.removeWriter(selectable) selectable.connectionLost(f) else: self.removeWriter(selectable) selectable.connectionLost(failure.Failure(why))
Example #8
Source File: test_worker.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_processEnded(self): """ L{LocalWorker.processEnded} calls C{connectionLost} on itself and on the L{AMP} protocol. """ class FakeStream(object): callNumber = 0 def close(self): self.callNumber += 1 transport = FakeTransport() protocol = FakeAMProtocol() localWorker = LocalWorker(protocol, '.', 'test.log') localWorker.makeConnection(transport) localWorker._outLog = FakeStream() localWorker.processEnded(Failure(CONNECTION_DONE)) self.assertEqual(localWorker._outLog.callNumber, 1) self.assertIdentical(None, protocol.transport) return self.assertFailure(localWorker.endDeferred, ConnectionDone)
Example #9
Source File: smtp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _processConnectionError(self, connector, err): self.currentProtocol = None if (self.retries < 0) and (not self.sendFinished): log.msg("SMTP Client retrying server. Retry: %s" % -self.retries) # Rewind the file in case part of it was read while attempting to # send the message. self.file.seek(0, 0) connector.connect() self.retries += 1 elif not self.sendFinished: # If we were unable to communicate with the SMTP server a ConnectionDone will be # returned. We want a more clear error message for debugging if err.check(error.ConnectionDone): err.value = SMTPConnectError(-1, "Unable to connect to server.") self.result.errback(err.value)
Example #10
Source File: test_imap.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def testPathelogicalScatteringOfLiterals(self): self.server.checker.addUser(b'testuser', b'password-test') transport = StringTransport() self.server.makeConnection(transport) transport.clear() self.server.dataReceived(b"01 LOGIN {8}\r\n") self.assertEqual(transport.value(), b"+ Ready for 8 octets of text\r\n") transport.clear() self.server.dataReceived(b"testuser {13}\r\n") self.assertEqual(transport.value(), b"+ Ready for 13 octets of text\r\n") transport.clear() self.server.dataReceived(b"password-test\r\n") self.assertEqual(transport.value(), b"01 OK LOGIN succeeded\r\n") self.assertEqual(self.server.state, 'auth') self.server.connectionLost(error.ConnectionDone("Connection done."))
Example #11
Source File: test_imap.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_fetchWithPartialValidArgument(self): """ If by any chance, extra bytes got appended at the end of a valid FETCH arguments, the client should get a BAD - arguments invalid response. See U{RFC 3501<http://tools.ietf.org/html/rfc3501#section-6.4.5>}, section 6.4.5, """ # We need to clear out the welcome message. self.transport.clear() # Let's send out the faulty command. self.server.dataReceived(b"0001 FETCH 1 FULLL\r\n") expected = b"0001 BAD Illegal syntax: Invalid Argument\r\n" self.assertEqual(self.transport.value(), expected) self.transport.clear() self.server.connectionLost(error.ConnectionDone("Connection closed"))
Example #12
Source File: test_memcache.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_connectionLost(self): """ When disconnection occurs while commands are still outstanding, the commands fail. """ d1 = self.proto.get(b"foo") d2 = self.proto.get(b"bar") self.transport.loseConnection() done = DeferredList([d1, d2], consumeErrors=True) def checkFailures(results): for success, result in results: self.assertFalse(success) result.trap(ConnectionDone) return done.addCallback(checkFailures)
Example #13
Source File: test_memcache.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_timeOut(self): """ Test the timeout on outgoing requests: when timeout is detected, all current commands fail with a L{TimeoutError}, and the connection is closed. """ d1 = self.proto.get(b"foo") d2 = self.proto.get(b"bar") d3 = Deferred() self.proto.connectionLost = d3.callback self.clock.advance(self.proto.persistentTimeOut) self.assertFailure(d1, TimeoutError) self.assertFailure(d2, TimeoutError) def checkMessage(error): self.assertEqual(str(error), "Connection timeout") d1.addCallback(checkMessage) self.assertFailure(d3, ConnectionDone) return gatherResults([d1, d2, d3])
Example #14
Source File: test_tcp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_closePortInProtocolFactory(self): """ A port created with L{IReactorTCP.listenTCP} can be connected to with L{IReactorTCP.connectTCP}. """ f = ClosingFactory() port = reactor.listenTCP(0, f, interface="127.0.0.1") f.port = port self.addCleanup(f.cleanUp) portNumber = port.getHost().port clientF = MyClientFactory() reactor.connectTCP("127.0.0.1", portNumber, clientF) def check(x): self.assertTrue(clientF.protocol.made) self.assertTrue(port.disconnected) clientF.lostReason.trap(error.ConnectionDone) return clientF.deferred.addCallback(check)
Example #15
Source File: test_endpoints.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_connectionClosedBeforeSecure(self): """ If the connection closes at any point before the SSH transport layer has finished key exchange (ie, gotten to the point where we may attempt to authenticate), the L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires with a L{Failure} wrapping the reason for the lost connection. """ endpoint = SSHCommandClientEndpoint.newConnection( self.reactor, b"/bin/ls -l", b"dummy user", self.hostname, self.port, knownHosts=self.knownHosts, ui=FixedResponseUI(False)) factory = Factory() factory.protocol = Protocol d = endpoint.connect(factory) transport = StringTransport() factory = self.reactor.tcpClients[0][2] client = factory.buildProtocol(None) client.makeConnection(transport) client.connectionLost(Failure(ConnectionDone())) self.failureResultOf(d).trap(ConnectionDone)
Example #16
Source File: test_manhole.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_controlD(self): """ A CTRL+D in the middle of a line doesn't close a connection, but at the beginning of a line it does. """ self._testwrite(b"1 + 1") yield self.recvlineClient.expect(br"\+ 1") self._assertBuffer([b">>> 1 + 1"]) self._testwrite(manhole.CTRL_D + b" + 1") yield self.recvlineClient.expect(br"\+ 1") self._assertBuffer([b">>> 1 + 1 + 1"]) self._testwrite(b"\n") yield self.recvlineClient.expect(b"3\n>>> ") self._testwrite(manhole.CTRL_D) d = self.recvlineClient.onDisconnection yield self.assertFailure(d, error.ConnectionDone)
Example #17
Source File: test_session.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def test_wrapProtocol(self): """ L{wrapProtocol}, when passed a L{Protocol} should return something that has write(), writeSequence(), loseConnection() methods which call the Protocol's dataReceived() and connectionLost() methods, respectively. """ protocol = MockProtocol() protocol.transport = StubTransport() protocol.connectionMade() wrapped = session.wrapProtocol(protocol) wrapped.dataReceived(b'dataReceived') self.assertEqual(protocol.transport.buf, b'dataReceived') wrapped.write(b'data') wrapped.writeSequence([b'1', b'2']) wrapped.loseConnection() self.assertEqual(protocol.data, b'data12') protocol.reason.trap(error.ConnectionDone)
Example #18
Source File: test_protocol.py From flocker with Apache License 2.0 | 5 votes |
def test_stop_pinging_on_connection_lost(self): """ When the protocol loses its connection, it stops trying to send ``NoOp`` commands. """ reactor = Clock() protocol = self.build_protocol(reactor) transport = StringTransportWithAbort() protocol.makeConnection(transport) transport.clear() protocol.connectionLost(Failure(ConnectionDone("test, simulated"))) reactor.advance(PING_INTERVAL.total_seconds()) self.assertEqual(b"", transport.value())
Example #19
Source File: test_ftp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_passiveRETR(self): """ Test the RETR command in passive mode: get a file and verify its content. L{ftp.FTPClient.retrieveFile} should return a Deferred which fires with the protocol instance passed to it after the download has completed. (XXX - This API should be based on producers and consumers) """ def cbRetr(res, proto): self.assertEqual(proto.buffer, b'x' * 1000) def cbConnect(host, port, factory): self.assertEqual(host, '127.0.0.1') self.assertEqual(port, 12345) proto = factory.buildProtocol((host, port)) proto.makeConnection(proto_helpers.StringTransport()) self.client.lineReceived( b'150 File status okay; about to open data connection.') proto.dataReceived(b"x" * 1000) proto.connectionLost(failure.Failure(error.ConnectionDone(""))) self.client.connectFactory = cbConnect self._testLogin() proto = _BufferingProtocol() d = self.client.retrieveFile("spam", proto) d.addCallback(cbRetr, proto) self.assertEqual(self.transport.value(), b'PASV\r\n') self.transport.clear() self.client.lineReceived(passivemode_msg(self.client)) self.assertEqual(self.transport.value(), b'RETR spam\r\n') self.transport.clear() self.client.lineReceived(b'226 Transfer Complete.') return d
Example #20
Source File: stdio_test_loseconn.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def connectionLost(self, reason): """ Check that C{reason} is a L{Failure} wrapping a L{ConnectionDone} instance and stop the reactor. If C{reason} is wrong for some reason, log something about that in C{self.errorLogFile} and make sure the process exits with a non-zero status. """ try: try: reason.trap(ConnectionDone) except: log.err(None, "Problem with reason passed to connectionLost") self.exitCode = 1 finally: reactor.stop()
Example #21
Source File: _newclient.py From learn_python3_spider with MIT License | 5 votes |
def _finishResponse_WAITING(self, rest): # Currently the rest parameter is ignored. Don't forget to use it if # we ever add support for pipelining. And maybe check what trailers # mean. if self._state == 'WAITING': self._state = 'QUIESCENT' else: # The server sent the entire response before we could send the # whole request. That sucks. Oh well. Fire the request() # Deferred with the response. But first, make sure that if the # request does ever finish being written that it won't try to fire # that Deferred. self._state = 'TRANSMITTING_AFTER_RECEIVING_RESPONSE' self._responseDeferred.chainDeferred(self._finishedRequest) # This will happen if we're being called due to connection being lost; # if so, no need to disconnect parser again, or to call # _quiescentCallback. if self._parser is None: return reason = ConnectionDone(u"synthetic!") connHeaders = self._parser.connHeaders.getRawHeaders(b'connection', ()) if ((b'close' in connHeaders) or self._state != "QUIESCENT" or not self._currentRequest.persistent): self._giveUp(Failure(reason)) else: # Just in case we had paused the transport, resume it before # considering it quiescent again. self.transport.resumeProducing() # We call the quiescent callback first, to ensure connection gets # added back to connection pool before we finish the request. try: self._quiescentCallback(self) except: # If callback throws exception, just log it and disconnect; # keeping persistent connections around is an optimisation: self._log.failure('') self.transport.loseConnection() self._disconnectParser(reason)
Example #22
Source File: test_memcache.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_timeoutPipelining(self): """ When two requests are sent, a timeout call remains around for the second request, and its timeout time is correct. """ d1 = self.proto.get(b"foo") d2 = self.proto.get(b"bar") d3 = Deferred() self.proto.connectionLost = d3.callback self.clock.advance(self.proto.persistentTimeOut - 1) self.proto.dataReceived(b"VALUE foo 0 3\r\nbar\r\nEND\r\n") def check(result): self.assertEqual(result, (0, b"bar")) self.assertEqual(len(self.clock.calls), 1) for i in range(self.proto.persistentTimeOut): self.clock.advance(1) return self.assertFailure(d2, TimeoutError).addCallback(checkTime) def checkTime(ignored): # Check that the timeout happened C{self.proto.persistentTimeOut} # after the last response self.assertEqual( self.clock.seconds(), 2 * self.proto.persistentTimeOut - 1) d1.addCallback(check) self.assertFailure(d3, ConnectionDone) return d1
Example #23
Source File: test_memcache.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_timeOutStat(self): """ Test the timeout when stat command has started: the timeout is not reset until the final B{END} is received. """ d1 = self.proto.stats() d2 = Deferred() self.proto.connectionLost = d2.callback self.proto.dataReceived(b"STAT foo bar\r\n") self.clock.advance(self.proto.persistentTimeOut) self.assertFailure(d1, TimeoutError) self.assertFailure(d2, ConnectionDone) return gatherResults([d1, d2])
Example #24
Source File: test_memcache.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_timeOutRaw(self): """ Test the timeout when raw mode was started: the timeout is not reset until all the data has been received, so we can have a L{TimeoutError} when waiting for raw data. """ d1 = self.proto.get(b"foo") d2 = Deferred() self.proto.connectionLost = d2.callback self.proto.dataReceived(b"VALUE foo 0 10\r\n12345") self.clock.advance(self.proto.persistentTimeOut) self.assertFailure(d1, TimeoutError) self.assertFailure(d2, ConnectionDone) return gatherResults([d1, d2])
Example #25
Source File: test_httpauth.py From learn_python3_spider with MIT License | 5 votes |
def test_logoutOnError(self): """ The realm's logout callback is also invoked if there is an error generating the response (for example, if the client disconnects early). """ request = self._logoutTest() request.processingFailed( Failure(ConnectionDone("Simulated disconnect"))) self.assertEqual(self.realm.loggedOut, 1)
Example #26
Source File: proto_helpers.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def loseConnection(self): if self.connected: self.connected = False self.protocol.connectionLost( failure.Failure(error.ConnectionDone("Bye.")))
Example #27
Source File: test_agent.py From learn_python3_spider with MIT License | 5 votes |
def test_closeCachedConnections(self): """ L{HTTPConnectionPool.closeCachedConnections} closes all cached connections and removes them from the cache. It returns a Deferred that fires when they have all lost their connections. """ persistent = [] def addProtocol(scheme, host, port): p = HTTP11ClientProtocol() p.makeConnection(StringTransport()) self.pool._putConnection((scheme, host, port), p) persistent.append(p) addProtocol("http", b"example.com", 80) addProtocol("http", b"www2.example.com", 80) doneDeferred = self.pool.closeCachedConnections() # Connections have begun disconnecting: for p in persistent: self.assertEqual(p.transport.disconnecting, True) self.assertEqual(self.pool._connections, {}) # All timeouts were cancelled and removed: for dc in self.fakeReactor.getDelayedCalls(): self.assertEqual(dc.cancelled, True) self.assertEqual(self.pool._timeouts, {}) # Returned Deferred fires when all connections have been closed: result = [] doneDeferred.addCallback(result.append) self.assertEqual(result, []) persistent[0].connectionLost(Failure(ConnectionDone())) self.assertEqual(result, []) persistent[1].connectionLost(Failure(ConnectionDone())) self.assertEqual(result, [None])
Example #28
Source File: test_tcp.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def connectionLost(self, reason): msg("ClosingProtocol.connectionLost") reason.trap(error.ConnectionDone)
Example #29
Source File: test_xmlrpc.py From learn_python3_spider with MIT License | 5 votes |
def test_interruptedDeferredResponse(self): """ While waiting for the L{Deferred} returned by an L{XMLRPC} C{xmlrpc_*} method to fire, the connection the request was issued over may close. If this happens, neither C{write} nor C{finish} is called on the request. """ self.resource.render(self.request) self.request.processingFailed( failure.Failure(ConnectionDone("Simulated"))) self.result.callback("result") self.assertEqual(self.request.written, []) self.assertEqual(self.request.finished, 0)
Example #30
Source File: test_endpoints.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_connectionCancelledBeforeSecure(self): """ If the connection is cancelled before the SSH transport layer has finished key exchange (ie, gotten to the point where we may attempt to authenticate), the L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires with a L{Failure} wrapping L{CancelledError} and the connection is aborted. """ endpoint = SSHCommandClientEndpoint.newConnection( self.reactor, b"/bin/ls -l", b"dummy user", self.hostname, self.port, knownHosts=self.knownHosts, ui=FixedResponseUI(False)) factory = Factory() factory.protocol = Protocol d = endpoint.connect(factory) transport = AbortableFakeTransport(None, isServer=False) factory = self.reactor.tcpClients[0][2] client = factory.buildProtocol(None) client.makeConnection(transport) d.cancel() self.failureResultOf(d).trap(CancelledError) self.assertTrue(transport.aborted) # Make sure the connection closing doesn't result in unexpected # behavior when due to cancellation: client.connectionLost(Failure(ConnectionDone()))