在Twisted的网站上,他们有一个名为"接收回复"它向您展示了如何获取响应头和响应代码等,而不是响应主体(请求返回到网站的实际HTML主体)。
在def cbrequest(响应)中,如何打印出来自example.com的GET请求返回的HTML文本?它们显示了诸如用于获取标题的response.headers之类的方法,但我还没有看到返回前端主体的方法。
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()
编辑:我尝试打印response.deliverBody(BeginningPrinter(已完成))以获取响应文本,但无济于事
答案 0 :(得分:0)
当我缩进你的connectionLost
方法,因此它恰好是BeginningPrinter
上的一个方法而不仅仅是一个自由浮动函数,这很好。我确实在Twisted 15.4.0上运行它,所以也许你应该尝试升级?