Python twisted.protocols.basic.FileSender() Examples

The following are 30 code examples of twisted.protocols.basic.FileSender(). 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.protocols.basic , or try the search function .
Example #1
Source File: test_basic.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_transfer(self):
        """
        L{basic.FileSender} sends the content of the given file using a
        C{IConsumer} interface via C{beginFileTransfer}. It returns a
        L{Deferred} which fires with the last byte sent.
        """
        source = BytesIO(b"Test content")
        consumer = proto_helpers.StringTransport()
        sender = basic.FileSender()
        d = sender.beginFileTransfer(source, consumer)
        sender.resumeProducing()
        # resumeProducing only finishes after trying to read at eof
        sender.resumeProducing()
        self.assertIsNone(consumer.producer)

        self.assertEqual(b"t", self.successResultOf(d))
        self.assertEqual(b"Test content", consumer.value()) 
Example #2
Source File: test_basic.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_transferMultipleChunks(self):
        """
        L{basic.FileSender} reads at most C{CHUNK_SIZE} every time it resumes
        producing.
        """
        source = BytesIO(b"Test content")
        consumer = proto_helpers.StringTransport()
        sender = basic.FileSender()
        sender.CHUNK_SIZE = 4
        d = sender.beginFileTransfer(source, consumer)
        # Ideally we would assertNoResult(d) here, but <http://tm.tl/6291>
        sender.resumeProducing()
        self.assertEqual(b"Test", consumer.value())
        sender.resumeProducing()
        self.assertEqual(b"Test con", consumer.value())
        sender.resumeProducing()
        self.assertEqual(b"Test content", consumer.value())
        # resumeProducing only finishes after trying to read at eof
        sender.resumeProducing()

        self.assertEqual(b"t", self.successResultOf(d))
        self.assertEqual(b"Test content", consumer.value()) 
Example #3
Source File: test_basic.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_abortedTransfer(self):
        """
        The C{Deferred} returned by L{basic.FileSender.beginFileTransfer} fails
        with an C{Exception} if C{stopProducing} when the transfer is not
        complete.
        """
        source = BytesIO(b"Test content")
        consumer = proto_helpers.StringTransport()
        sender = basic.FileSender()
        d = sender.beginFileTransfer(source, consumer)
        # Abort the transfer right away
        sender.stopProducing()

        failure = self.failureResultOf(d)
        failure.trap(Exception)
        self.assertEqual("Consumer asked us to stop producing",
                         str(failure.value)) 
Example #4
Source File: test_basic.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_abortedTransfer(self):
        """
        The C{Deferred} returned by L{basic.FileSender.beginFileTransfer} fails
        with an C{Exception} if C{stopProducing} when the transfer is not
        complete.
        """
        source = BytesIO(b"Test content")
        consumer = proto_helpers.StringTransport()
        sender = basic.FileSender()
        d = sender.beginFileTransfer(source, consumer)
        # Abort the transfer right away
        sender.stopProducing()

        failure = self.failureResultOf(d)
        failure.trap(Exception)
        self.assertEqual("Consumer asked us to stop producing",
                         str(failure.value)) 
Example #5
Source File: test_basic.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_transferMultipleChunks(self):
        """
        L{basic.FileSender} reads at most C{CHUNK_SIZE} every time it resumes
        producing.
        """
        source = BytesIO(b"Test content")
        consumer = proto_helpers.StringTransport()
        sender = basic.FileSender()
        sender.CHUNK_SIZE = 4
        d = sender.beginFileTransfer(source, consumer)
        # Ideally we would assertNoResult(d) here, but <http://tm.tl/6291>
        sender.resumeProducing()
        self.assertEqual(b"Test", consumer.value())
        sender.resumeProducing()
        self.assertEqual(b"Test con", consumer.value())
        sender.resumeProducing()
        self.assertEqual(b"Test content", consumer.value())
        # resumeProducing only finishes after trying to read at eof
        sender.resumeProducing()

        self.assertEqual(b"t", self.successResultOf(d))
        self.assertEqual(b"Test content", consumer.value()) 
Example #6
Source File: test_basic.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_transfer(self):
        """
        L{basic.FileSender} sends the content of the given file using a
        C{IConsumer} interface via C{beginFileTransfer}. It returns a
        L{Deferred} which fires with the last byte sent.
        """
        source = BytesIO(b"Test content")
        consumer = proto_helpers.StringTransport()
        sender = basic.FileSender()
        d = sender.beginFileTransfer(source, consumer)
        sender.resumeProducing()
        # resumeProducing only finishes after trying to read at eof
        sender.resumeProducing()
        self.assertIsNone(consumer.producer)

        self.assertEqual(b"t", self.successResultOf(d))
        self.assertEqual(b"Test content", consumer.value()) 
