如何发出 GET 请求,然后用 Python 打印出响应 body (Twisted)

How do I make a GET request and then print out the response body with Python (Twisted)

在 Twisted 的网站上,他们有一个名为 "Receiving Responses" 的示例,它向您展示了如何获得响应 headers 和响应代码等,但不是响应 body(实际 HTML body return 由网站请求编辑。

在 def cbrequest(response) 中,我如何通过对 example.com 的 GET 请求打印出 HTML 文本 return?他们展示了像 response.headers 这样的方法来获取 headers,但我还没有看到 return 前端的 body.

的方法

我尝试打印 response.deliverBody(BeginningPrinter(finished)) 来获取响应文本,但无济于事

http://twistedmatrix.com/documents/12.1.0/web/howto/client.html#auto9

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()

您必须保留数据并return之后

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

    def dataReceived(self, bytes):
        if self.buff is None:
            self.buff = bytes
        else:
            self.buff += 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(self.buff)

请注意,您可能应该删除变量 remaining,因为此代码示例期望服务器以 [​​=13=] 字节响应。

class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.buff = None

    def dataReceived(self, bytes):
        if self.buff is None:
            self.buff = bytes
        else:
            self.buff += bytes

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