0

I've tried the render_GET, it would only be called when the whole chunk body in received and bufferd.

How can I get the partial trunk data? I need to handle the data continuous(like stream), it is a long-exists connection(>10 mins).

Can someone help me!

Rex
  • 76
  • 10

1 Answers1

0

render_GET is generally implemented on a web server but, based on your question, it seems like you're trying to consume data from a web server instead of serving it.

What you want is easy to achieve using the Agent API. Check out this example from the Twisted Web documentation.

from __future__ import print_function

from pprint import pformat

from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
from twisted.web.client import Agent
from twisted.web.http_headers import Headers

class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.remaining = 1024 * 10

    def dataReceived(self, bytes):
        if self.remaining:
            display = bytes[:self.remaining]
            print('Some data received:')
            print(display)
            self.remaining -= len(display)

    def connectionLost(self, reason):
        print('Finished receiving body:', reason.getErrorMessage())
        self.finished.callback(None)

agent = Agent(reactor)
d = agent.request(
    'GET',
    'http://example.com/',
    Headers({'User-Agent': ['Twisted Web Client Example']}),
    None)

def cbRequest(response):
    print('Response version:', response.version)
    print('Response code:', response.code)
    print('Response phrase:', response.phrase)
    print('Response headers:')
    print(pformat(list(response.headers.getAllRawHeaders())))
    finished = Deferred()
    response.deliverBody(BeginningPrinter(finished))
    return finished
d.addCallback(cbRequest)

def cbShutdown(ignored):
    reactor.stop()
d.addBoth(cbShutdown)

reactor.run()

The dataReceived event is invoked when something arrives.

Hope this helps.

cdonts
  • 9,304
  • 4
  • 46
  • 72
  • thanks. But in this demo `dataReceived` would be invoked when some raw data arrived. In my case, I'd like to get notified when a 'chunk' is arrived. That means, I don't want to handle the parser of http-chunk, only the decoded 'body' – Rex Jun 22 '17 at 10:56