Example #7
Source File: attachments_resource.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def _send_attachment(self, encoding, filename, request):
        attachment = yield self.mail_service.attachment(self.attachment_id)

        bytes_io = io.BytesIO(attachment['content'])

        try:
            request.code = 200
            yield FileSender().beginFileTransfer(bytes_io, request)
        finally:
            bytes_io.close()
            request.finish() 
Example #8
Source File: util.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def startProducing(self, fd):
        self.deferred = basic.FileSender().beginFileTransfer(fd, self)
        self.deferred.addBoth(lambda x : self.stopPaging()) 
Example #9
Source File: ftp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def send(self, consumer):
        assert not self._send, "Can only call IReadFile.send *once* per instance"
        self._send = True
        d = basic.FileSender().beginFileTransfer(self.fObj, consumer)
        d.addBoth(self._close)
        return d 
Example #10
Source File: pop3.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def _sendMessageContent(self, i, fpWrapper, successResponse):
        d = self._getMessageFile(i)
        def cbMessageFile(info):
            if info is None:
                # Some error occurred - a failure response has been sent
                # already, just give up.
                return

            self._highest = max(self._highest, int(i))
            resp, fp = info
            fp = fpWrapper(fp)
            self.successResponse(successResponse(resp))
            s = basic.FileSender()
            d = s.beginFileTransfer(fp, self.transport, self.transformChunk)

            def cbFileTransfer(lastsent):
                if lastsent != '\n':
                    line = '\r\n.'
                else:
                    line = '.'
                self.sendLine(line)

            def ebFileTransfer(err):
                self.transport.loseConnection()
                log.msg("Unexpected error in _sendMessageContent:")
                log.err(err)

            d.addCallback(cbFileTransfer)
            d.addErrback(ebFileTransfer)
            return d
        return self._longOperation(d.addCallback(cbMessageFile)) 
Example #11
Source File: smtp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def smtpState_data(self, code, resp):
        s = basic.FileSender()
        d = s.beginFileTransfer(
            self.getMailData(), self.transport, self.transformChunk)
        def ebTransfer(err):
            self.sendError(err.value)
        d.addCallbacks(self.finishedFileTransfer, ebTransfer)
        self._expected = SUCCESS
        self._okresponse = self.smtpState_msgSent 
Example #12
Source File: smtp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def smtpState_msgSent(self, code, resp):
        if self._from is not None:
            self.sentMail(code, resp, len(self.successAddresses),
                          self.toAddressesResult, self.log)

        self.toAddressesResult = []
        self._from = None
        self.sendLine('RSET')
        self._expected = SUCCESS
        self._okresponse = self.smtpState_from

    ##
    ## Helpers for FileSender
    ## 
Example #13
Source File: maildir.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def startUp(self):
        self.createTempFile()
        if self.fh != -1:
            self.filesender = basic.FileSender()
            self.filesender.beginFileTransfer(self.msg, self) 
Example #14
Source File: test_tpfile.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def connectionMade(self):
        s = basic.FileSender()
        d = s.beginFileTransfer(self.f, self.transport, lambda x: x)
        d.addCallback(lambda r: self.transport.loseConnection()) 
Example #15
Source File: test_tpfile.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def testSendingEmptyFile(self):
        fileSender = basic.FileSender()
        consumer = abstract.FileDescriptor()
        consumer.connected = 1
        emptyFile = StringIO.StringIO('')

        d = fileSender.beginFileTransfer(emptyFile, consumer, lambda x: x)

        # The producer will be immediately exhausted, and so immediately
        # unregistered
        self.assertEqual(consumer.producer, None)

        # Which means the Deferred from FileSender should have been called
        self.failUnless(d.called, 
                        'producer unregistered with deferred being called') 
Example #16
Source File: stdio_test_consumer.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def connectionMade(self):
        d = basic.FileSender().beginFileTransfer(file(self.junkPath), self.transport)
        d.addErrback(failed)
        d.addCallback(lambda ign: self.transport.loseConnection()) 
Example #17
Source File: util.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def startProducing(self, fd):
        self.deferred = basic.FileSender().beginFileTransfer(fd, self)
        self.deferred.addBoth(lambda x : self.stopPaging()) 
Example #18
Source File: smtp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def smtpState_data(self, code, resp):
        s = basic.FileSender()
        d = s.beginFileTransfer(
            self.getMailData(), self.transport, self.transformChunk)
        def ebTransfer(err):
            self.sendError(err.value)
        d.addCallbacks(self.finishedFileTransfer, ebTransfer)
        self._expected = SUCCESS
        self._okresponse = self.smtpState_msgSent 
