Exemplo n.º 1
0
 def dataReceived(self, p_data):
     """ This seems to be a line received function
     """
     Protocol.dataReceived(self, p_data)
     self.setLineMode()
     l_data = p_data[:-2]  # Drop the trailing CrLf
     if l_data == b'R':
         return
     LOG.info('Data Received.\n\tData:{}'.format(l_data))
Exemplo n.º 2
0
 def test_protocolToConsumer(self):
     """
     L{IProtocol} providers can be adapted to L{IConsumer} providers using
     L{ProtocolToConsumerAdapter}.
     """
     result = []
     p = Protocol()
     p.dataReceived = result.append
     consumer = IConsumer(p)
     consumer.write(b"hello")
     self.assertEqual(result, [b"hello"])
     self.assertIsInstance(consumer, ProtocolToConsumerAdapter)
Exemplo n.º 3
0
 def test_protocolToConsumer(self):
     """
     L{IProtocol} providers can be adapted to L{IConsumer} providers using
     L{ProtocolToConsumerAdapter}.
     """
     result = []
     p = Protocol()
     p.dataReceived = result.append
     consumer = IConsumer(p)
     consumer.write(b"hello")
     self.assertEqual(result, [b"hello"])
     self.assertIsInstance(consumer, ProtocolToConsumerAdapter)
Exemplo n.º 4
0
    def test_identityPumpPolicy(self):
        """
        L{identityPumpPolicy} is a pump policy which calls the target's
        C{dataReceived} method one for each string in the queue passed to it.
        """
        bytes = []
        client = Protocol()
        client.dataReceived = bytes.append
        queue = loopback._LoopbackQueue()
        queue.put(b"foo")
        queue.put(b"bar")
        queue.put(None)

        loopback.identityPumpPolicy(queue, client)

        self.assertEqual(bytes, [b"foo", b"bar"])
Exemplo n.º 5
0
    def test_identityPumpPolicy(self):
        """
        L{identityPumpPolicy} is a pump policy which calls the target's
        C{dataReceived} method one for each string in the queue passed to it.
        """
        bytes = []
        client = Protocol()
        client.dataReceived = bytes.append
        queue = loopback._LoopbackQueue()
        queue.put(b"foo")
        queue.put(b"bar")
        queue.put(None)

        loopback.identityPumpPolicy(queue, client)

        self.assertEqual(bytes, [b"foo", b"bar"])
Exemplo n.º 6
0
    def test_collapsingPumpPolicy(self):
        """
        L{collapsingPumpPolicy} is a pump policy which calls the target's
        C{dataReceived} only once with all of the strings in the queue passed
        to it joined together.
        """
        bytes = []
        client = Protocol()
        client.dataReceived = bytes.append
        queue = loopback._LoopbackQueue()
        queue.put(b"foo")
        queue.put(b"bar")
        queue.put(None)

        loopback.collapsingPumpPolicy(queue, client)

        self.assertEqual(bytes, [b"foobar"])
Exemplo n.º 7
0
    def test_collapsingPumpPolicy(self):
        """
        L{collapsingPumpPolicy} is a pump policy which calls the target's
        C{dataReceived} only once with all of the strings in the queue passed
        to it joined together.
        """
        bytes = []
        client = Protocol()
        client.dataReceived = bytes.append
        queue = loopback._LoopbackQueue()
        queue.put(b"foo")
        queue.put(b"bar")
        queue.put(None)

        loopback.collapsingPumpPolicy(queue, client)

        self.assertEqual(bytes, [b"foobar"])
Exemplo n.º 8
0
 def dataReceived(self, data):
     Protocol.dataReceived(self, data)
     self.lockBuffer.acquire().addCallback(self.AddDataAndDecode, data)
Exemplo n.º 9
0
 def dataReceived(self, p_data):
     Protocol.dataReceived(self, p_data)
 def dataReceived(self, data):
     Protocol.dataReceived(self, data)
     self.lockBuffer.acquire().addCallback(self.AddDataAndDecode, data)
 def dataReceived(self, data):
     Protocol.dataReceived(self, data)
     print "data received: ", data, ",", id(self.transport)
     self.transport.write(data)
Exemplo n.º 12
0
 def connectionMade(self):
     """Fire up stdin/stdout once we connect."""
     c = Protocol()
     c.dataReceived = self.write
     self.stdio = stdio.StandardIO(c)
Exemplo n.º 13
0
 def deliver_body(p: Protocol):
     p.dataReceived(body)
     p.connectionLost(Failure(twisted.web.client.ResponseDone()))
Exemplo n.º 14
0
 def dataReceived(self, data):
     LOG.debug('data rxed. {}'.format(data))
     Protocol.dataReceived(self, data)
Exemplo n.º 15
0
 def dataReceived(self, data):
     Protocol.dataReceived(self, data)
     print("data received: ", data, ",", id(self.transport))
     self.transport.write(data)
Exemplo n.º 16
0
 def dataReceived(self, data):
     try:
         Protocol.dataReceived(self, data)
         self.lockBuffer.acquire().addCallback(self.AddDataAndDecode, data)
     except:
         print(traceback.format_exc())
Exemplo n.º 17
0
def _receive(response, url, headers, follow_redirect, redirect_history):
    d = Deferred()
    headers = dict(response.headers.getAllRawHeaders())
    code = response.code
    length = response.length

    # check for redirects
    if follow_redirect and (code == 302 or code == 301):
        try:
            redirect = headers['Location'][0]
        except KeyError:
            raise CorruptRedirect, 'Received redirect response without Location header'

        parts = list(urlparse(redirect))
        original_parts = list(urlparse(url))

        if parts[0]=='': parts[0] = original_parts[0]
        if parts[1]=='': parts[1] = original_parts[1]

        redirect = urlunparse(parts)

        if code==301:
            _permanent_redirects[url] = redirect

        if redirect_history is None:
            redirect_history = () # comes from post, don't add as history
        else:
            redirect_history = redirect_history + (url,)

        if redirect in redirect_history:
            raise CyclicRedirect, 'Next url has already been in the redirects cycle: ' + redirect

        return get(redirect, headers, True, redirect_history)

    body = ['']
    last_modified = None

    # common closure
    def close(_):
        response = Response(code, body[0], headers, url, redirect_history)
        if last_modified is not None:
            _cache[url] = (last_modified, body[0])
        d.callback(response)

    # check for not modified:
    if code == 304:
        body[0] = _cache[url][1]
        reactor.callLater(0, close, None)
        return d

    # check for caching
    if 'Last-Modified' in headers:
        last_modified = headers['Last-Modified'][0]

    if length == 0:
        reactor.callLater(0, close, None)
        return d

    # retrieve body
    def _receiveChunk(chunk):
        body[0] = body[0] + chunk

    bodyReceiver = Protocol()
    bodyReceiver.dataReceived = _receiveChunk
    bodyReceiver.connectionLost = close

    response.deliverBody(bodyReceiver)

    return d