Example #19
Source File: ftp.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def send(self, consumer):
        assert not self._send, "Can only call IReadFile.send *once* per instance"
        self._send = True
        d = basic.FileSender().beginFileTransfer(self.fObj, consumer)
        d.addBoth(self._close)
        return d 
Example #20
Source File: pop3.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def _sendMessageContent(self, i, fpWrapper, successResponse):
        d = self._getMessageFile(i)
        def cbMessageFile(info):
            if info is None:
                # Some error occurred - a failure response has been sent
                # already, just give up.
                return

            self._highest = max(self._highest, int(i))
            resp, fp = info
            fp = fpWrapper(fp)
            self.successResponse(successResponse(resp))
            s = basic.FileSender()
            d = s.beginFileTransfer(fp, self.transport, self.transformChunk)

            def cbFileTransfer(lastsent):
                if lastsent != '\n':
                    line = '\r\n.'
                else:
                    line = '.'
                self.sendLine(line)

            def ebFileTransfer(err):
                self.transport.loseConnection()
                log.msg("Unexpected error in _sendMessageContent:")
                log.err(err)

            d.addCallback(cbFileTransfer)
            d.addErrback(ebFileTransfer)
            return d
        return self._longOperation(d.addCallback(cbMessageFile)) 
Example #21
Source File: smtp.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def smtpState_data(self, code, resp):
        s = basic.FileSender()
        s.beginFileTransfer(
            self.getMailData(), self.transport, self.transformChunk
        ).addCallback(self.finishedFileTransfer)
        self._expected = SUCCESS
        self._okresponse = self.smtpState_msgSent 
Example #22
Source File: smtp.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def smtpState_msgSent(self, code, resp):
        if self._from is not None:
            self.sentMail(code, resp, len(self.successAddresses),
                          self.toAddressesResult, self.log)

        self.toAddressesResult = []
        self._from = None
        self.sendLine('RSET')
        self._expected = SUCCESS
        self._okresponse = self.smtpState_from

    ##
    ## Helpers for FileSender
    ## 
Example #23
Source File: maildir.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def startUp(self):
        self.createTempFile()
        if self.fh != -1:
            self.filesender = basic.FileSender()
            self.filesender.beginFileTransfer(self.msg, self) 
Example #24
Source File: test_tpfile.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def connectionMade(self):
        s = basic.FileSender()
        d = s.beginFileTransfer(self.f, self.transport, lambda x: x)
        d.addCallback(lambda r: self.transport.loseConnection()) 
Example #25
Source File: test_tpfile.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testSendingEmptyFile(self):
        fileSender = basic.FileSender()
        consumer = abstract.FileDescriptor()
        consumer.connected = 1
        emptyFile = StringIO.StringIO('')

        d = fileSender.beginFileTransfer(emptyFile, consumer, lambda x: x)

        # The producer will be immediately exhausted, and so immediately
        # unregistered
        self.assertEqual(consumer.producer, None)

        # Which means the Deferred from FileSender should have been called
        self.failUnless(d.called, 
                        'producer unregistered with deferred being called') 
Example #26
Source File: stdio_test_consumer.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def connectionMade(self):
        d = basic.FileSender().beginFileTransfer(file(self.junkPath), self.transport)
        d.addErrback(failed)
        d.addCallback(lambda ign: self.transport.loseConnection()) 
Example #27
Source File: util.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def startProducing(self, fd):
        self.deferred = basic.FileSender().beginFileTransfer(fd, self)
        self.deferred.addBoth(lambda x : self.stopPaging()) 
Example #28
Source File: stdio_test_consumer.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def connectionMade(self):
        d = basic.FileSender().beginFileTransfer(open(self.junkPath, 'rb'),
                                                 self.transport)
        d.addErrback(failed)
        d.addCallback(lambda ign: self.transport.loseConnection()) 
Example #29
Source File: test_basic.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_interface(self):
        """
        L{basic.FileSender} implements the L{IPullProducer} interface.
        """
        sender = basic.FileSender()
        self.assertTrue(verifyObject(IProducer, sender)) 
Example #30
Source File: test_basic.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_producerRegistered(self):
        """
        When L{basic.FileSender.beginFileTransfer} is called, it registers
        itself with provided consumer, as a non-streaming producer.
        """
        source = BytesIO(b"Test content")
        consumer = proto_helpers.StringTransport()
        sender = basic.FileSender()
        sender.beginFileTransfer(source, consumer)
        self.assertEqual(consumer.producer, sender)
        self.assertFalse(consumer.streaming)